神经网络-ValueError:无法输入形状的值

时间:2018-06-30 21:21:16

标签: python tensorflow neural-network

我是Python和Tensorflow的新手。首先,我看了MNIST教程并了解了它。

但是现在我必须用数值​​input_datas创建一个新的神经网络。 我有一个提供input_data和v_data的数据集。

如果我运行input_data.shape->(1000,25,4)

如果我运行v_data.shape->(1000,2)

我试图做的是将数据拆分为(培训+验证)和测试。

  • 培训+验证= train_data的90%(input.pkl的90%)

  • 测试数据=剩余的10%

然后我将输入数据的90%划分为训练和验证(70%训练,30%验证)

网络应基于v_data正确预测,但仍然出现错误。请参见下面的代码和错误。

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# Imports
import tensorflow as tf
import pickle as pkl
import numpy as np

# load data
with open('input.pkl', 'rb') as f:
    input_data = pkl.load(f)
    f.close()

X_train, y_train = input_data


#split data into train / validation and test
X_input = X_train[0:900]
y_input = y_train[0:900]

#print (X_input.shape)
#print (y_input.shape)

X_train_data = X_input[0:630]
X_test_data = X_input[630:900]

y_train_data = y_input[0:630]
y_test_data = y_input[630:900]

# Variables
hidden_layer_1_nodes = 300
hidden_layer_2_nodes = 100
output_layer_nodes = 100
epochs = 10
classes = 2
epoch_errors = []
stddev = 0.035
learning_rate = 0.08
batch_size = 100

#print (X_train_data[0])

# TF Placeholders
X = tf.placeholder('float', [25, 4], name='X')
y = tf.placeholder('float', name='y')

# Weights Matrices
W1 = tf.Variable(tf.truncated_normal([4, hidden_layer_1_nodes], stddev=stddev), name='W1')
W2 = tf.Variable(tf.truncated_normal([hidden_layer_1_nodes, hidden_layer_2_nodes], stddev=stddev), name='W2')
W3 = tf.Variable(tf.truncated_normal([hidden_layer_2_nodes, output_layer_nodes], stddev=stddev), name='W3')
W4 = tf.Variable(tf.truncated_normal([output_layer_nodes, classes], stddev=stddev), name='W4')

# Biases Vectors
b1 = tf.Variable(tf.truncated_normal([hidden_layer_1_nodes], stddev=stddev), name='b1')
b2 = tf.Variable(tf.truncated_normal([hidden_layer_2_nodes], stddev=stddev), name='b2')
b3 = tf.Variable(tf.truncated_normal([output_layer_nodes], stddev=stddev), name='b3')
b4 = tf.Variable(tf.truncated_normal([classes], stddev=stddev), name='b4')

# Define the Neural Network
def nn_model(X):
    input_layer     =    {'weights': W1, 'biases': b1}
    hidden_layer_1  =    {'weights': W2, 'biases': b2}
    hidden_layer_2  =    {'weights': W3, 'biases': b3}
    output_layer    =    {'weights': W4, 'biases': b4}

    input_layer_sum = tf.add(tf.matmul(X, input_layer['weights']), input_layer['biases'])
    input_layer_sum = tf.nn.relu(input_layer_sum)

    hidden_layer_1_sum = tf.add(tf.matmul(input_layer_sum, hidden_layer_1['weights']), hidden_layer_1['biases'])
    hidden_layer_1_sum = tf.nn.relu(hidden_layer_1_sum)

    hidden_layer_2_sum = tf.add(tf.matmul(hidden_layer_1_sum, hidden_layer_2['weights']), hidden_layer_2['biases'])
    hidden_layer_2_sum = tf.nn.relu(hidden_layer_2_sum)

    output_layer_sum = tf.add(tf.matmul(hidden_layer_2_sum, output_layer['weights']), output_layer['biases'])
    return output_layer_sum

# Train the Neural Network
def nn_train(X):
    pred = nn_model(X)
    pred = tf.identity(pred)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
    optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate).minimize(cost)

    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        #saver = tf.train.Saver()
        sess.run(init_op)

        for epoch in range(epochs):
            epoch_loss = 0.0

            i = 0
            while i < len(X_train_data):
                start = i
                end = i+batch_size

                batch_x = np.array(X_train_data[start:end])
                batch_y = np.array(y_train_data[start:end])

                _, c = sess.run([optimizer, cost], feed_dict={X: batch_x, y: batch_y})
                epoch_loss += c
                i+= batch_size

            epoch_errors.append(epoch_loss)
            print('Epoch ', epoch + 1, ' of ', epochs, ' with loss: ', epoch_loss)

        correct_result = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_result, 'float'))
        print('Acc: ', accuracy.eval({X:X_test_data, y:y_test_data}))

if __name__ == "__main__":
    nn_train(X)

以下错误

  

