我正在为我正在构建的卷积网络不断收到错误,我不知道为什么会出现错误。这是我的网络:
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.)
知道这个错误可能来自哪里?谢谢!
答案 0 :(得分:4)
糟糕。事实证明我只需要改变
init = tf.global_variables_initializer
到
init = tf.global_variables_initializer()