참고) 해당 포스팅은 패스트 캠퍼스 100% 환급 챌린지 미션 기록용 포스팅이며, 강좌 내용에 맞추어 포스팅함을 언급
클립명 : PART2)TensorFlow 2.0과 Pytorch 프레임워크 기초
07. TensorFlow2.0 - 각 Layer별 역활 개념 및 파라미터 파악 - 4 08. TensorFlow2.0 각 - Optimizer 및 Training 09. TensorFlow2.0 각 - Optimizer 및 Training(Expert)
이번 포스팅에서 언급할 내용은 따로 없다. 그동한 말로써 설명해왔던 것들은 Code로 구현하는 과정이며 Model 설정 -> 데이터 불로오기 -> Optimizer 및 Loss 함수 설정 -> Compile 설정 -> 실행 이러한 순서로 코드가 진행이 된다. 여기서 말하는 Model은 이전 포스팅에서 언급한 feature extracion 및 classification이 모두 구현되며, Dense까지도 몇개로 설정될지 구성한다.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <code> import tensorflow as tf from tensorflow.keras import layers from tensorflow.keras import datasets input_shape = ( 28 , 28 , 1 ) # input variable의 shape를 알아야 한다 num_classes = 10 # 총 결과값 수 inputs = layers. Input (input_shape, dtype = tf.float64) net = layers.Conv2D( 32 , ( 3 , 3 ), padding = 'SAME' )(inputs) net = layers.Activation( 'relu' )(net) net = layers.Conv2D( 32 , ( 3 , 3 ), padding = 'SAME' )(net) net = layers.Activation( 'relu' )(net) net = layers.MaxPooling2D(pool_size = ( 2 , 2 ))(net) net = layers.Dropout( 0.25 )(net) net = layers.Conv2D( 64 , ( 3 , 3 ), padding = 'SAME' )(net) net = layers.Activation( 'relu' )(net) net = layers.Conv2D( 64 , ( 3 , 3 ), padding = 'SAME' )(net) net = layers.Activation( 'relu' )(net) net = layers.MaxPooling2D(pool_size = ( 2 , 2 ))(net) net = layers.Dropout( 0.25 )(net) net = layers.Flatten()(net) net = layers.Dense( 512 )(net) net = layers.Activation( 'relu' )(net) net = layers.Dropout( 0.5 )(net) net = layers.Dense( 10 )(net) # num_classes net = layers.Activation( 'softmax' )(net) model = tf.keras.Model(inputs = inputs, outputs = net, name = 'Basic_CNN' ) model model.summary() mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() # Channel 차원 추가 x_train = x_train[..., tf.newaxis] x_test = x_test[..., tf.newaxis] # Data normalization x_train, x_test = x_train / 255.0 , x_test / 255.0 # tf.data train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)) train_ds = train_ds.shuffle( 1000 ) train_ds = train_ds.batch( 32 ) test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)) test_ds = test_ds.batch( 32 ) import matplotlib.pyplot as plt # train_ds.take() for image, label in train_ds.take( 2 ): plt.title(label[ 0 ].numpy()) plt.imshow(image[ 0 , :, :, 0 ], 'gray' ) plt.show() # Training(Keras) Keras로 학습 할 때는 기존과 같지만, train_ds는 generator라서 그대로 넣을 수 있음 model. compile (optimizer = 'adam' , loss = 'sparse_categorical_crossentropy' ) model.fit(train_ds, epochs = 10000 ) # Optimization loss_object = tf.keras.losses.SparseCategoricalCrossentropy() optimizer = tf.keras.optimizers.Adam() train_loss = tf.keras.metrics.Mean(name = 'train_loss' ) train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name = 'train_accuracy' ) test_loss = tf.keras.metrics.Mean(name = 'test_loss' ) test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name = 'test_accuracy' ) < / code> |
위의 코드가 앞에서 언급한 순서대로의 절차이며, 간락하게 작성했다. 딱히 이번 3 강의에서는 설명이 없었으며 코드만 구현했기에, 이렇게만 적어두고 포스팅을 마무리 한다.
(글자수 :Code포함 2000자 이상)
강의 소개 링크 : bit.ly/3cx6kMd

'Deep Learning & AI > AI' 카테고리의 다른 글
딥러닝의 3 STEP의 기초 (1~3강) (0) | 2021.03.08 |
---|---|
Tensorflow2.0 각 CNN 코드 구현 02 (0) | 2021.03.06 |
Tensorflow2.0 각 Layer별 역활 개념 및 파라미터 파악 (0) | 2021.02.27 |
TensorFlow & Pytorch 소개, 사용법 및 dataset 소개 (0) | 2021.02.25 |
CNN 모델구조 (0) | 2021.02.17 |