我目前正在使用tensorflow 1.7
由于某些原因,未应用优化程序梯度。这是我的简单代码:
import tensorflow as tf
import numpy as np
print("tensorflow version={}".format(tf.__version__))
a= np.zeros((2,2), dtype="float32")
b= np.array([[6,7],[8,9]], dtype="float32")
t1= tf.placeholder(tf.float32, shape=(2,2))
label_t = tf.placeholder(tf.float32, shape=(2,2))
t2 = tf.layers.dense(t1,2, activation=tf.nn.relu)
loss = tf.reduce_sum(tf.square(t2 - label_t))
optimizer = tf.train.AdamOptimizer(0.1)
train_op = optimizer.minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
steps=2
for i in range(steps):
print("trainop", train_op)
pred, loss_val, _= sess.run([t2,loss, train_op], feed_dict={t1:a, label_t:b})
print("pred={}, loss={}".format(pred, loss_val))
输出为:
trainop name: "Adam"
op: "NoOp"
input: "^Adam/update_dense/kernel/ApplyAdam"
input: "^Adam/update_dense/bias/ApplyAdam"
input: "^Adam/Assign"
input: "^Adam/Assign_1"
pred=[[0. 0.]
[0. 0.]], loss=230.0
trainop name: "Adam"
op: "NoOp"
input: "^Adam/update_dense/kernel/ApplyAdam"
input: "^Adam/update_dense/bias/ApplyAdam"
input: "^Adam/Assign"
input: "^Adam/Assign_1"
pred=[[0. 0.]
[0. 0.]], loss=230.0
我尝试将minimize()
分为compute_gradient
和apply_gradient
。即使这样,似乎也不会应用梯度(损耗值保持不变,可训练变量保持不变)。
谁能给我一个关于我做错了什么的提示?
答案 0 :(得分:0)
由于@BugKiller在评论中帮助了我,原因是我对激活功能的选择不正确。如果删除显式激活分配,则可以看到损失在减少。正如@BugKiller在评论中所指出的那样,从一开始就盲目使用tf.nn.relu
函数作为激活函数是我的错。