#TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles. For reference, the tensor object was Tensor(“ArgMax:0”, shape=(64,), dtype=int64) which was passed to the feed with key Tensor(“Y:0”, dtype=int64).
这个是我在学习了卷积神经网络之后自己试着搭的一个神经网络,我用的数据是来至【这篇博客】
我用自己的方法写的程序,结果出现了有关数据维度的问题,下面是我写的程序
import tensorflow as tf
import numpy as np
import tf_utils
import cnn_utils
'''
构造阶段
'''
np.random.seed(1)
# 导入数据
X_train, Y_train, X_test, Y_test, classes = tf_utils.load_dataset()
X_train = X_train / 225
X_test = X_test / 225
print(np.shape(Y_train))
Y_train = cnn_utils.convert_to_one_hot(Y_train, 6).T # 独热编码
Y_test = cnn_utils.convert_to_one_hot(Y_test, 6).T
print(np.shape(Y_train)) # (1080,6)
print(np.shape(X_train)) # (1080,64,64,3)
print(np.shape(X_test)) # (120,64,64,3)
# 参数设置
m, n_h, n_w, n_c = X_train.shape
n_y = Y_train.shape[1]
n_epochs = 200
learning_rate = 0.01
min_batch_size = 64
# 占位符
X = tf.compat.v1.placeholder(tf.float32, shape=(None, n_h, n_w, n_c), name='X')
Y = tf.compat.v1.placeholder(tf.int64, shape=None, name='Y')
# 卷积核参数设置
with tf.name_scope('parmeters'):
W1 = tf.compat.v1.get_variable("W1", [3, 3, 3, 8], initializer=tf.contrib.layers.xavier_initializer(seed=0))
W2 = tf.compat.v1.get_variable("W2", [5, 5, 8, 16], initializer=tf.contrib.layers.xavier_initializer(seed=0))
'''
前向传播:CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FC
'''
# 前向传播
with tf.name_scope('forward_propagation'):
Z1 = tf.nn.conv2d(X, W1, strides=[1, 1, 1, 1], padding='SAME')
A1 = tf.nn.relu(Z1)
P1 = tf.nn.max_pool2d(A1, ksize=[1, 8, 8, 1], strides=[1, 8, 8, 1], padding='SAME')
Z2 = tf.nn.conv2d(P1, W2, strides=[1, 1, 1, 1], padding='SAME')
A2 = tf.nn.relu(Z2)
P2 = tf.nn.max_pool2d(A2, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='SAME')
P = tf.contrib.layers.flatten(P2) # 向量化,为全连接层做准备
Z3 = tf.contrib.layers.fully_connected(P, 6, activation_fn=None)
print(np.shape(Z3)) # (?,6)
# 计算成本
with tf.name_scope('cost'):
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z3, labels=Y))
# 反向传播,梯度下降
with tf.name_scope('train'):
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# 预测模型
with tf.name_scope('eval'):
correct = tf.nn.in_top_k(Z3, Y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
# 全局变量初始化
init = tf.compat.v1.global_variables_initializer()
'''
执行阶段
'''
with tf.compat.v1.Session() as sees:
sees.run(init)
tf.compat.v1.set_random_seed(1)
seed = 3
for epoch in range(n_epochs):
num_minibatches = int(m / min_batch_size) # 获取数据块的数量
seed = seed + 1
minibatches = cnn_utils.random_mini_batches(X_train, Y_train, min_batch_size, seed)
for minbatch in minibatches:
X_batch, Y_batch = minbatch
# print(np.shape(X_batch)) # (64,64,64,3)
# print(np.shape(Y_batch)) # (64,6)
Y_batch = tf.argmax(Y_batch, 1) # (64,)numpy.reshape
print(np.shape(Y_batch))
sees.run(train_op, feed_dict={X: X_batch, Y: Y_batch})
acc_train = accuracy.eval(feed_dict={X: X_batch, Y: Y_batch})
acc_test = accuracy.eval(feed_dict={X: X_test, Y: Y_test})
print(epoch, "Train accuracy:", acc_train, "Test accuracy:", acc_test)
这就是我运行之后报的错误,最后一句话是:TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles. For reference, the tensor object was Tensor(“ArgMax:0”, shape=(64,), dtype=int64) which was passed to the feed with key Tensor(“Y:0”, dtype=int64).
希望大佬们能够帮我看一下