Tensorflow: Tensorflow - ValueError: ๋ชจ์–‘ ๊ฐ’์„ ์ œ๊ณตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2016๋…„ 11์›” 28์ผ  ยท  1๋…ผํ‰  ยท  ์ถœ์ฒ˜: tensorflow/tensorflow

12๊ฐœ์˜ ์ž…๋ ฅ ์ •์ˆ˜ ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๋ฐ ๋ ˆ์ด๋ธ”์€ 1 ๋˜๋Š” 0์ž…๋‹ˆ๋‹ค. tensorflow ์›น ์‚ฌ์ดํŠธ์—์„œ MNIST ์˜ˆ์ œ๋ฅผ ๊ฒ€ํ† ํ•ฉ๋‹ˆ๋‹ค.
๋‚ด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

import tensorflow as tf
import numpy as np
def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))
def model(X, w):
    return tf.matmul(X, w) # notice we use the same model as linear regression, this is because there is a baked in cost function which performs softmax and cross entropy
train_data1  = "./data/xx.csv"
test_data1 = "./data/xxx.csv"
train_label1 = "./data/xxl.csv"
test_label1 = "./data/xx.csv"
train_data = np.genfromtxt(train_data1, delimiter=',')
train_label = np.genfromtxt(train_label1, delimiter=',').astype(int)
test_data = np.genfromtxt(test_data1, delimiter=',')
test_label = np.genfromtxt(test_label1, delimiter=',').astype(int)
# Load datasets.
trX, trY, teX, teY = train_data,train_label, test_data, test_label
X = tf.placeholder("float", [None, 12]) # create symbolic variables
Y = tf.placeholder("float", [None, 2])

w = init_weights([12, 2]) 
py_x = model(X, w)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y)) # compute mean cross entropy (softmax is applied internally)
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct optimizer
predict_op = tf.argmax(py_x, 1) # at predict time, evaluate the argmax of the logistic regression

# Launch the graph in a session
with tf.Session() as sess:
    # you need to initialize all variables
    tf.initialize_all_variables().run()

    for i in range(100):
         #for (x, y) in zip(train_X, train_Y):
             #sess.run(optimizer, feed_dict={X: trX, Y: trY})
        for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
        print(i, np.mean(np.argmax(teY, axis=1) ==
                         sess.run(predict_op, feed_dict={X: teX})))

ํ•˜์ง€๋งŒ ์ƒ์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

Traceback (most recent call last):
  File "LRexample.py", line 74, in <module>
    sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
ValueError: Cannot feed value of shape (128,) for Tensor u'Placeholder_1:0', which has shape '(?, 2)

์ด ์˜ค๋ฅ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

community support

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.
๋จผ์ € ๋กœ๋“œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

train_data = np.genfromtxt(train_data1, delimiter=',')
train_label = np.transpose(train_label1, delimiter=',')
test_data = np.genfromtxt(test_data1, delimiter=',')
test_label = np.transpose(test_label1, delimiter=',')

๊ทธ๋Ÿฐ ๋‹ค์Œ trX, trY, teX, teY ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

# convert the data
trX, trY, teX, teY = train_data,train_label, test_data, test_label
temp = trY.shape
trY = trY.reshape(temp[0], 1)
trY = np.concatenate((1-trY, trY), axis=1)
temp = teY.shape
teY = teY.reshape(temp[0], 1)
teY = np.concatenate((1-teY, teY), axis=1)

๋งˆ์ง€๋ง‰์œผ๋กœ ์„ธ์…˜์—์„œ ๊ทธ๋ž˜ํ”„ ์‹œ์ž‘์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

with tf.Session() as sess:
    # you need to initialize all variables
    tf.initialize_all_variables().run()

    for i in range(100):
            sess.run(train_op, feed_dict={X:  trX, Y: trY})        
            print(i, np.mean(np.argmax(teY, axis=1) == sess.run(predict_op, feed_dict={X: teX})))

๊ทธ๊ฒŒ ๋‹ค์•ผ.

>๋ชจ๋“  ๋Œ“๊ธ€

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.
๋จผ์ € ๋กœ๋“œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

train_data = np.genfromtxt(train_data1, delimiter=',')
train_label = np.transpose(train_label1, delimiter=',')
test_data = np.genfromtxt(test_data1, delimiter=',')
test_label = np.transpose(test_label1, delimiter=',')

๊ทธ๋Ÿฐ ๋‹ค์Œ trX, trY, teX, teY ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

# convert the data
trX, trY, teX, teY = train_data,train_label, test_data, test_label
temp = trY.shape
trY = trY.reshape(temp[0], 1)
trY = np.concatenate((1-trY, trY), axis=1)
temp = teY.shape
teY = teY.reshape(temp[0], 1)
teY = np.concatenate((1-teY, teY), axis=1)

๋งˆ์ง€๋ง‰์œผ๋กœ ์„ธ์…˜์—์„œ ๊ทธ๋ž˜ํ”„ ์‹œ์ž‘์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

with tf.Session() as sess:
    # you need to initialize all variables
    tf.initialize_all_variables().run()

    for i in range(100):
            sess.run(train_op, feed_dict={X:  trX, Y: trY})        
            print(i, np.mean(np.argmax(teY, axis=1) == sess.run(predict_op, feed_dict={X: teX})))

๊ทธ๊ฒŒ ๋‹ค์•ผ.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