728x90
반응형
728x170
■ 컨볼루션 신경망을 만드는 방법을 보여준다.
▶ 예제 코드 (PY)
import tensorflow as tf
import tensorflow.examples.tutorials.mnist as mnist
imageSize = 28
batchSize = 100
outputLayerNodeCount = 10
learningRate = 0.001
epochCount = 10
dropoutRate = 0.8
mnistDatasets = mnist.input_data.read_data_sets("data", one_hot = True)
inputLayerTensor = tf.placeholder(tf.float32, [None, imageSize, imageSize, 1])
correctOutputTensor = tf.placeholder(tf.float32, [None, outputLayerNodeCount])
dropoutRateTensor = tf.placeholder(tf.float32)
convolutionLayer1WeightVariable = tf.Variable(tf.random_normal([4, 4, 1, 16 ], stddev = 0.01))
convolutionLayer2WeightVariable = tf.Variable(tf.random_normal([4, 4, 16, 32 ], stddev = 0.01))
fullyConnectedLayerWeightVariable = tf.Variable(tf.random_normal([7 * 7 * 32, 256 ], stddev = 0.01))
outputLayerWeightVariable = tf.Variable(tf.random_normal([256, outputLayerNodeCount], stddev = 0.01))
convolutionLayer1OutputTensor = tf.nn.conv2d(inputLayerTensor, convolutionLayer1WeightVariable, strides = [1, 1, 1, 1], padding = "SAME")
convolutionLayer1OutputTensor = tf.nn.relu(convolutionLayer1OutputTensor)
convolutionLayer1OutputTensor = tf.nn.max_pool(convolutionLayer1OutputTensor, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = "SAME")
convolutionLayer2OutputTensor = tf.nn.conv2d(convolutionLayer1OutputTensor, convolutionLayer2WeightVariable, strides = [1, 1, 1, 1], padding = "SAME")
convolutionLayer2OutputTensor = tf.nn.relu(convolutionLayer2OutputTensor)
convolutionLayer2OutputTensor = tf.nn.max_pool(convolutionLayer2OutputTensor, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = "SAME")
fullyConnectedLayerOutputTensor = tf.reshape(convolutionLayer2OutputTensor, [-1, 7 * 7 * 32])
fullyConnectedLayerOutputTensor = tf.matmul(fullyConnectedLayerOutputTensor, fullyConnectedLayerWeightVariable)
fullyConnectedLayerOutputTensor = tf.nn.relu(fullyConnectedLayerOutputTensor)
fullyConnectedLayerOutputTensor = tf.nn.dropout(fullyConnectedLayerOutputTensor, dropoutRateTensor)
outputLayerOutputTensor = tf.matmul(fullyConnectedLayerOutputTensor, outputLayerWeightVariable)
costTensor = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = outputLayerOutputTensor, labels = correctOutputTensor))
optimizerOperation = tf.train.AdamOptimizer(learningRate).minimize(costTensor)
with tf.Session() as session:
session.run(tf.global_variables_initializer())
totalBatch = int(mnistDatasets.train.num_examples / batchSize)
for epoch in range(epochCount):
totalCost = 0
for batch in range(totalBatch):
batchInputNDArray, batchCorrectOutputNDArray = mnistDatasets.train.next_batch(batchSize)
batchInputNDArray = batchInputNDArray.reshape(-1, imageSize, imageSize, 1)
_, cost = session.run([optimizerOperation, costTensor], feed_dict = {inputLayerTensor : batchInputNDArray, correctOutputTensor : batchCorrectOutputNDArray,\
dropoutRateTensor : dropoutRate})
totalCost += cost
print("반복 : ", "%04d" % (epoch + 1), "평균 비용 : ", "{:.4f}".format(totalCost / totalBatch))
print("학습 완료!")
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 : mnistDatasets.test.images.reshape(-1, imageSize, imageSize, 1),\
correctOutputTensor : mnistDatasets.test.labels, dropoutRateTensor : 1}))
728x90
반응형
그리드형(광고전용)
'Python > tensorflow' 카테고리의 다른 글
[PYTHON/TENSORFLOW] 컨볼루션 오토 인코더 만들기 (0) | 2018.08.10 |
---|---|
[PYTHON/TENSORFLOW] 노이즈 제거 오토 인코더 만들기 (0) | 2018.08.07 |
[PYTHON/TENSORFLOW] 오토 인코더 만들기 (0) | 2018.08.07 |
[PYTHON/TENSORFLOW] 컨볼루션 신경망 만들기 (얼굴 감정 판정) (0) | 2018.08.05 |
[PYTHON/TENSORFLOW] 컨볼루션 신경망 만들기 (MNIST) (0) | 2018.08.04 |
[PYTHON/TENSORFLOW] 다층 퍼셉트론 신경망 만들기 (MNIST) : 4계층 ReLU Dropout 1계층 Softmax (0) | 2018.08.01 |
[PYTHON/TENSORFLOW] 다층 퍼셉트론 신경망 만들기 (MNIST) : 4계층 ReLU 1계층 Softmax (0) | 2018.08.01 |
[PYTHON/TENSORFLOW] 다층 퍼셉트론 신경망 만들기 (MNIST) : 4계층 Sigmoid 1계층 Softmax (0) | 2018.08.01 |
[PYTHON/TENSORFLOW] 신경망 모델 로드하기 (0) | 2018.08.01 |
[PYTHON/TENSORFLOW] 다층 퍼셉트론 신경망 만들기 (MNIST) : 1계층 Softmax (0) | 2018.07.31 |
댓글을 달아 주세요