본 정리의 주된 자료는 CS231n CNN 강의자료이며 핸즈온 머신러닝을 참고했다. http://aikorea.org/cs231n/convolutional-networks/ (TeamAI가 번역한 CS231n 강의) CNN 개요와 구성요소 소개 CNN 주요단어 개념…
지금까지 우리는 훈련속도를 높히고 좋은 해를 찾으려고 하는 방법으로 네 가지를 알아보았다. 1. 좋은 초깃값을 적용하는 전략 ( He, Xavier 등) 2. 좋은 activation function…
이전포스팅 내용 : Reusing Pretrained Layers 아주 큰 DNN을 처음부터 학습한다면 좋지 않은 방법이다. 이미 그런 비슷한 부류의 neural network가 존재하는지 보고 존재한다면 하위 층을…
Early Stopping 방법의 구현 : 구현 코드 : ( 모르는 함수와 부분에 대해 하나씩 따져본다.)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
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) |
result : training test accuracy …
개요 : 우리가 사는 세상에서 해결해야 하는 문제는 숫자 맞추기 문제가 아니다. 고해상도의 이미지, 영상에서 수 종의 물체를 감지해야 하는 문제들이다. 이런 문제를 해결하려면 아주…
인공신경망 : 개념적인 설명은 Deep Learning from Scratch 참고 코드구현 중심 고수준API로 MNIST 훈련:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
(X_train, y_train), (X_test, y_test) = mnist.load_mnist() 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) X_valid, X_train = X_train[:5000], X_train[5000:] y_valid, y_train = y_train[:5000], y_train[5000:] feature_cols = tf.contrib.learn.infer_real_valued_columns_from_input(X_train) dnn_clf = tf.contrib.learn.DNNClassifier(hidden_units=[300,100], n_classes=10, feature_columns=feature_cols) dnn_clf = tf.contrib.learn.SKCompat(dnn_clf) dnn_clf.fit(X_train,y_train, batch_size=50, steps=40000) from sklearn.metrics import accuracy_score y_pred = dnn_clf.predict(X_test) print(accuracy_score(y_test,y_pred['classes'])) |
한 5분 걸림.. 오래걸림 …
https://datascienceschool.net/view-notebook/5cbab09d777841f591a67928d7043f51/ Tensorflow를 잘 소개한 링크 소개 : 머신러닝을 다루는 데에 아주 전문적이고 강력한 오픈소스 소프트웨어 라이브러리로 대규모 데이터를 처리하는 데에 아주 좋다. python 위에서 돌아가며…
개요 : 흔한 빅데이터를 다루는 머신러닝 문제는 매우 많은 특성을 가지고 있어 훈련이 느릴 뿐아니라 좋은 솔루션을 찾기 힘들다. 하지만 불필요한 특성이 무엇인지 알아낸다면 그…
들어가기 앞서 위키피디아 처럼 전문가의 답보다 수 만명의 사람들이 의견을 모아 내놓은 답이 더 낫다. 이를 대중의 지혜라고 한다. 이와 비슷하게 일련의 예측기로부터 예측을 수집하면…