import tensorflow as tf

# 데이터 준비
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

x_train = x_train.reshape(-1, 28*28)
x_test = x_test.reshape(-1, 28*28)

y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)

print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

# 모델 만들기
X = tf.placeholder(tf.float32, shape=[None, 784])
Y = tf.placeholder(tf.float32, shape=[None, 10])

# Hidden Layer를 위한 Weight
W1 = tf.Variable(tf.random_normal(shape=[784, 256]))
B1 = tf.Variable(tf.random_normal(shape=[256]))
H1 = tf.matmul(X, W1)+B1
H1 = tf.nn.relu(H1)

# H1의 shape: (None, 256)

W2 = tf.Variable(tf.random_normal(shape=[256, 256]))
B2 = tf.Variable(tf.random_normal(shape=[256]))
H2 = tf.matmul(H1, W2) + B2
H2 = tf.nn.relu(H2)

# H2의 shape: (None, 256)

W3 = tf.Variable(tf.random_normal(shape=[256,128]))
B3 = tf.Variable(tf.random_normal(shape=[128]))
H3 = tf.matmul(H2, W3) + B3
H3 = tf.nn.relu(H3)

# H3의 shape: (None, 128)
W4 = tf.Variable(tf.random_normal(shape=[128, 10]))
B4 = tf.Variable(tf.random_normal(shape=[10]))

logit = tf.matmul(H3, W4) + B4

loss = tf.nn.softmax_cross_entropy_with_logits(logits=logit, labels=Y)
loss = tf.reduce_mean(loss)

optimizer = tf.train.AdamOptimizer(0.01).minimize(loss)

pred = tf.nn.softmax(logit)
acc = tf.equal(tf.argmax(pred, axis=1), tf.argmax(Y, axis=1))
acc = tf.reduce_mean(tf.cast(acc, tf.float32))

# 모델 학습
sess = tf.Session()
sess.run(tf.global_variables_initializer())

epochs = 10
batch  = 512
n_batch = len(x_train) // batch
for e in range(epochs):
  for b in range(n_batch):
    x = x_train[b * batch : (b+1) * batch]
    y = y_train[b * batch : (b+1) * batch]
    sess.run(optimizer, feed_dict={X:x,Y:y})
    
#    print("{0: .2f}%".format(100 * sess.run(acc, feed_dict={X:x, Y:y})))


# 모델 평가
accuracy = sess.run(acc, feed_dict={X: x_test, Y: y_test})
print("평가")
print("{0: .2f}%".format(accuracy * 100))

+ Recent posts