我有一个模型,该模型需要两个输入(分别为[50x2特征]和连续[50x714特征]变量)。我想使用以下代码来计算相对于目标的输入梯度:
features_tens = [tf.convert_to_tensor(x) for x in features]
def get_gradients(data_input):
with tf.GradientTape() as tape:
tape.watch(data_input)
# Run the forward pass of the layer.
# The operations that the layer applies
# to its inputs are going to be recorded
# on the GradientTape.
preds = tf_model(data_input, training=False)
# Use the gradient tape to automatically retrieve
# the gradients of the trainable variables with respect to the target.
grads = tape.gradient(target=preds, sources=data_input)
return grads
grads = get_gradients(data_input=features_tens)
应届毕业生的毕业证书为:
[None, <tf.Tensor: id=1237340, shape=(50, 714), dtype=float32, numpy=
array([[ 3.2849261e-05, 1.1156656e+00, -1.0219267e-02, ...,
-6.4205074e-01, -1.5517103e-02, -5.7817552e-02],
[ 1.6995892e-04, 1.0363499e+00, -8.8197924e-02, ...,
-5.7580328e-01, 6.5039340e-03, 1.1646263e-01],
[-2.5057874e-04, 1.4367001e+00, -1.3081819e-02, ...,
-6.8868393e-01, -3.5415408e-01, 2.4788140e-01],
...,
[ 2.5332673e-04, 1.0083977e+00, -3.3813875e-02, ...,
-4.5079905e-01, 9.5710188e-02, 5.0962836e-01],
[ 2.3785071e-04, 1.3727474e+00, -4.5678988e-02, ...,
-6.2601656e-01, -2.9648536e-01, 3.2699940e-01],
[ 1.5690899e-04, 1.0958921e+00, -1.1370061e-02, ...,
-3.3662343e-01, -1.6279417e-01, 9.6774891e-02]], dtype=float32)>]
第一部分是None
,第二部分是一些渐变,但我不确定它们是否正确。
这到底是怎么回事?