gpt4 book ai didi

python - 我怎样才能摆脱这个 ValueError

转载 作者:行者123 更新时间:2023-11-30 09:19:54 32 4
gpt4 key购买 nike

权重和偏差

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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com