import numpy as np
import tensorflow as tf
import mnist
import os
import time
def reset_graph(seed=42):
tf.reset_default_graph()
tf.set_random_seed(seed)
np.random.seed(seed)
#data setting -----------------------------------
(X_train, y_train), (X_test, y_test) = mnist.load_mnist(normalize=True)
y_train = y_train.astype(np.int32)
y_test = y_test.astype(np.int32)
X_valid, X_train = X_train[:5000], X_train[5000:]
y_valid, y_train = y_train[:5000], y_train[5000:]
#------------------------------------------------
#층깊이
n_inputs = 28 * 28
n_hidden1 = 300
n_hidden2 = 100
n_outputs = 10
reset_graph()
X = tf.placeholder(tf.float32, shape=(None,n_inputs), name="X")
y = tf.placeholder(tf.int32, shape=(None), name="y")
#tf.layers.dense를 이용해 신경망 층구현
with tf.name_scope("dnn"):
he_init = tf.contrib.layers.variance_scaling_initializer()
hidden1 = tf.layers.dense(X,n_hidden1, activation=tf.nn.relu,
kernel_initializer=he_init, name="hidden1")
hidden2 = tf.layers.dense(hidden1, n_hidden2, activation=tf.nn.relu,
kernel_initializer=he_init, name="hidden2")
logits = tf.layers.dense(hidden2,n_outputs, name="outputs")
y_proba = tf.nn.softmax(logits)
#loss 값 구하는 연산
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)
#훈련 연산
learning_rate = 0.01
with tf.name_scope("train"):
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)
#평가 연산
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)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
m,n = X_train.shape # m = 55000 , n = 784
#mini_batch하려고
def shuffle_batch(X, y, batch_size):
rnd_idx = np.random.permutation(len(X)) #예를들어 55000개
n_batches = len(X) // batch_size #len(X)는 X의 행데이터 1100
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
n_epochs = 10001
batch_size = 50
n_batces = int(np.ceil(m/batch_size))
checkpoint_path = "C:\\Users\\고영민\\workspace\\parametersave\\my_deep_mnist_model.ckpt"
checkpoint_epoch_path = checkpoint_path + ".epoch"
final_model_path = "C:\\Users\\고영민\\workspace\\parametersave\\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): #os.path.isfile : True,False
# if the checkpoint file exists, restore the model and load the epoch number
with open(checkpoint_epoch_path, "rb") as f: #이진파일 읽기
start_epoch = int(f.read())
print("Training was interrupted. Continuing at epoch", start_epoch)
saver.restore(sess, checkpoint_path)
else:
start_epoch = 0
sess.run(init)
start_time = time.time()
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 = sess.run([accuracy, loss], feed_dict={X:X_valid, y:y_valid})
if epoch % 5 == 0:
print("Epoch: ",epoch,
"\tValidation accuracy: {:.3f}%".format(accuracy_val * 100),
"\tLoss : {:.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("Early Stopping")
break
end_time = time.time()
during_time = end_time - start_time
print("걸린시간 : ",round(during_time,2))
"""
os.remove(checkpoint_epoch_path)
with tf.Session() as sess:
saver.restore(sess, final_model_path)
accuracy_val = accuracy.eval( feed_dict={X:X_test, y: y_test})
print(accuracy_val)