首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tf.GradientTape()返回None

tf.GradientTape()返回None
EN

Stack Overflow用户
提问于 2018-08-24 04:47:50
回答 1查看 2.5K关注 0票数 2

我试着用tf.GradientTape来计算梯度。当我尝试这样做时,使用loss和Model.variables (tf.keras.Model)作为输入,这是一个返回None数组的结果。我做错了什么?我使用的tensorflow版本是1.9。

代码语言:javascript
复制
Model = CubeValModel(TrainingCurves)

LearningRate = 0.0005
TrainOpe = tf.train.AdamOptimizer(LearningRate, name="MainTrainingOpe")

for i in range (5):
    with tf.GradientTape() as t:
        Predictions = tf.nn.softmax(Model.FinalFC, name="SoftmaxPredictions")
        Cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=Predictions, labels=TrainingLabels, name="CrossEntropy")
        Loss = tf.reduce_mean(Cross_entropy, name="Loss")
        print (Loss)
        print (Model.variables)
        Gradients = t.gradient(Loss, Model.variables)
        print(Gradients)

输出:

代码语言:javascript
复制
tf.Tensor(0.84878147, shape=(), dtype=float32)

[<tf.Variable 'LayerBlock1/Weights1:0' shape=(1, 3, 1, 3) dtype=float32, numpy=

[None, None, None, None, None, None, None, None, None]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-24 21:02:52

我假设您使用的是TensorFlow eager,不是吗?如果我没记错的话,在tf.GradientTape()下,您应该调用计算模型的方法,而不是调用它的一个成员。这个计算执行将允许t计算出它需要在以后生成哪些梯度。我希望这能帮到你

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51994043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档