2017-06-20 122 views
-1

我正在學習Tensorflow。以下是我與TensorFlow MLP的代碼。我有一些數據維度不匹配的問題。ValueError:無法提供形狀爲'(?,5000)'的張量'重塑:0'的形狀值(3375,50,50,2)'

import numpy as np 
import tensorflow as tf 
import matplotlib.pyplot as plt 
wholedataset = np.load('C:/Users/pourya/Downloads/WholeTrueData.npz') 
data = wholedataset['wholedata'].astype('float32') 
label = wholedataset['wholelabel'].astype('float32') 
height = wholedataset['wholeheight'].astype('float32') 
print(type(data[20,1,1,0])) 

learning_rate = 0.001 
training_iters = 5 
display_step = 20 
n_input = 3375 

X = tf.placeholder("float32") 
Y = tf.placeholder("float32") 
weights = { 
    'wc1': tf.Variable(tf.random_normal([3, 3, 2, 1])), 
    'wd1': tf.Variable(tf.random_normal([3, 3, 1, 1])) 
} 
biases = { 
    'bc1': tf.Variable(tf.random_normal([1])), 
    'out': tf.Variable(tf.random_normal([1,50,50,1])) 
} 
mnist= data 

n_nodes_hl1 = 500 
n_nodes_hl2 = 500 
n_nodes_hl3 = 500 

n_classes = 2 
batch_size = 100 

x = tf.placeholder('float', shape = [None,50,50,2]) 
shape = x.get_shape().as_list() 
dim = np.prod(shape[1:]) 
x_reshaped = tf.reshape(x, [-1, dim]) 

y = tf.placeholder('float', shape= [None,50,50,2]) 
shape = y.get_shape().as_list() 
dim = np.prod(shape[1:]) 
y_reshaped = tf.reshape(y, [-1, dim]) 

def neural_network_model(data): 
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([5000, 
         n_nodes_hl1])), 
         'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))} 

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, 
         n_nodes_hl2])), 
         'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))} 

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, 
         n_nodes_hl3])), 
         'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))} 

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, 
        n_classes])), 
        'biases':tf.Variable(tf.random_normal([n_classes])),} 
    l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), 
     hidden_1_layer['biases']) 
    l1 = tf.nn.relu(l1) 

    l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), 
     hidden_2_layer['biases']) 
    l2 = tf.nn.relu(l2) 

    l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), 
     hidden_3_layer['biases']) 
    l3 = tf.nn.relu(l3) 

    output = tf.matmul(l3,output_layer['weights']) + output_layer['biases'] 

    return output 
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: 
     sess.run(tf.global_variables_initializer()) 

     for epoch in range(hm_epochs): 
      epoch_loss = 0 
      for _ in range(int(n_input/batch_size)): 
       epoch_x = wholedataset['wholedata'].astype('float32') 
       epoch_y = wholedataset['wholedata'].astype('float32') 

       _, 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:mnist.test.images, 
     y:mnist.test.labels})) 

train_neural_network(x) 

我得到了以下錯誤:

ValueError: Cannot feed value of shape (3375, 50, 50, 2) for Tensor 'Reshape:0', which has shape '(?, 5000)' 

有誰知道什麼是我的代碼的問題,如何解決呢? 數據值是(3375,50,50,2)

謝謝各位的意見!

回答

0

我覺得現在的問題是,你使用相同的變量名x的佔位符和重塑,在線路

x = tf.placeholder('float', shape = [None,50,50,2]) 

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

,這樣當你

feed_dict={x: your_val} 

您正在輸入重塑操作的輸出。

你應該有不同的名稱,例如

x_placeholder = tf.placeholder('float', shape = [None,50,50,2]) 
x_reshaped = tf.reshape(x, [-1, dim]) 

然後

feed_dict={x_placeholder: your_val} 
+0

@ Poetro我也做了chenges,但我得到了另一個錯誤。 「ValueError:形狀必須是等級2,但是對於'MatMul'(op:'MatMul'),等級4的輸入形狀爲:[?,50,50,2],[5000,500]。」 – popo

+0

其餘部分你應該用'x_reshaped'替換'x',或者把'x_reshaped'重命名爲'x'。 –

相關問題