我想计算 tf 模型权重的梯度,但只在一个方向:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(from_logits=False))
features = tf.random.normal((1000,10))
labels = tf.random.normal((1000,))
model.fit(features, labels, batch_size=32, epochs=1)
x_star = model.layers[0].weights #the layer has kernel and bias
v = tf.random.normal((10,1)) #direction of the gradient
def directional_loss(model, x, y, t):
model.layers[0].set_weights([x_star[0] + t*v, x_star[1]])
y_ = model(x)
return model.loss(y_true=y, y_pred=y_)
def directional_grad(model, inputs, targets, t):
with tf.GradientTape() as tape:
loss_value = directional_loss(model, inputs, targets, t)
return loss_value, tape.gradient(loss_value, t)
t=0.
loss_value, grads = directional_grad(model, features, labels, t)
但它返回以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in directional_grad
File "C:\Users\pierr\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\backprop.py", line 1070, in gradient
if not backprop_util.IsTrainable(t):
File "C:\Users\pierr\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\backprop_util.py", line 58, in IsTrainable
dtype = dtypes.as_dtype(dtype)
File "C:\Users\pierr\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\dtypes.py", line 725, in as_dtype
raise TypeError(f"Cannot convert value {type_value!r} to a TensorFlow DType.")
TypeError: Cannot convert value 0.0 to a TensorFlow DType.
我认为这是因为操作model.layers[0].set_weights
不是“可微分的”。
我该如何解决?或者,在 TensorFlow 中,我可以通过直接指定权重来计算层的输出,例如y = layer(x, weights=w)
?