첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

■ 컨볼루션 신경망 만들기 (MNIST)

------------------------------------------------------------------------------------------------------------------------

import numpy as np

import tensorflow as tf

import tensorflow.examples.tutorials.mnist as mnist

 

batchSize            = 128

testSize             = 256

imageSize            = 28

outputLayerNodeCount = 10

 

convolutionDropoutRate    = 0.8

fullyConnectedDropoutRate = 0.5

 

mnistDatasets = mnist.input_data.read_data_sets("data", one_hot = True)

 

trainInputNDArray         = mnistDatasets.train.images

trainCorrectOutputNDArray = mnistDatasets.train.labels

testInputNDArray          = mnistDatasets.test.images

testCorrectOutputNDArray  = mnistDatasets.test.labels

 

trainInputNDArray = trainInputNDArray.reshape(-1, imageSize, imageSize, 1)

testInputNDArray  = testInputNDArray.reshape (-1, imageSize, imageSize, 1)

 

inputLayerTensor    = tf.placeholder("float", [None, imageSize, imageSize, 1])

correctOutputTensor = tf.placeholder("float", [None, outputLayerNodeCount   ])

 

convolutionLayer1WeightVariable   = tf.Variable(tf.random_normal([3, 3, 1 , 32             ], stddev = 0.01))

convolutionLayer2WeightVariable   = tf.Variable(tf.random_normal([3, 3, 32, 64             ], stddev = 0.01))

convolutionLayer3WeightVariable   = tf.Variable(tf.random_normal([3, 3, 64, 128            ], stddev = 0.01))

fullyConnectedLayerWeightVariable = tf.Variable(tf.random_normal([128 * 4 * 4, 625         ], stddev = 0.01))

outputLayerWeightVariable         = tf.Variable(tf.random_normal([625, outputLayerNodeCount], stddev = 0.01))

 

convolutionDropoutRateTensor    = tf.placeholder("float")

fullyConnectedDropoutRateTensor = tf.placeholder("float")

 

convolutionLayer1OutputTensor          = tf.nn.conv2d(inputLayerTensor, convolutionLayer1WeightVariable, strides = [1, 1, 1, 1], padding = "SAME")

convolutionLayer1OutputTensorReLU      = tf.nn.relu(convolutionLayer1OutputTensor)

convolutionLayer1OutputTensorMaxPool   = tf.nn.max_pool(convolutionLayer1OutputTensorReLU, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = "SAME")

convolutionLayer1OutputTensorDropout   = tf.nn.dropout(convolutionLayer1OutputTensorMaxPool, convolutionDropoutRateTensor)

convolutionLayer2OutputTensor          = tf.nn.conv2d(convolutionLayer1OutputTensorDropout, convolutionLayer2WeightVariable, strides = [1, 1, 1, 1], padding = "SAME")

convolutionLayer2OutputTensorReLU      = tf.nn.relu(convolutionLayer2OutputTensor)

convolutionLayer2OutputTensorMaxPool   = tf.nn.max_pool(convolutionLayer2OutputTensorReLU, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = "SAME")

convolutionLayer2OutputTensorDropout   = tf.nn.dropout(convolutionLayer2OutputTensorMaxPool, convolutionDropoutRateTensor)

convolutionLayer3OutputTensor          = tf.nn.conv2d(convolutionLayer2OutputTensorDropout, convolutionLayer3WeightVariable, strides = [1, 1, 1, 1], padding = "SAME")

convolutionLayer3OutputTensorReLU      = tf.nn.relu(convolutionLayer3OutputTensor)

convolutionLayer3OutputTensorMaxPool   = tf.nn.max_pool(convolutionLayer3OutputTensorReLU, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = "SAME")

convolutionLayer3OutputTensorReshape   = tf.reshape(convolutionLayer3OutputTensorMaxPool, [-1, fullyConnectedLayerWeightVariable.get_shape().as_list()[0]])

convolutionLayer3OutputTensorDropout   = tf.nn.dropout(convolutionLayer3OutputTensorReshape, convolutionDropoutRateTensor)

fullyConnectedLayerOutputTensor        = tf.matmul(convolutionLayer3OutputTensorDropout, fullyConnectedLayerWeightVariable)

fullyConnectedLayerOutputTensorReLU    = tf.nn.relu(fullyConnectedLayerOutputTensor)

fullyConnectedLayerOutputTensorDropout = tf.nn.dropout(fullyConnectedLayerOutputTensorReLU, fullyConnectedDropoutRateTensor)

outputLayerOutputTensor                = tf.matmul(fullyConnectedLayerOutputTensorDropout, outputLayerWeightVariable)

 

costTensor = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = outputLayerOutputTensor, labels = correctOutputTensor))

 

optimizerOperation = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(costTensor)

 

predictTensor = tf.argmax(outputLayerOutputTensor, 1)

 

with tf.Session() as session:

    session.run(tf.global_variables_initializer())

    for i in range(10):

        trainingBatch = zip(range(0, len(trainInputNDArray), batchSize), range(batchSize, len(trainInputNDArray) + 1, batchSize))

        for startIndex, endIndex in trainingBatch:

            session.run(optimizerOperation, feed_dict = {inputLayerTensor : trainInputNDArray[startIndex:endIndex],\

                correctOutputTensor: trainCorrectOutputNDArray[startIndex:endIndex], convolutionDropoutRateTensor : convolutionDropoutRate,\

                fullyConnectedDropoutRateTensor : fullyConnectedDropoutRate})

        testIndexNDArray = np.arange(len(testInputNDArray))

        np.random.shuffle(testIndexNDArray)

        testIndexNDArray = testIndexNDArray[0:testSize]

        print("Epoch : ", i + 1, "정확도 : ", np.mean(np.argmax(testCorrectOutputNDArray[testIndexNDArray], axis = 1) == session.run(predictTensor,\

            feed_dict = {inputLayerTensor : testInputNDArray[testIndexNDArray], correctOutputTensor : testCorrectOutputNDArray[testIndexNDArray],\

            convolutionDropoutRateTensor : 1.0, fullyConnectedDropoutRateTensor : 1.0})))

    scoreTensor    = tf.equal(tf.argmax(outputLayerOutputTensor, 1), tf.argmax(correctOutputTensor, 1))

    accuracyTensor = tf.reduce_mean(tf.cast(scoreTensor, tf.float32))

    print("정확도 : ", session.run(accuracyTensor, feed_dict = {inputLayerTensor : testInputNDArray, correctOutputTensor : testCorrectOutputNDArray,\

        convolutionDropoutRateTensor : 1.0, fullyConnectedDropoutRateTensor : 1.0}))

------------------------------------------------------------------------------------------------------------------------

728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요