Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow 2:GradientTape返回无_Python_Tensorflow_Deep Learning - Fatal编程技术网

Python Tensorflow 2:GradientTape返回无

Python Tensorflow 2:GradientTape返回无,python,tensorflow,deep-learning,Python,Tensorflow,Deep Learning,我有以下代码,用tf.keras和Tensorflow 2编写。基本上;我需要交叉熵项相对于变量self.temperature的梯度dce1_dx正确计算导数。但另一方面,当我将相同的交叉熵计算包装到tf.keras.Model对象中时,第二个梯度计算,dce2\u dx返回None。这两个tf.GradientTape计算之间有什么区别?我在TF1方面很有经验,但最近切换到了TF2并渴望执行,所以我被困在了这一点上 import numpy as np import tensorflow a

我有以下代码,用tf.keras和Tensorflow 2编写。基本上;我需要交叉熵项相对于变量
self.temperature
的梯度
dce1_dx
正确计算导数。但另一方面,当我将相同的交叉熵计算包装到tf.keras.Model对象中时,第二个梯度计算,
dce2\u dx
返回
None
。这两个tf.GradientTape计算之间有什么区别?我在TF1方面很有经验,但最近切换到了TF2并渴望执行,所以我被困在了这一点上

import numpy as np
import tensorflow as tf

logits = np.random.uniform(low=-10.0, high=10.0, size=(10000, 5))
labels = np.random.randint(low=0, high=5, size=(10000, ))
logits_tf = tf.keras.Input(name="logits_tf", shape=(logits.shape[1]), dtype=tf.float32)
labels_tf = tf.keras.Input(name="labels_tf", shape=(), dtype=tf.int32)
dataset = tf.data.Dataset.from_tensor_slices((logits, labels))
dataset = dataset.batch(batch_size=logits.shape[0])
for lgts, idx in dataset:
   temperature = tf.Variable(name="temperature", dtype=tf.float32, initial_value=tf.constant(2.0),
                               trainable=True)
   scaled_logits = logits_tf / temperature
   ce_loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
   ce_loss = ce_loss(labels_tf, scaled_logits)
   model = tf.keras.Model(inputs=[logits_tf, labels_tf], outputs=[ce_loss], name="calibration_model")
   with tf.GradientTape() as tape0:
       tape0.watch(temperature)
       scaled_lgts = tf.cast(lgts, tf.float32) / temperature
       ce = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
       ce = ce(idx, scaled_lgts)
   dce1_dx = tape0.gradient(ce, temperature)

   with tf.GradientTape() as tape1:
       # Compute the derivative: d{CrossEntropy}/d{Temperature}
       tape1.watch(temperature)
       ce2 = model([lgts, idx])
   # !!!Returns None!!!
   dce2_dx = tape1.gradient(ce2, temperature)

你试过阅读文档吗?您是否检查了
ce_loss
是否以您预期的值结束?@KarlKnechtel是的,ce_loss似乎具有预期的值。我试图遵循tf.GradientTape的文档。什么是
logitsTf
labelsTf
?A会有帮助的。@Lescurel我已经编辑了截取的代码;现在可以复制粘贴。我不能用TF2,4复制。我得到
dce2\u dx=