ValueError:无法为Tensor' Placeholder:0'提供形状值(128,28,28),其形状为'(?,784)'

时间:2018-02-24 06:23:37

标签: numpy tensorflow machine-learning neural-network training-data

我是Tensorflow和机器学习的新手,并使用Tensorflow和我的自定义输入数据尝试CNN。但是我收到了下面的错误。

数据或图像尺寸为28x28,带有15个标签。 我没有在这个脚本或错误中得到numpy reshape的东西。

非常感谢帮助。

enter image description here

import tensorflow as tf
import os
import skimage.data
import numpy as np
import random

def load_data(data_directory):
    directories = [d for d in os.listdir(data_directory) 
                   if os.path.isdir(os.path.join(data_directory, d))]
    labels = []
    images = []
    for d in directories:
        label_directory = os.path.join(data_directory, d)
        file_names = [os.path.join(label_directory, f) 
                      for f in os.listdir(label_directory) 
                      if f.endswith(".jpg")]
        for f in file_names:
            images.append(skimage.data.imread(f))
            labels.append(d)
        print(str(d)+' Completed')
    return images, labels

ROOT_PATH = "H:\Testing\TrainingData"
train_data_directory = os.path.join(ROOT_PATH, "Training")
test_data_directory = os.path.join(ROOT_PATH, "Testing")

print('Loading Data...')
images, labels = load_data(train_data_directory)
print('Data has been Loaded')

n_classes = 15
training_examples = 10500
test_examples = 4500
batch_size = 128

x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float')

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')

def maxpool2d(x):
    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

def neural_network_model(x):
    weights = {'W_Conv1':tf.Variable(tf.random_normal([5,5,1,32])),
               'W_Conv2':tf.Variable(tf.random_normal([5,5,32,64])),
               'W_FC':tf.Variable(tf.random_normal([7*7*64, 1024])),
               'Output':tf.Variable(tf.random_normal([1024, n_classes]))}

    biases = {'B_Conv1':tf.Variable(tf.random_normal([32])),
               'B_Conv2':tf.Variable(tf.random_normal([64])),
               'B_FC':tf.Variable(tf.random_normal([1024])),
               'Output':tf.Variable(tf.random_normal([n_classes]))}   

    x = tf.reshape(x, shape=[-1,28,28,1])

    conv1 = conv2d(x, weights['W_Conv1'])
    conv1 = maxpool2d(conv1)

    conv2 = conv2d(conv1, weights['W_Conv2'])
    conv2 = maxpool2d(conv2)

    fc = tf.reshape(conv2, [-1, 7*7*64])
    fc = tf.nn.relu(tf.matmul(fc, weights['W_FC'])+biases['B_FC'])

    output = tf.matmul(fc, weights['Output'])+biases['Output']

    return output

def next_batch(num, data, labels):
    idx = np.arange(0 , len(data))
    np.random.shuffle(idx)
    idx = idx[:num]
    data_shuffle = [data[ i] for i in idx]
    labels_shuffle = [labels[ i] for i in idx]

    return np.asarray(data_shuffle), np.asarray(labels_shuffle)

def train_neural_network(x):
    prediction = neural_network_model(x)

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

    hm_epochs = 10
    with tf.Session() as sess:
        # OLD:
        #sess.run(tf.initialize_all_variables())
        # NEW:
        sess.run(tf.global_variables_initializer())

        for epoch in range(hm_epochs):
            epoch_loss = 0
            for _ in range(int(training_examples/batch_size)):
                epoch_x, epoch_y = next_batch(batch_size, images, labels)
                _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
                epoch_loss += c

            print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)

        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))

        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Accuracy:',accuracy.eval({x: images, y: labels}))

print('Training Neural Network...')
train_neural_network(x)

我做错了什么?需要修复什么以及如何修复numpy数组的形状?

1 个答案:

答案 0 :(得分:4)

如果你仔细观察,你会发现你有两个 x占位符:

x = tf.placeholder('float', [None, 784])  # global

...

x = tf.reshape(x, shape=[-1,28,28,1])     # in neural_network_model

其中一个在函数范围内,因此在train_neural_network中不可见,因此tensorflow采用[?, 784]形状的那个。你应该摆脱其中一个。

另请注意,您的培训数据的排名为3,即[batch_size, 28, 28],因此它与这些占位符的任何不直接兼容。

要将其提供给第一个x,请执行epoch_x.reshape([-1, 784])。对于第二个占位符(一旦您将其显示),请执行epoch_x.reshape([-1, 28, 28, 1])