첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ MNIST 데이터를 로드하는 방법을 보여준다.

▶ 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

 

▶ mnist.py

import input_data
import matplotlib.pyplot as pp
import numpy as np

numberPixelDatasets = input_data.read_data_sets("MNIST_data/", one_hot = False)

# numberPixelNDArray : 숫자별 784개의 픽셀 값을 갖는 2차원 배열이다.
# numberNDArray      : 숫자 값을 갖는 1차원 배열이다.
numberPixelNDArray, numberNDArray = numberPixelDatasets.train.next_batch(10)

print("로드한 숫자 : {0}".format(numberNDArray))

# 출력할 숫자 인덱스
plotNumberIndex = 5

print("출력할 숫자 : " + str(numberNDArray[plotNumberIndex]))

# 출력할 숫자에 대한 1차원 배열을 구한다.
pixelNDArray = numberPixelNDArray[plotNumberIndex,:]
# 1차원 배열을 2차원 배열로 변환한다.
pixelNDArray = np.reshape(pixelNDArray, [28, 28])

pp.imshow(pixelNDArray)
pp.show()

 

▶ 결과

로드한 숫자 : [6 2 0 5 9 6 0 1 1 1]
출력할 숫자 : 6
728x90
그리드형(광고전용)
Posted by icodebroker
,