权重和偏见
self.weights = { "layer1":tensorflow.Variable(tensorflow.random_normal([self.state_size, self.neurons_layer_1])), "layer2":tensorflow.Variable(tensorflow.random_normal([self.neurons_layer_1, self.neurons_layer_2])), "layerOut":tensorflow.Variable(tensorflow.random_normal([self.neurons_layer_2, self.action_size])) }
self.biases = { "layer1":tensorflow.Variable(tensorflow.random_normal([self.neurons_layer_1])), "layer2":tensorflow.Variable(tensorflow.random_normal([self.neurons_layer_2])), "layerOut":tensorflow.Variable(tensorflow.random_normal([self.action_size])) }
模型功能
def model(self, X, weights, biases):
layer1 = tensorflow.add(tensorflow.matmul(X, weights["layer1"]),biases["layer1"])
layer1 = tensorflow.nn.relu(layer1)
layer2 = tensorflow.add(tensorflow.matmul(layer1, weights["layer2"]), biases["layer2"])
layer2 = tensorflow.nn.relu(layer2)
layerOut = tensorflow.add(tensorflow.matmul(layer2, weights["layerOut"]), biases["layerOut"])
layerOut = tensorflow.nn.softmax(layerOut)
with tensorflow.Session() as sess:
init_op = tensorflow.global_variables_initializer()
sess.run(init_op)
q_values = sess.run(layerOut)
return q_values
我这样计算预测值。状态是这样的列表:
predicted = self.model(state, self.weights, self.biases) # state = [0.4, 0.3, 0.2, 0.9]
这些是我的占位符
X_placeholder = tensorflow.placeholder("float", [None, state_size])
Y_placeholder = tensorflow.placeholder("float", [None, action_size])
这是我的成本函数
cost = tensorflow.reduce_mean(tensorflow.square(right_prediction - predicted))
这是我的优化器
optimizer = tensorflow.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(cost)
然后我初始化上面的变量
init = tensorflow.global_variables_initializer()
并运行 session
with tensorflow.Session() as sess:
sess.run(init)
for epoch in range(1):
sess.run(optimizer, feed_dict={X_placeholder:X , Y_placeholder:Y})
现在,我运行此代码,并引发以下错误。
ValueError: No gradients provided for any variable, check your graph for ops that do not
support gradients, between variables ["<tf.Variable 'Variable:0' shape=(4, 24)
dtype=float32_ref>", "<tf.Variable 'Variable_1:0' shape=(24, 24) dtype=float32_ref>",
"<tf.Variable 'Variable_2:0' shape=(24, 2) dtype=float32_ref>", "<tf.Variable
'Variable_3:0' shape=(24,) dtype=float32_ref>", "<tf.Variable 'Variable_4:0' shape=(24,)
dtype=float32_ref>", "<tf.Variable 'Variable_5:0' shape=(2,) dtype=float32_ref>",
"<tf.Variable 'Variable_6:0' shape=(4, 24) dtype=float32_ref>", "<tf.Variable
'Variable_7:0' shape=(24, 24) dtype=float32_ref>", "<tf.Variable 'Variable_8:0'
shape=(24, 2) dtype=float32_ref>",
"<tf.Variable 'Variable_9:0' shape=(24,) dtype=float32_ref>",
"<tf.Variable 'Variable_10:0' shape=(24,) dtype=float32_ref>",
"<tf.Variable 'Variable_11:0' shape=(2,)
dtype=float32_ref>"] and loss Tensor("Mean:0", shape=(), dtype=float32).
所以请帮帮我。
感谢您的关注。
编辑:好的,这是我的整个代码,我不知道这是否合法,并通过此链接发布,但here是。
最佳答案
该错误表明优化器与参数之间没有连接方式,换句话说,优化器未知参数(权重和偏差)。
由于您没有发布整个代码,因此似乎您在类中定义了参数和模型,并在其外部使用了优化器,以相同的方式执行了这两者,或者在内部定义了参数,模型,成本和优化器。在外面分类或定义它们。
关于python - 我如何摆脱这个ValueError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43935774/