網(wǎng)站首頁 編程語言 正文
keras.layers.Layer中無法定義name問題
在使用keras之中定義層的時(shí)候,如果按照以下的方法直接在keras.layers.Layer中定義相應(yīng)的self.name
會(huì)發(fā)生相應(yīng)的報(bào)錯(cuò)
import params as pp
import params_flow as pf
import tensorflow as tf
import tensorflow.keras as keras
class MyParams(tf.keras.layers.Layer):
def __init__(self):
super(MyParams,self).__init__()
self.name = 'hello'
def build(self,input_shape):
self.dense0 = keras.layers.Dense(units = 25,
#kernel_initializer = self.create_initializer(),
name = "dense0")
def call(self,inputs):
results = self.dense0(inputs)
data = MyParams()
print(data.name)
此時(shí)會(huì)相應(yīng)的報(bào)錯(cuò)
AttributeError: Can't set the attribute "name", likely because it conflicts with an existing read-only @property of the object. Please choose a different name.
也就是說,在keras之中的"self.name"為只讀屬性,不能夠被定義,此時(shí)需要更換另外一個(gè)名字。
但是只有name這個(gè)屬性能夠?qū)?yīng)到self.weights的名稱之中,那么這里我們?cè)撊绾味x呢
此時(shí)我發(fā)現(xiàn),直接定義在__init__函數(shù)之前,可以對(duì)keras的名稱完成相應(yīng)的定義
import params as pp
import params_flow as pf
import tensorflow as tf
import tensorflow.keras as keras
class MyParams(tf.keras.layers.Layer):
name = 'hello'
def __init__(self):
super(MyParams,self).__init__()
def build(self,input_shape):
self.dense0 = keras.layers.Dense(units = 25,
#kernel_initializer = self.create_initializer(),
name = "dense0")
def call(self,inputs):
results = self.dense0(inputs)
data = MyParams()
print(data.name)
同時(shí)我們傳入一個(gè)tensor的input_ids類型進(jìn)行相應(yīng)的輸入,發(fā)現(xiàn)已經(jīng)能夠?qū)eights的權(quán)重名稱進(jìn)行改變了
input_ids = keras.layers.Input(shape=(50,), dtype='int32', name="input_ids")
outputs = data(input_ids)
data.weights
對(duì)應(yīng)的輸出內(nèi)容如下
[<tf.Variable 'hello/dense0/kernel:0' shape=(50, 25) dtype=float32, numpy=
?array([[-0.10505645, ?0.09756875, ?0.14427656, ..., -0.17254017,
? ? ? ? ?-0.18592533, -0.13920134],
? ? ? ? [-0.10033116, -0.17831415, -0.03435555, ..., -0.02460951,
? ? ? ? ? 0.13194972, ?0.21918347],
? ? ? ? [ 0.15699485, -0.24836 ? , ?0.01044622, ..., ?0.04577217,
? ? ? ? ? 0.23334488, ?0.09155059],
? ? ? ? ...,
? ? ? ? [-0.22210473, ?0.14221036, ?0.07721925, ..., ?0.03358698,
? ? ? ? ? 0.08100349, ?0.15415356],
? ? ? ? [-0.1433322 , -0.00878078, -0.0760702 , ..., -0.06091703,
? ? ? ? ? 0.18796855, -0.19009456],
? ? ? ? [-0.0446853 , ?0.14639893, ?0.1729418 , ..., -0.04699725,
? ? ? ? ? 0.12940568, -0.24003454]], dtype=float32)>,
?<tf.Variable 'hello/dense0/bias:0' shape=(25,) dtype=float32, numpy=
?array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
? ? ? ? 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>]
可以發(fā)現(xiàn)這里的權(quán)重內(nèi)容已經(jīng)變成hello打頭的內(nèi)容了,然而此時(shí)又一個(gè)對(duì)應(yīng)的問題出現(xiàn)了,這得益于__init__函數(shù)前面內(nèi)容的特殊性:
如果我們將對(duì)應(yīng)的內(nèi)容改為如下
import params as pp
import params_flow as pf
import tensorflow as tf
import tensorflow.keras as keras
class MyParams(tf.keras.layers.Layer):
print('begining')
name = 'hello'
def __init__(self):
super(MyParams,self).__init__()
def build(self,input_shape):
self.dense0 = keras.layers.Dense(units = 25,
#kernel_initializer = self.create_initializer(),
name = "dense0")
def call(self,inputs):
results = self.dense0(inputs)
data = MyParams()
print(data.name)
此時(shí)運(yùn)行的時(shí)候會(huì)運(yùn)行一次print(‘begining’),輸出對(duì)應(yīng)的begining的內(nèi)容,
也就是說__init__函數(shù)之前的內(nèi)容會(huì)在定義MyParams這個(gè)類的時(shí)候就調(diào)用,而MyParams這個(gè)類只會(huì)被定義一次,也就是說__init__函數(shù)之前的內(nèi)容只會(huì)被調(diào)用一次。
這樣就帶來了一個(gè)問題,也就是name = 'hello’這里的name沒有辦法修改,也就是說我們需要想一種辦法將__init__函數(shù)前面的name='hello’修改一次,這里究竟該如何修改呢?
此時(shí)我們只需要在定義之后每次使用的時(shí)候重新對(duì)name的值進(jìn)行定義即可
具體的例子如下
import tensorflow as tf
import tensorflow.keras as keras
class MyParams(tf.keras.layers.Layer):
#print('begining')
name = 'hello'
def __init__(self):
super(MyParams,self).__init__()
def build(self,input_shape):
self.dense0 = keras.layers.Dense(units = 25,
#kernel_initializer = self.create_initializer(),
name = "dense0")
def call(self,inputs):
results = self.dense0(inputs)
data = MyParams()
print(data.name)
data.name = 'hello10000'
print(data.name)
輸出的內(nèi)容為
進(jìn)一步查看一下對(duì)應(yīng)的weight的屬性
可以看出這里weights之中的屬性名稱已經(jīng)成功地被我們定義了
總結(jié)
由于name的屬性的特殊性,如果在__init__函數(shù)之中直接定義可讀變量會(huì)造成報(bào)錯(cuò),此時(shí)我們需要調(diào)整思路,通過__init__函數(shù)之前定義name變量實(shí)現(xiàn)對(duì)于name的修改
原文鏈接:https://blog.csdn.net/znevegiveup1/article/details/115337688
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-09-17 docker安裝postgresql的圖文教程_docker
- 2022-03-18 .NET?6開發(fā)TodoList應(yīng)用之使用MediatR實(shí)現(xiàn)POST請(qǐng)求_實(shí)用技巧
- 2024-03-13 QAobject修改excel字體亂碼問題
- 2023-11-23 nginx偽靜態(tài)try_files命令解讀
- 2022-11-10 rust延遲5秒鎖屏的實(shí)現(xiàn)代碼_相關(guān)技巧
- 2022-11-22 Nginx?Tomcat負(fù)載均衡動(dòng)靜分離原理解析_nginx
- 2023-01-23 React中props使用介紹_React
- 2022-04-20 Python?設(shè)計(jì)模式中命令模式_python
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支