gpt4 book ai didi

python - 无法将函数转换为张量或运算

转载 作者:行者123 更新时间:2023-11-28 22:30:07 24 4
gpt4 key购买 nike

我正在构建的卷积网络不断出现错误,我不知道为什么会弹出错误。这是我的网络:

learning_rate = 0.0001
epochs = 10
batch_size = 128
test_valid_size = 128
n_classes = 5
dropout = 0.75

start1 = timer()

with tf.device('/gpu:0'):
weights = {
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
'out': tf.Variable(tf.random_normal([1024, n_classes]))}
biases = {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bd1': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([n_classes]))}

x = tf.placeholder(tf.float32, [None, 80, 240, 1])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32)

layer_1 = tf.nn.conv2d(x, weights['wc1'], strides = [1, 1, 1, 1], padding = 'SAME')
layer_1 = tf.nn.bias_add(layer_1, biases['bc1'])
layer_1 = tf.nn.max_pool(layer_1, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = 'SAME')

layer_2 = tf.nn.conv2d(layer_1, weights['wc2'], strides = [1, 1, 1, 1], padding = 'SAME')
layer_2 = tf.nn.bias_add(layer_2, biases['bc2'])
layer_2 = tf.nn.max_pool(layer_2, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = 'SAME')

layer_3 = tf.reshape(layer_2, [-1, weights['wd1'].get_shape().as_list()[0]])
layer_3 = tf.add(tf.matmul(layer_3, weights['wd1']), biases['bd1'])
layer_3 = tf.nn.relu(layer_3)
layer_3 = tf.nn.dropout(layer_3, dropout)

logits = tf.add(tf.matmul(layer_3, weights['out']), biases['out'])

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(cost)

correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

init = tf.global_variables_initializer

save_file_2 = './train_model2.ckpt'
saver = tf.train.Saver()

with tf.Session() as sess:
sess.run(init)

for epoch in range(epochs):
for batch_features, batch_labels in batches(batch_size, count_X_train, count_y_train):
sess.run(optimizer, feed_dict = {x:batch_features, y:batch_labels, keep_prob:dropout})

saver.save(sess, save_file_2)
print("")
print("trained model_2 saved")

test_acc = sess.run(accuracy, feed_dict = {x: count_X_test[:test_valid_size], y: count_y_test[:test_valid_size], keep_prob:1.})
print('Testing Accuracy: {}'.format(test_acc))


end1 = timer()
print("time:", str(end1 - start1))

在代码的某处,我猜 sess.run(init) 给我以下错误:

TypeError: Fetch argument <function global_variables_initializer at 0x00000224FD81FD90> has invalid type <class 'function'>, must be a string or Tensor. (Can not convert a function into a Tensor or Operation.)

知道这个错误可能来自哪里吗?谢谢!

最佳答案

糟糕。原来我只需要改变

init = tf.global_variables_initializer 

init = tf.global_variables_initializer()

关于python - 无法将函数转换为张量或运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42562837/

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