0
방금 tensorflow , 오늘 제가 읽은 튜토리얼을 구현하고 싶습니다. link. 그리고 실제 코드는 이것입니다. 0-9 사이의 숫자를 분류하기 위해 아주 기본적인 NN을 구현하려고했습니다. 내가 이것을 실행하려고하면Python - TensorFlow/tf ValueError : '(?, 28, 28, 1)'모양의 Tensor '자리 표시 자 : 0'에 대한 모양 값 (100, 784)을 입력 할 수 없음
import tensorflow as tf
# load dataset
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)
X = tf.placeholder(tf.float32, [None, 28, 28, 1])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
init = tf.initialize_all_variables()
# model
Y = tf.nn.softmax(tf.matmul(tf.reshape(X,[-1, 784]), W) + b)
# placeholder fr correct labels
Y_ = tf.placeholder(tf.float32, [None, 10])
# loss function
cross_entropy = -tf.reduce_sum(Y_ * tf.log(Y))
# % of a correct answer found in batch
is_correct = tf.equal(tf.argmax(Y,1), tf.argmax(Y_,1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
# optimizer, workhorse of a NN
optimizer = tf.train.GradientDescentOptimizer(0.003)
train_step = optimizer.minimize(cross_entropy)
# start the session
sess = tf.Session()
sess.run(init)
# feed the data for training
for i in range(10000):
# load batch of images and correct answers
batch_X, batch_Y = mnist.train.next_batch(100)
train_data={X: batch_X, Y_: batch_Y}
# train
sess.run(optimizer, feed_dict=train_data)
# succes ?
a,c = sess.run([accuracy, cross_entropy], feed=train_data)
# success on test data
test_data={X: mnist.test.images, Y_: mnist.test.labels}
a,c = sess.run([accuracy, cross_entropy], feed=test_data)
는하지만
I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties:
name: GeForce 840M
major: 5 minor: 0 memoryClockRate (GHz) 1.124
pciBusID 0000:03:00.0
Total memory: 1.96GiB
Free memory: 1.72GiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0: Y
I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce 840M, pci bus id: 0000:03:00.0)
Traceback (most recent call last):
File "mnist_v1.py", line 41, in <module>
sess.run(optimizer, feed_dict=train_data)
File "/home/gopi34/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/home/gopi34/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 944, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (100, 784) for Tensor 'Placeholder:0', which has shape '(?, 28, 28, 1)'
고마워, 그것은 매력처럼 작동 :) – gopi