728x90
728x170
■ 단일 계층 퍼셉트론 신경망을 만드는 방법을 보여준다.
▶ input_data.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import gzip
import os
import tempfile
import numpy
from six.moves import urllib
from six.moves import xrange
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
▶ slp.py
import input_data
import matplotlib.pyplot as pp
import tensorflow as tf
numberPixelDatasets = input_data.read_data_sets("numberPixelDatasets_data/", one_hot = True)
learningRate = 0.01
trainingEpochCount = 25
batchSize = 100
displayStep = 1
xTensor = tf.placeholder("float", [None, 784]) # 28×28 = 784
yTensor = tf.placeholder("float", [None, 10 ]) # 0-9 숫자 인식
weightVariable = tf.Variable(tf.zeros([784, 10]))
biasVariable = tf.Variable(tf.zeros([10]))
activationTensor = tf.nn.softmax(tf.matmul(xTensor, weightVariable) + biasVariable)
crossEntropyTensor = yTensor * tf.log(activationTensor)
costTensor = tf.reduce_mean(-tf.reduce_sum(crossEntropyTensor, reduction_indices = 1))
optimizerOperation = tf.train.GradientDescentOptimizer(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 = {xTensor : xBatchNDArray, yTensor : yBatchNDArray})
averageCost += sess.run(costTensor, feed_dict = {xTensor : xBatchNDArray, yTensor : 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(activationTensor, 1), tf.argmax(yTensor, 1))
accuracyTebsor = tf.reduce_mean(tf.cast(correctPredictionTensor, "float"))
print("모델 정확도 :", accuracyTebsor.eval({xTensor : numberPixelDatasets.test.images, yTensor : numberPixelDatasets.test.labels}))
pp.plot(epochList, averageList, "o", label = "Logistic Regression Training phase")
pp.ylabel("cost")
pp.xlabel("epoch")
pp.legend()
pp.show()
▶ 결과
회차 : 0001 비용 = 1.176287905
회차 : 0002 비용 = 0.662353746
회차 : 0003 비용 = 0.550700741
회차 : 0004 비용 = 0.496796931
회차 : 0005 비용 = 0.463800235
회차 : 0006 비용 = 0.440929263
회차 : 0007 비용 = 0.423949052
회차 : 0008 비용 = 0.410716742
회차 : 0009 비용 = 0.399927922
회차 : 0010 비용 = 0.390965419
회차 : 0011 비용 = 0.383320482
회차 : 0012 비용 = 0.376762401
회차 : 0013 비용 = 0.371033744
회차 : 0014 비용 = 0.365946764
회차 : 0015 비용 = 0.361372409
회차 : 0016 비용 = 0.357258832
회차 : 0017 비용 = 0.353575538
회차 : 0018 비용 = 0.350136466
회차 : 0019 비용 = 0.347030454
회차 : 0020 비용 = 0.344138179
회차 : 0021 비용 = 0.341470252
회차 : 0022 비용 = 0.339007374
회차 : 0023 비용 = 0.336622848
회차 : 0024 비용 = 0.334463308
회차 : 0025 비용 = 0.332481194
트레이닝 단계 완료
모델 정확도 : 0.914
728x90
그리드형(광고전용)
'Python > tensorflow' 카테고리의 다른 글
[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 |
[PYTHON/TENSORFLOW] 편미분 방정식을 사용해 빗방울 시뮬레이션 하기 (0) | 2018.07.06 |