728x90
반응형
In [111]:
# MNIST 데이터 불러오기
import tensorflow as tf
(X_train,y_train),(X_test,y_test) = tf.keras.datasets.mnist.load_data()
In [112]:
# preprocessing
import numpy as np
X_train = X_train.astype(np.float32).reshape(-1,28*28) / 255.0
X_test = X_test.astype(np.float32).reshape(-1,28*28) / 255.0
y_train = y_train.astype(np.int32)
y_test = y_test.astype(np.int32)
In [113]:
X_train, X_valid = X_train[5000:], X_train[:5000]
y_train, y_valid = y_train[5000:], y_train[:5000]
In [119]:
tf.reset_default_graph()
n_inputs = 28*28 # MNIST
n_hidden1 = 300
n_hidden2 = 100
n_outputs = 10
In [120]:
X = tf.placeholder(tf.float32, shape=(None,n_inputs),name='X')
y = tf.placeholder(tf.int32, shape=(None),name='y')
In [121]:
with tf.name_scope('dnn'):
hidden1 = tf.layers.dense(X,n_hidden1,activation=tf.nn.relu,name='hidden1')
hidden2 = tf.layers.dense(hidden1,n_hidden2,activation=tf.nn.relu,name='hidden2')
logits = tf.layers.dense(hidden2,n_outputs,name='outputs')
In [122]:
with tf.name_scope('loss'):
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,labels=y)
loss = tf.reduce_mean(xentropy,name='loss')
loss_summary = tf.summary.scalar('log_loss',loss)
In [123]:
lr = 0.01
with tf.name_scope('train'):
optimizer = tf.train.GradientDescentOptimizer(lr)
training_op = optimizer.minimize(loss)
In [124]:
with tf.name_scope('eval'):
correct = tf.nn.in_top_k(logits,y,1)
accuracy = tf.reduce_mean(tf.cast(correct,tf.float32))
accuracy_summary = tf.summary.scalar("accuracy",accuracy)
In [125]:
# 텐서보드
from datetime import datetime
def log_dir(prefix=""):
now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
root_logdir = "tf_logs"
if prefix:
prefix += "-"
name = prefix + "run-" + now
return "{}/{}/".format(root_logdir, name)
In [126]:
logdir = log_dir("mnist_dnn")
In [127]:
file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())
In [128]:
m,n = X_train.shape
In [129]:
def shuffle_batch(X, y, batch_size):
rnd_idx = np.random.permutation(len(X))
n_batches = len(X) // batch_size
for batch_idx in np.array_split(rnd_idx, n_batches):
X_batch, y_batch = X[batch_idx], y[batch_idx]
yield X_batch, y_batch
In [130]:
saver = tf.train.Saver()
In [131]:
import os
n_epochs = 1001
batch_size = 50
total_batch = int(np.ceil(m / batch_size))
checkpoint_path = "/tmp/my_deep_mnist_model.ckpt"
checkpoint_epoch_path = checkpoint_path + ".epoch"
final_model_path = "./my_deep_mnist_model"
best_loss = np.infty
epochs_without_progress = 0
max_epochs_without_progress = 50
with tf.Session() as sess:
if os.path.isfile(checkpoint_epoch_path):
with open(checkpoint_epoch_path,"rb") as f:
start_epoch = int(f.read())
print("이전 훈련이 중지되었습니다. 에포크 {}에서 시작합니다".format(start_epoch))
saver.restore(sess, checkpoint_path)
else:
start_epoch = 0
sess.run(tf.global_variables_initializer())
for epoch in range(start_epoch,n_epochs):
for X_batch, y_batch in shuffle_batch(X_train,y_train,batch_size):
sess.run(training_op,feed_dict={X:X_batch,y:y_batch})
accuracy_val, loss_val, accuracy_summary_str, loss_summary_str = \
sess.run([accuracy,loss,accuracy_summary,loss_summary],feed_dict={X:X_val,y:y_val})
file_writer.add_summary(accuracy_summary_str, epoch)
file_writer.add_summary(loss_summary_str, epoch)
if epoch % 5 == 0:
print("에포크:", epoch,
"\t검증 세트 정확도: {:.3f}%".format(accuracy_val * 100),
"\t손실: {:.5f}".format(loss_val))
saver.save(sess, checkpoint_path)
with open(checkpoint_epoch_path, "wb") as f:
f.write(b"%d" % (epoch + 1))
if loss_val < best_loss:
saver.save(sess, final_model_path)
best_loss = loss_val
else:
epochs_without_progress += 5
if epochs_without_progress > max_epochs_without_progress:
print("조기 종료")
break
In [132]:
os.remove(checkpoint_epoch_path)
In [133]:
with tf.Session() as sess:
saver.restore(sess, final_model_path)
accuracy_val = accuracy.eval(feed_dict={X: X_test, y: y_test})
In [134]:
accuracy_val
Out[134]:
728x90
반응형