網站首頁 編程語言 正文
keras.layers.Layer中無法定義name問題
在使用keras之中定義層的時候,如果按照以下的方法直接在keras.layers.Layer中定義相應的self.name
會發生相應的報錯
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)
此時會相應的報錯
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"為只讀屬性,不能夠被定義,此時需要更換另外一個名字。
但是只有name這個屬性能夠對應到self.weights的名稱之中,那么這里我們該如何定義呢
此時我發現,直接定義在__init__函數之前,可以對keras的名稱完成相應的定義
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)
同時我們傳入一個tensor的input_ids類型進行相應的輸入,發現已經能夠對weights的權重名稱進行改變了
input_ids = keras.layers.Input(shape=(50,), dtype='int32', name="input_ids")
outputs = data(input_ids)
data.weights
對應的輸出內容如下
[<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)>]
可以發現這里的權重內容已經變成hello打頭的內容了,然而此時又一個對應的問題出現了,這得益于__init__函數前面內容的特殊性:
如果我們將對應的內容改為如下
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)
此時運行的時候會運行一次print(‘begining’),輸出對應的begining的內容,
也就是說__init__函數之前的內容會在定義MyParams這個類的時候就調用,而MyParams這個類只會被定義一次,也就是說__init__函數之前的內容只會被調用一次。
這樣就帶來了一個問題,也就是name = 'hello’這里的name沒有辦法修改,也就是說我們需要想一種辦法將__init__函數前面的name='hello’修改一次,這里究竟該如何修改呢?
此時我們只需要在定義之后每次使用的時候重新對name的值進行定義即可
具體的例子如下
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)
輸出的內容為
進一步查看一下對應的weight的屬性
可以看出這里weights之中的屬性名稱已經成功地被我們定義了
總結
由于name的屬性的特殊性,如果在__init__函數之中直接定義可讀變量會造成報錯,此時我們需要調整思路,通過__init__函數之前定義name變量實現對于name的修改
原文鏈接:https://blog.csdn.net/znevegiveup1/article/details/115337688
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-07-21 C語言全面細致講解單雙精度float與double的使用方法_C 語言
- 2024-03-24 spring boot 3 + spring cloud sleuth 無法注入Tracer問題
- 2022-04-18 C語言復數的加減及輸出結構體_C 語言
- 2022-12-28 C++關鍵字const使用方法詳解_C 語言
- 2022-01-16 jQuery 核心函數和動態更新員工表
- 2022-07-18 RabbitMQ隊列阻塞該如何處理
- 2023-01-04 go?doudou應用中使用枚舉類型教程示例_Golang
- 2022-09-04 go語言中的面向對象_Golang
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支