728x90
반응형
728x170
■ 다층 퍼셉트론 신경망을 만드는 방법을 보여준다.
▶ mlp.py
import input_data
import matplotlib.pyplot as pp
import tensorflow as tf
numberPixelDatasets = input_data.read_data_sets("MNIST_data/", one_hot = True)
learningRate = 0.001
trainingEpochCount = 20
batchSize = 100
displayStep = 1
inputNodeCount = 784 # 입력 노드 카운트 ; 28×28 픽셀 이미지
hiddenNodeCount1 = 256 # 은닉 노드 카운트 1
hiddenNodeCount2 = 256 # 은닉 노드 카운트 2
outputNodeCount = 10 # 출력 노드 카운트 ; 슷자 0-9
inputValueTensor = tf.placeholder("float", [None, inputNodeCount])
outputValueTensor = tf.placeholder("float", [None, outputNodeCount])
hiddenLayerWeightVariable1 = tf.Variable(tf.random_normal([inputNodeCount, hiddenNodeCount1]))
hiddenLayerBiasVariable1 = tf.Variable(tf.random_normal([hiddenNodeCount1]))
hiddenLayerTensor1 = tf.nn.sigmoid(tf.add(tf.matmul(inputValueTensor, hiddenLayerWeightVariable1), hiddenLayerBiasVariable1))
hiddenLayerWeightVariable2 = tf.Variable(tf.random_normal([hiddenNodeCount1, hiddenNodeCount2]))
hiddenLayerBiasVariable2 = tf.Variable(tf.random_normal([hiddenNodeCount2]))
hiddenLayerTensor2 = tf.nn.sigmoid(tf.add(tf.matmul(hiddenLayerTensor1, hiddenLayerWeightVariable2), hiddenLayerBiasVariable2))
outputLayerWeightVariable = tf.Variable(tf.random_normal([hiddenNodeCount2, outputNodeCount]))
outputLayerBiasVariable = tf.Variable(tf.random_normal([outputNodeCount]))
outputLayerTensor = tf.matmul(hiddenLayerTensor2, outputLayerWeightVariable) + outputLayerBiasVariable
costTensor = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = outputLayerTensor, labels = outputValueTensor))
optimizerOperation = tf.train.AdamOptimizer(learningRate).minimize(costTensor)
averageList = []
epochList = []
initializerOperation = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(initializerOperation)
for epoch in range(trainingEpochCount):
averageCost = 0.
totalBatch = int(numberPixelDatasets.train.num_examples / batchSize)
for i in range(totalBatch):
xBatchNDArray, yBatchNDArray = numberPixelDatasets.train.next_batch(batchSize)
sess.run(optimizerOperation, feed_dict = {inputValueTensor : xBatchNDArray, outputValueTensor : yBatchNDArray})
averageCost += sess.run(costTensor, feed_dict = {inputValueTensor : xBatchNDArray, outputValueTensor : yBatchNDArray}) / totalBatch
if epoch % displayStep == 0:
print("회차 :", '%04d' % (epoch + 1), "비용 =", "{:.9f}".format(averageCost))
averageList.append(averageCost)
epochList.append(epoch + 1)
print("훈련 단계 완료")
correctPredictionTensor = tf.equal(tf.argmax(outputLayerTensor, 1), tf.argmax(outputValueTensor, 1))
accuracyTensor = tf.reduce_mean(tf.cast(correctPredictionTensor, "float"))
print("모델 정확도 :", accuracyTensor.eval({inputValueTensor : numberPixelDatasets.test.images, outputValueTensor : numberPixelDatasets.test.labels}))
pp.plot(epochList, averageList, 'o', label = "MLP Training phase")
pp.ylabel("cost")
pp.xlabel("epoch")
pp.legend()
pp.show()
▶ 결과
회차 : 0001 비용 = 1.835462693
회차 : 0002 비용 = 0.543075322
회차 : 0003 비용 = 0.365367627
회차 : 0004 비용 = 0.269016541
회차 : 0005 비용 = 0.207292393
회차 : 0006 비용 = 0.162509755
회차 : 0007 비용 = 0.128678610
회차 : 0008 비용 = 0.102387188
회차 : 0009 비용 = 0.080819757
회차 : 0010 비용 = 0.064745211
회차 : 0011 비용 = 0.051405598
회차 : 0012 비용 = 0.040682739
회차 : 0013 비용 = 0.032230697
회차 : 0014 비용 = 0.025317747
회차 : 0015 비용 = 0.019939722
회차 : 0016 비용 = 0.015559807
회차 : 0017 비용 = 0.011993421
회차 : 0018 비용 = 0.009289965
회차 : 0019 비용 = 0.007239956
회차 : 0020 비용 = 0.005603419
훈련 단계 완료
모델 정확도 : 0.9465
728x90
반응형
그리드형(광고전용)
'Python > tensorflow' 카테고리의 다른 글
[PYTHON/TENSORFLOW] InteractiveSession 클래스 사용하기 (0) | 2018.07.29 |
---|---|
[PYTHON/TENSORFLOW] Variable 클래스 : 변수 사용하기 (0) | 2018.07.29 |
[PYTHON/TENSORFLOW] constant 함수 : 상수 만들기 (0) | 2018.07.29 |
[PYTHON/TENSORFLOW] Tensor 클래스 : get_shape 메소드를 사용해 랭크 구하기 (0) | 2018.07.29 |
[PYTHON/TENSORFLOW] placeholder 함수 사용하기 (0) | 2018.07.29 |
[PYTHON/TENSORFLOW] 단일 계층 퍼셉트론 신경망 만들기 (MNIST) (0) | 2018.07.09 |
[PYTHON/TENSORFLOW] K-평균 군집화(K-Means Clustering) 알고리즘 사용하기 (0) | 2018.07.08 |
[PYTHON/TENSORFLOW] K-최근접 이웃(KNN, K-Nearest Neighbors) 알고리즘 사용하기 (0) | 2018.07.08 |
[PYTHON/TENSORFLOW] MNIST 데이터 로드하기 (0) | 2018.07.08 |
[PYTHON/TENSORFLOW] 선형 회귀 알고리즘 사용하기 (0) | 2018.07.06 |
댓글을 달아 주세요