티스토리 뷰

5. 파이썬

[텐서플로2] MNIST 예제 tf2_function.py

패스트코드블로그 2020. 5. 14. 23:12

MNIST 예제 main 처리

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
 
 
if __name__ == '__main__':
    mnist = keras.datasets.mnist
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
 
    mnist_idx = 100
 
    for row in train_images[mnist_idx]:
        for col in row:
            print('%10f' % col, end='')
        print('\n')
    print('\n')
 
    plt.figure(figsize=(5,5))
    image = train_images[mnist_idx]
    plt.imshow(image)
    plt.show()
cs

MNIST 예제 객체지향 처리

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import tensorflow as tf
 
class TFFuntion:
    
 
    def execute(self):
        mnist = tf.keras.datasets.mnist
        (x_train, y_train),(x_test, y_test) = mnist.load_data()
        x_train, x_test = x_train / 255.0, x_test / 255.0
 
        # 채널 차원 추가, 차원은 컬럼의의미
        x_train = x_train[..., tf.newaxis]
        x_test = x_test[..., tf.newaxis]
        train_ds = tf.data.Dataset.from_tensor_slices(
            (x_train, y_train)
        ).shuffle(10000).batch(32)
        test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_train)).batch(32)
cs

 

MNIST 예제 오리지널

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np
import matplotlib.pyplot as plt
 
from tensorflow import keras
mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
 
print("훈련 이미지: ", train_images.shape)
print("훈련 라벨: ", train_labels.shape)
print("테스트 이미지: ", test_images.shape)
print("테스트 라벨: ", test_labels.shape)
 
mnist_idx = 100
 
print('[label]')
print('number label = ', train_labels[mnist_idx])
 
for row in train_images[mnist_idx]:
    for col in row:
        print('%10f' % col, end='')
    print('\n')
print('\n')
 
plt.figure(figsize=(5,5))
image = train_images[mnist_idx]
plt.imshow(image)
plt.show()
cs
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함