发布于2020-02-21 15:27 阅读(1432) 评论(0) 点赞(25) 收藏(0)
#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).
希望大佬们能够帮我看一下
作者:我是天上的仙女
链接:https://www.pythonheidong.com/blog/article/231932/aa04c26b3346c283528c/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!