日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

keras.layers.Layer中無法定義name的問題及解決_python

作者:唐僧愛吃唐僧肉 ? 更新時(shí)間: 2023-05-26 編程語言

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)容為

輸出的內(nèi)容1

進(jìn)一步查看一下對(duì)應(yīng)的weight的屬性

查看weights對(duì)應(yīng)的屬性名稱

可以看出這里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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新