gpt4 book ai didi

python - TensorFlow:变量初始化中的 "Attempting to use uninitialized value"

转载 作者:IT老高 更新时间:2023-10-28 21:36:58 26 4
gpt4 key购买 nike

我正在尝试使用 TensorFlow 在 Python 中实现多元线性回归,但遇到了一些逻辑和实现问题。我的代码抛出以下错误:

Attempting to use uninitialized value Variable
Caused by op u'Variable/read'

理想情况下,weights 输出应该是 [2, 3]

def hypothesis_function(input_2d_matrix_trainingexamples,
output_matrix_of_trainingexamples,
initial_parameters_of_hypothesis_function,
learning_rate, num_steps):
# calculate num attributes and num examples
number_of_attributes = len(input_2d_matrix_trainingexamples[0])
number_of_trainingexamples = len(input_2d_matrix_trainingexamples)

#Graph inputs
x = []
for i in range(0, number_of_attributes, 1):
x.append(tf.placeholder("float"))
y_input = tf.placeholder("float")

# Create Model and Set Model weights
parameters = []
for i in range(0, number_of_attributes, 1):
parameters.append(
tf.Variable(initial_parameters_of_hypothesis_function[i]))

#Contruct linear model
y = tf.Variable(parameters[0], "float")
for i in range(1, number_of_attributes, 1):
y = tf.add(y, tf.multiply(x[i], parameters[i]))

# Minimize the mean squared errors
loss = tf.reduce_mean(tf.square(y - y_input))
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(loss)

#Initialize the variables
init = tf.initialize_all_variables()

# launch the graph
session = tf.Session()
session.run(init)
for step in range(1, num_steps + 1, 1):
for i in range(0, number_of_trainingexamples, 1):
feed = {}
for j in range(0, number_of_attributes, 1):
array = [input_2d_matrix_trainingexamples[i][j]]
feed[j] = array
array1 = [output_matrix_of_trainingexamples[i]]
feed[number_of_attributes] = array1
session.run(train, feed_dict=feed)

for i in range(0, number_of_attributes - 1, 1):
print (session.run(parameters[i]))

array = [[0.0, 1.0, 2.0], [0.0, 2.0, 3.0], [0.0, 4.0, 5.0]]
hypothesis_function(array, [8.0, 13.0, 23.0], [1.0, 1.0, 1.0], 0.01, 200)

最佳答案

运行这个:

init = tf.global_variables_initializer()
sess.run(init)

或者(取决于您拥有的 TF 版本):

init = tf.initialize_all_variables()
sess.run(init)

关于python - TensorFlow:变量初始化中的 "Attempting to use uninitialized value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36007883/

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