【发布时间】:2022-01-25 08:58:52
【问题描述】:
我正在使用 Keras Lambda 层进行一些具有可训练权重张量的操作(或者至少应该如此);为此,我选择了一个 tf.Variable 作为参数,但尽管 trainable=True,但摘要显示 0 个可训练参数。
weights = tf.Variable(initial_value=tf.random.normal((300,)), trainable=True)
custom_layer = keras.layers.Lambda(custom_func)((input_layer, weights))
独立于 trainable=True,权重仍然不可训练。 另一种选择是使用如下层:
weights = Dense(300, activation='linear', use_bias=False)
在这种情况下,由于 tf.math.multiply 不接受(至少根据我的实验)以任何方式(我尝试了 .get_weights() 和 .variables)的密集层参数,我在 custom_func 中遇到了麻烦。
非常欢迎所有获得可训练权重张量的解决方案,提前感谢您。
【问题讨论】:
-
你能分享
custom_func代码吗? -
这可能很容易通过制作适当的自定义层来解决。
-
@newt custom_func 代码只是一个 tf.math.multiply(tensor_A, weights) 其中 tensor_A 源自对 input_layer 的一些格式良好的操作
-
@Dr.Snoopy 我找不到更好的方法来计算自定义层,其输出张量是可训练权重向量的元素乘积和模型另一层的结果.
标签: python tensorflow keras tf.keras keras-layer