2017-06-20 12 views
-1

Tensorflowを学習しています。以下はTensorFlowを使用したMLPのコードです。私はデータの次元の不一致にいくつかの問題があります。ValueError:形状が '(?、5000)'のTensor 'Reshape: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

私はときに

feed_dict={x: your_val} 
ようにラインで

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

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

、問題はプレースホルダとリシェイプに同じ変数名xを使用することであると思います

リシェイプ操作の出力を供給しています。

あなたは、インスタンス

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:Shapeはランク2でなければなりませんが、入力形状が[?、50,50,2]、[5000,500]の 'MatMul'(op: 'MatMul')のランク4です。" – popo

+0

あなたは 'x'を' x_reshaped'で置き換えるか、 'x_reshaped'の名前を' x'に変更する必要があります。 –

関連する問題