我需要从Tensorflow/Keras2.0 (super_model
)中实现的神经网络中获取导数。由于我之前在this post中解释的问题,这个模型由多个基本模型(从x1
到x6
)组成。(因此,如果只将角度传递给模型,我将得到一个错误。)请参阅以下代码:
angles=[0] * 21
data = {
'x1_model_input': numpy.array([angles[0:3]]),
'x2_model_input': numpy.array([angles[3:6]]),
'x3_model_input': numpy.array([[angles[6]]]),
'x4_model_input': numpy.array([angles[7:13]]),
'x5_model_input': numpy.array([angles[13:15]]),
'x6_model_input': numpy.array([angles[15:21]])
}
# this super_model prediction is working well
pred = super_model.predict(data) # `pred` shape is `shape=(1,1)`
现在,我需要使用GradientTape
根据输入数据对网络进行求导。我尝试了以下方法,目的是获得上述指定数据的网络梯度值:
with tf.GradientTape() as tape:
pred = super_model(data)
# does not work as `data` is a dictionary
# the error is:
# ...
# return pywrap_tfe.TFE_Py_TapeGradient(
# AttributeError: 'numpy.ndarray' object has no attribute '_id'
grad = tape.gradient(pred, data)
但是,data
是一个字典,我不能先调用tape.watch
,然后调用gradient
。我也不能通过data
调用tf.convert_to_tesnor
,因为它是一个字典。因此,我的问题是如何在不更改super_model
结构的情况下继续工作
发布于 2021-11-13 06:25:57
我不确定这对您来说是否是一个可行的选择,但是您的代码可以使用tf.Variable
代替numpy
import tensorflow as tf
angles=[0] * 21
test_tensor = tf.Variable([angles[0:3]], dtype=tf.float32)
data = {
'x1_model_input': test_tensor,
'x2_model_input': tf.Variable([angles[3:6]], dtype=tf.float32),
'x3_model_input': tf.Variable([[angles[6]]], dtype=tf.float32),
'x4_model_input': tf.Variable([angles[7:13]], dtype=tf.float32),
'x5_model_input': tf.Variable([angles[13:15]], dtype=tf.float32),
'x6_model_input': tf.Variable([angles[15:21]], dtype=tf.float32)
}
with tf.GradientTape() as tape:
pred = tf.constant([[1.0]]) * test_tensor
grad = tape.gradient(pred, data)
tf.print(grad)
{'x1_model_input': [[1 1 1]],
'x2_model_input': None,
'x3_model_input': None,
'x4_model_input': None,
'x5_model_input': None,
'x6_model_input': None}
https://stackoverflow.com/questions/69954835
复制