ValueError:无法为形状为((25,4)'的张量'X:0'输入形状(100,25,4)的值

出现在第105行

  

_,c = sess.run([优化器,成本],feed_dict = {X:batch_x,y:batch_y})

2 个答案:

答案 0 :(得分:0)

您为输入X定义的占位符的形状为(25,4)

tf.placeholder('float', [25, 4], name='X')

但是您提供的输入的形状为(100,25,4),其中100是您的批量大小。 将定义更改为

tf.placeholder('float', [None, 25, 4], name='X') 

,错误应该消失了。在这里,None自动处理批量大小。

更新:抱歉,我没有阅读完整的代码。您的代码需要一些修复。 将数据提供给占位符的正确语法是:

X = tf.placeholder(tf.float32, [None, input_dim], name='X')

现在,如果要处理图像,则您的input_dim将是一个示例的展平数组的长度,即,如果图像的尺寸为25x4,则input_dim应为25 * 4 = 100。在此处W1,它应等于权重层1的第一尺寸。 另外,在喂食批次之前,您需要重塑形状。

下面是固定代码(注释已更改):

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# Imports
import tensorflow as tf
import pickle as pkl
import numpy as np

# load data
with open('input.pkl', 'rb') as f:
    input_data = pkl.load(f)
    f.close()

X_train, y_train = input_data


#split data into train / validation and test
X_input = X_train[0:900]
y_input = y_train[0:900]

#print (X_input.shape)
#print (y_input.shape)

X_train_data = X_input[0:630]
X_test_data = X_input[630:900]

y_train_data = y_input[0:630]
y_test_data = y_input[630:900]

# Variables
hidden_layer_1_nodes = 300
hidden_layer_2_nodes = 100
output_layer_nodes = 100
epochs = 10
classes = 2
epoch_errors = []
stddev = 0.035
learning_rate = 0.08
batch_size = 100

#print (X_train_data[0])

# TF Placeholders
# input data should be of the shape (batch_size, flatten data for one example). Also, the correct shape of y"
X = tf.placeholder(tf.float32, [None, 25 * 4], name='X')
y = tf.placeholder(tf.float32, [None, classes] name='y')

# Weights Matrices. First dimension of W1 == second dimension of X
W1 = tf.Variable(tf.truncated_normal([25 * 4, hidden_layer_1_nodes], stddev=stddev), name='W1')
W2 = tf.Variable(tf.truncated_normal([hidden_layer_1_nodes, hidden_layer_2_nodes], stddev=stddev), name='W2')
W3 = tf.Variable(tf.truncated_normal([hidden_layer_2_nodes, output_layer_nodes], stddev=stddev), name='W3')
W4 = tf.Variable(tf.truncated_normal([output_layer_nodes, classes], stddev=stddev), name='W4')

# Biases Vectors
b1 = tf.Variable(tf.truncated_normal([hidden_layer_1_nodes], stddev=stddev), name='b1')
b2 = tf.Variable(tf.truncated_normal([hidden_layer_2_nodes], stddev=stddev), name='b2')
b3 = tf.Variable(tf.truncated_normal([output_layer_nodes], stddev=stddev), name='b3')
b4 = tf.Variable(tf.truncated_normal([classes], stddev=stddev), name='b4')

# Define the Neural Network
def nn_model(X):
    input_layer     =    {'weights': W1, 'biases': b1}
    hidden_layer_1  =    {'weights': W2, 'biases': b2}
    hidden_layer_2  =    {'weights': W3, 'biases': b3}
    output_layer    =    {'weights': W4, 'biases': b4}

    input_layer_sum = tf.add(tf.matmul(X, input_layer['weights']), input_layer['biases'])
    input_layer_sum = tf.nn.relu(input_layer_sum)

    hidden_layer_1_sum = tf.add(tf.matmul(input_layer_sum, hidden_layer_1['weights']), hidden_layer_1['biases'])
    hidden_layer_1_sum = tf.nn.relu(hidden_layer_1_sum)

    hidden_layer_2_sum = tf.add(tf.matmul(hidden_layer_1_sum, hidden_layer_2['weights']), hidden_layer_2['biases'])
    hidden_layer_2_sum = tf.nn.relu(hidden_layer_2_sum)

    output_layer_sum = tf.add(tf.matmul(hidden_layer_2_sum, output_layer['weights']), output_layer['biases'])
    return output_layer_sum

# Train the Neural Network
def nn_train(X):
    pred = nn_model(X)
    pred = tf.identity(pred)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
    optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate).minimize(cost)

    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        #saver = tf.train.Saver()
        sess.run(init_op)

        for epoch in range(epochs):
            epoch_loss = 0.0

            i = 0
            while i < len(X_train_data):
                start = i
                end = i+batch_size
                # reshape before feeding.
                batch_x = np.array(X_train_data[start:end]).reshape(batch_size, 25 * 4)
                batch_y = np.array(y_train_data[start:end]).reshape(batch_size, classes)

                _, c = sess.run([optimizer, cost], feed_dict={X: batch_x, y: batch_y})
                epoch_loss += c
                i+= batch_size

            epoch_errors.append(epoch_loss)
            print('Epoch ', epoch + 1, ' of ', epochs, ' with loss: ', epoch_loss)

        correct_result = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_result, 'float'))
        print('Acc: ', accuracy.eval({X:X_test_data.reshape(-1, 25 * 4), y:y_test_data.reshape(-1, classes)}))

if __name__ == "__main__":
    nn_train(X)

答案 1 :(得分:0)

更新:对不起,我发布了错误的错误: @Kumar,我将批次大小更改为30(所以21 * 30 = 630)。现在可以打印时代,但是以某种奇怪的方式:

objdump -d --no-show-raw-insn my_binary_file.o | grep "^ " | cut -f2,3

我收到以下错误消息:

Epoch  1  of  10  with loss:  1680690.2648780346
Epoch  2  of  10  with loss:  2382142.9208984375
Epoch  3  of  10  with loss:  4215628.857421875
Epoch  4  of  10  with loss:  9046892.295166016
Epoch  5  of  10  with loss:  23961644.453125
Epoch  6  of  10  with loss:  31733882.34375
Epoch  7  of  10  with loss:  46124696.609375
Epoch  8  of  10  with loss:  61760446.28125
Epoch  9  of  10  with loss:  89145610.59375
Epoch  10  of  10  with loss:  121249417.25
  

ValueError:无法为张量为'(?,100)'的张量'X:0'输入形状(270、25、4)的值

以退出代码1完成的过程