■ 4계층 ReLU Dropout 1계층 Softmax로 구성된 다층 퍼셉트론 신경망을 만드는 방법을 보여준다.
▶ 예제 코드 (PY)
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 |
import math import tensorflow as tf import tensorflow.examples.tutorials.mnist as mnist inputLayerNodeCount = 784 hiddenLayer1NodeCount = 200 hiddenLayer2NodeCount = 100 hiddenLayer3NodeCount = 60 hiddenLayer4NodeCount = 30 outputLayerNodeCount = 10 summaryLogDirectoryPath = "log_mnist_4_layer_relu_dropout_1_layer_softmax" batchSize = 100 epochCount = 10 minimumLearningRate = 0.0001 maximumLearningRate = 0.003 decaySpeed = 2000 dropoutRate = 0.75 mnistDatasets = mnist.input_data.read_data_sets("data", one_hot = True) inputLayerTensor = tf.placeholder(tf.float32, [None, inputLayerNodeCount]) learningRateTensor = tf.placeholder(tf.float32) dropoutRateTensor = tf.placeholder(tf.float32) hiddenLayer1WeightVariable = tf.Variable(tf.truncated_normal([inputLayerNodeCount , hiddenLayer1NodeCount], stddev = 0.1)) hiddenLayer1BiasVariable = tf.Variable(tf.ones([hiddenLayer1NodeCount]) / 10) hiddenLayer2WeightVariable = tf.Variable(tf.truncated_normal([hiddenLayer1NodeCount, hiddenLayer2NodeCount], stddev = 0.1)) hiddenLayer2BiasVariable = tf.Variable(tf.ones([hiddenLayer2NodeCount]) / 10) hiddenLayer3WeightVariable = tf.Variable(tf.truncated_normal([hiddenLayer2NodeCount, hiddenLayer3NodeCount], stddev = 0.1)) hiddenLayer3BiasVariable = tf.Variable(tf.ones([hiddenLayer3NodeCount]) / 10) hiddenLayer4WeightVariable = tf.Variable(tf.truncated_normal([hiddenLayer3NodeCount, hiddenLayer4NodeCount], stddev = 0.1)) hiddenLayer4BiasVariable = tf.Variable(tf.ones([hiddenLayer4NodeCount]) / 10) outputLayerWeightVariable = tf.Variable(tf.truncated_normal([hiddenLayer4NodeCount, outputLayerNodeCount ], stddev = 0.1)) outputLayerBiasVariable = tf.Variable(tf.zeros([outputLayerNodeCount])) hiddenLayer1OutputTensor = tf.nn.relu(tf.matmul(inputLayerTensor , hiddenLayer1WeightVariable) + hiddenLayer1BiasVariable) hiddenLayer1OutputTensorDropout = tf.nn.dropout(hiddenLayer1OutputTensor, dropoutRateTensor) hiddenLayer2OutputTensor = tf.nn.relu(tf.matmul(hiddenLayer1OutputTensorDropout, hiddenLayer2WeightVariable) + hiddenLayer2BiasVariable) hiddenLayer2OutputTensorDropout = tf.nn.dropout(hiddenLayer2OutputTensor, dropoutRateTensor) hiddenLayer3OutputTensor = tf.nn.relu(tf.matmul(hiddenLayer2OutputTensorDropout, hiddenLayer3WeightVariable) + hiddenLayer3BiasVariable) hiddenLayer3OutputTensorDropout = tf.nn.dropout(hiddenLayer3OutputTensor, dropoutRateTensor) hiddenLayer4OutputTensor = tf.nn.relu(tf.matmul(hiddenLayer3OutputTensorDropout, hiddenLayer4WeightVariable) + hiddenLayer4BiasVariable) hiddenLayer4OutputTensorDropout = tf.nn.dropout(hiddenLayer4OutputTensor, dropoutRateTensor) outputLayerOutputTensor = tf.matmul(hiddenLayer4OutputTensorDropout, outputLayerWeightVariable ) + outputLayerBiasVariable outputLayerOutputTensorSoftmax = tf.nn.softmax(outputLayerOutputTensor) correctOutputTensor = tf.placeholder(tf.float32, [None, outputLayerNodeCount]) costTensor = tf.nn.softmax_cross_entropy_with_logits(logits = outputLayerOutputTensor, labels = correctOutputTensor) costTensor = tf.reduce_mean(costTensor) * 100 correctPredictionTensor = tf.equal(tf.argmax(outputLayerOutputTensorSoftmax, 1), tf.argmax(correctOutputTensor, 1)) accuracyTensor = tf.reduce_mean(tf.cast(correctPredictionTensor, tf.float32)) optimizerOperation = tf.train.AdamOptimizer(learningRateTensor).minimize(costTensor) tf.summary.scalar("cost" , costTensor ) tf.summary.scalar("accuracy", accuracyTensor) summaryTensor = tf.summary.merge_all() with tf.Session() as session: session.run(tf.global_variables_initializer()) fileWriter = tf.summary.FileWriter(summaryLogDirectoryPath, graph = tf.get_default_graph()) batchCount = int(mnistDatasets.train.num_examples / batchSize) for epoch in range(epochCount): for batch in range(batchCount): batchInputNDArray, batchCorrectOutputNDArray = mnistDatasets.train.next_batch(batchSize) learningRatio = minimumLearningRate + (maximumLearningRate - minimumLearningRate) * math.exp(-batch / decaySpeed) _, summary = session.run([optimizerOperation, summaryTensor], feed_dict = {inputLayerTensor : batchInputNDArray, correctOutputTensor : batchCorrectOutputNDArray,\ dropoutRateTensor : dropoutRate, learningRateTensor : learningRatio}) fileWriter.add_summary(summary, epoch * batchCount + batch) print("Epoch : ", epoch) print("정확도 : ", accuracyTensor.eval(feed_dict = {inputLayerTensor : mnistDatasets.test.images, correctOutputTensor : mnistDatasets.test.labels,\ dropoutRateTensor : dropoutRate})) print("학습을 완료했습니다.") |