У меня просто есть train = optimizer.minimize(loss = tf.constant(4,dtype="float32"))
Строка кода, которую я изменяю, прежде чем все заработает.
Почему это дает ошибку? Поскольку документация говорит, что это может быть тензор Вот Документы
W = tf.Variable([0.5],tf.float32)
b = tf.Variable([0.1],tf.float32)
x = tf.placeholder(tf.float32)
y= tf.placeholder(tf.float32)
discounted_reward = tf.placeholder(tf.float32,shape=[4,], name="discounted_reward")
linear_model = W*x + b
squared_delta = tf.square(linear_model - y)
print(squared_delta)
loss = tf.reduce_sum(squared_delta*discounted_reward)
print(loss)
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss = tf.constant(4,dtype="float32"))
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(3):
sess.run(train,{x:[1,2,3,4],y:[0,-1,-2,-3],discounted_reward:[1,2,3,4]})
print(sess.run([W,b]))
Мне действительно нужна эта вещь для работы. В этом конкретном примере у нас могут быть другие способы ее решения, но мне нужно, чтобы это работало, поскольку мой реальный код может сделать это только
Ошибка
> ValueError: No gradients provided for any variable, check your graph
> for ops that do not support gradients, between variables
> ["<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_2:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_3:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_4:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_5:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_6:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_7:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_8:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_9:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_10:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_11:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_12:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_13:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_14:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_15:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_16:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_17:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_18:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_19:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_20:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_21:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_22:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_23:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_24:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_25:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_26:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_27:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_28:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_29:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_30:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_31:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_32:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_33:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_34:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_35:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_36:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_37:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_38:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_39:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_40:0' shape=(1,) dtype=float32_ref>",
> "<tf.Variable 'Variable_41:0' shape=(1,) dtype=float32_ref>"] and loss
> Tensor("Const_4:0", shape=(), dtype=float32).