程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-05(4)

tensorflow报错

发布于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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

这就是我运行之后报的错误,最后一句话是: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).

这就是我
希望大佬们能够帮我看一下

发布了1 篇原创文章 · 获赞 0 · 访问量 9


所属网站分类: 技术文章 > 博客

作者:我是天上的仙女

链接:https://www.pythonheidong.com/blog/article/231932/aa04c26b3346c283528c/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

25 0
收藏该文
已收藏

评论内容:(最多支持255个字符)