【问题标题】:How to get layer weight while training?训练时如何获取层重?
【发布时间】:2021-04-06 19:11:17
【问题描述】:

我有一个模型,我想在定义自定义损失函数时获取特定层的权重矩阵以使用它。

有什么方法可以在模型内部获得特定层的权重?

附:我目前正在使用 tensorflow 2 和 keras 功能 API。我测试了How do I get the weights of a layer in Keras? 方法,但它不起作用。

P.P.S.通过使用上述方法,我收到以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-26-e0bd481102a7> in <module>
      1 A_DENSE = Dense(1, use_bias = True, name = "A_DENSE")(INPUT)
----> 2 A_DENSE.get_weights()

AttributeError: 'Tensor' object has no attribute 'get_weights'

P.P.P.S.正如下面所回答的,结合自定义回调和 get_weights 解决了这个问题。祝和我有类似情况的人好运。

【问题讨论】:

  • 这能回答你的问题吗? How do I get the weights of a layer in Keras?
  • @AkshaySehgal 遗憾的是没有。如果 tensorflow2 不支持该功能,我没有在我的问题中指定。我必须先澄清这一点。谢谢
  • 我不确定你的意思,它在 tf2.请查看official 文档。
  • 我需要在训练过程中获取权重,当我这样做时,它会打印 AttributeError: 'Tensor' object has no attribute 'get_weights'。有什么想法吗?
  • 啊,你可以使用回调。让我看看我是否有一些示例代码;

标签: python keras deep-learning tensor


【解决方案1】:

您可以编写自定义Callback 并在每次纪元结束时使用它。我展示它用于打印权重,但您可以将其用作自定义损失的一部分。

class CustomCallback(keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        rand_int = tf.random.uniform((), 0, 2, dtype=tf.int32)
        print(rand_int)
        
model.fit(X, y epochs = 10, batch_size = 20, validation_split=0.1, callbacks=[CustomCallback()])

更多详情here.


例如,这是一个虚拟代码,用于在每个 epoch 之后打印 layer[1]weights and biases。您可以按照自己喜欢的方式设置功能。

from tensorflow.keras import layers, Model, callbacks

class CustomCallback(callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        print(' ')
        print(' ')
        print(model.layers[1].get_weights())
        

X, y = np.random.random((10,5)), np.random.random((10,))

inp = layers.Input((5,))
x = layers.Dense(3)(inp)
out = layers.Dense(1)(x)

model = Model(inp, out)

model.compile(loss='MAE',metrics=['accuracy'])
model.fit(X,y,callbacks=[CustomCallback()], epochs=3)
Epoch 1/3
1/1 [==============================] - ETA: 0s - loss: 0.2346 - accuracy: 0.0000e+00 
 
[array([[ 0.16518219, -0.44628695, -0.07702655],
       [-0.1993848 ,  0.03855793, -0.62964785],
       [ 0.5592851 , -0.28281152, -0.23358124],
       [ 0.05242977,  0.4023881 , -0.19522922],
       [ 0.07936202, -0.40436065,  0.10003945]], dtype=float32), array([ 0.01530731, -0.01565045, -0.01581042], dtype=float32)]
1/1 [==============================] - 0s 2ms/step - loss: 0.2346 - accuracy: 0.0000e+00
Epoch 2/3
1/1 [==============================] - ETA: 0s - loss: 0.2337 - accuracy: 0.0000e+00 
 
[array([[ 0.16814367, -0.4492649 , -0.08000461],
       [-0.19710523,  0.03622784, -0.6319782 ],
       [ 0.55797213, -0.28144714, -0.23221655],
       [ 0.05509637,  0.3996864 , -0.19793113],
       [ 0.07731982, -0.40226308,  0.10213734]], dtype=float32), array([ 0.01846951, -0.01881272, -0.01897269], dtype=float32)]
1/1 [==============================] - 0s 7ms/step - loss: 0.2337 - accuracy: 0.0000e+00
Epoch 3/3
1/1 [==============================] - ETA: 0s - loss: 0.2322 - accuracy: 0.0000e+00 
 
[array([[ 0.16706704, -0.448164  , -0.07889817],
       [-0.19894598,  0.0381193 , -0.63007975],
       [ 0.5558067 , -0.27921563, -0.22997847],
       [ 0.05663134,  0.3981127 , -0.19951159],
       [ 0.07536169, -0.400249  ,  0.10415838]], dtype=float32), array([ 0.01846951, -0.01881272, -0.01897269], dtype=float32)]
1/1 [==============================] - 0s 2ms/step - loss: 0.2322 - accuracy: 0.0000e+00

【讨论】:

  • 这种方法太棒了!谢谢你让我知道回调函数这么好的用法!
猜你喜欢
  • 2017-04-20
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多