모델(model)이
예측(logit)하고
얼마나 틀렸는지(loss) 계산하고
그걸 최적화(Optm)하고
최적화한 loss를 모델에 적용하고
이를 지정한 횟수만큼 반복해서
그 결과를 result에 출력
In [1]:
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
from tensorflow.keras import layers
import numpy as np
from tensorflow.keras import datasets
모델을 학습하기 전 설정(Optimization)¶
- Loss Function
- Optimization(최적화 방법 정하기)
- Metrics(결과 테스트 방법)
In [2]:
## sparse_categorical 원핫인코딩을 안하고 결과가 여러가지일경우
tf.keras.losses.sparse_categorical_crossentropy
Out[2]:
<function tensorflow.python.keras.losses.sparse_categorical_crossentropy(y_true, y_pred, from_logits=False, axis=-1)>
In [3]:
tf.keras.losses.categorical_crossentropy
# 원 핫 인코딩을 줬을때는 바로 categorical_crossentropy사용하면된다
Out[3]:
<function tensorflow.python.keras.losses.categorical_crossentropy(y_true, y_pred, from_logits=False, label_smoothing=0)>
In [4]:
loss_fun = tf.keras.losses.sparse_categorical_crossentropy
In [5]:
# Metrics (평가방법)
#전체 문제에서 결과가 얼마나 맞았는지 accuracy
metrics = ['accuracy']
#tf.keras.metrics.Accuracy() #위에 해놓은거랑 똑같음
#tf.keras.metrics.Precision()
#tf.keras.metrics.Recall()
In [6]:
optm = tf.keras.optimizers.Adam()
In [7]:
### 전 단계에서 만들어놨던 model
input_shape=(28,28,1)
num_classes = 10
inputs = layers.Input(shape=input_shape)#사용할 데이터 (레이어에 통과시켜서 전처리)
#Feature Extraction
net = layers.Conv2D(32,3,padding='SAME')(inputs) #(필터갯수,사이즈,패딩방법)(적용할 데이터)
net = layers.Activation('relu')(net)
#결과1 도출
net = layers.Conv2D(32,3,padding='SAME')(net)
net = layers.Activation('relu')(net)
#결과2 도출(결과1 사용)
net = layers.MaxPool2D((2,2))(net) #MaxPool 풀링을 할때 이미지에서 가장많이 사용하는 방법, 이미지 데이터를 분할해서 각 파트별로 최대값들을 모아서 처리하는 방식(2,2)니까 4분할이라고 보면된다.
net = layers.Dropout(0.25)(net)
###############################################
net = layers.Conv2D(64,3,padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.Conv2D(64,3,padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.MaxPool2D((2,2))(net)
net = layers.Dropout(0.25)(net)
#Fully Connected
net = layers.Flatten()(net)
net = layers.Dense(512)(net)
net = layers.Activation('relu')(net)
net = layers.Dropout(0.25)(net)
#결과를 몇개 출력하고 싶은지(학습이후 결과 이미지가 10인지 9인지 8인지 각 퍼센트가 제일 높은것중 총 몇개를 출력하고 싶은지 결정)
net = layers.Dense(10)(net)
net = layers.Activation('softmax')(net)
model=tf.keras.Model(inputs=inputs,outputs=net,name='Basic_CNN')
In [8]:
model.compile(optimizer=optm,
loss=loss_fun,
metrics=metrics
)
#metrics는 리스트를 사용하는데 결과에 대한 정보를 출력하기 때문에 그럼
In [9]:
#데이터 입력
(train_x,train_y),(test_x,test_y) = datasets.mnist.load_data()
train_x.shape,train_y.shape
Out[9]:
((60000, 28, 28), (60000,))
In [10]:
# 문제점 1) 채널이 없음(흑백이라그럼)
# 차원수 늘리기
# 방법 1 : np.expand_dims(train_x,-1).shape
# 방법 2 : tf.expand_dims(train_x,-1).shape
# 방법 3 : train_x = train_x[...,tf.newaxis]
train_x = train_x[...,tf.newaxis]
In [11]:
train_x.shape
Out[11]:
(60000, 28, 28, 1)
In [12]:
np.min(train_x),np.max(train_x)
Out[12]:
(0, 255)
In [13]:
# Rescaling
# 0~255까지는 너무 넓어서 0~1로 리스케일링 해준다.
train_x=train_x / 255.
test_x=test_x/255. #train의 갯수랑 test갯수는 동일해야 한다(비교군)
In [14]:
train_x.shape
Out[14]:
(60000, 28, 28, 1)
In [15]:
np.min(train_x),np.max(train_x)
#데이터 전처리 과정
#차원수를 동일하게 만들기(gray 스케일이면 차원수를 늘려주는게 좋은 RGB면 굳이 안늘려도됨)
Out[15]:
(0.0, 1.0)
In [55]:
num_epochs= 1
batch_size = 64
#당연히 epochs가 늘어나면 학습 시간이 비례하게 증가한다.
#이떄문에 hadoop을 사용해서 병렬처리하는 기술이 필요함
In [17]:
model.fit(train_x,train_y,batch_size=batch_size,shuffle=True,epochs=num_epochs) #mode.compile하고 나서 model.fit()
1875/1875 [==============================] - 88s 47ms/step - loss: 0.2729 - accuracy: 0.9099
Out[17]:
<tensorflow.python.keras.callbacks.History at 0x1c9f28827c0>
Preprocess¶
데이터를 export하는 방법 tf.data 사용
In [34]:
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
- from_tensor_slices()
- shuffle()
- batch()
마크다운 '*'는 점 표시다¶
In [35]:
train_ds = tf.data.Dataset.from_tensor_slices((x_train,y_train))
#batch size를 주어주는거임 #위쪽을 보면 train_data에 batch(순서)가 없어서 데이터가 중구난방이지만 from_tensor_slices를 통해 batch를 준다음 train_ds에 입력하는것이다.
train_df = train_ds.shuffle(1000) #1000은 default값으로 제일 적절하다
train_ds = train_ds.batch(32) #batch사이즈만큼 불러오기
test_ds = tf.data.Dataset.from_tensor_slices((x_test,y_test))
test_ds = test_ds.batch(32)
In [36]:
import matplotlib.pyplot as plt
%matplotlib inline
train_ds
Out[36]:
<BatchDataset shapes: ((None, 28, 28, 1), (None,)), types: (tf.float64, tf.uint8)>
In [37]:
image,label = next(iter(train_ds))
In [38]:
image.shape , label.shape
Out[38]:
(TensorShape([32, 28, 28, 1]), TensorShape([32]))
In [40]:
for image, label in train_ds.take(2): #train_ds.take() 원하는 길이만큼만 가져오기
# plt.title(label[0])
plt.imshow(image[0,:,:,0],'gray')
plt.imshow(image[1,:,:,0],'gray')
plt.show()
In [44]:
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy')
model.fit(train_ds,epochs=2)
Epoch 1/1000
1875/1875 [==============================] - 86s 46ms/step - loss: 0.0548
Epoch 2/1000
1875/1875 [==============================] - 93s 50ms/step - loss: 0.0364
Epoch 3/1000
1875/1875 [==============================] - 85s 45ms/step - loss: 0.0331
Epoch 4/1000
1875/1875 [==============================] - 88s 47ms/step - loss: 0.0258
Epoch 5/1000
1875/1875 [==============================] - 87s 46ms/step - loss: 0.0211
Epoch 6/1000
1875/1875 [==============================] - 89s 47ms/step - loss: 0.0201
Epoch 7/1000
1875/1875 [==============================] - 88s 47ms/step - loss: 0.0191
Epoch 8/1000
1875/1875 [==============================] - 85s 45ms/step - loss: 0.0143
Epoch 9/1000
1875/1875 [==============================] - 85s 45ms/step - loss: 0.0147
Epoch 10/1000
1875/1875 [==============================] - 86s 46ms/step - loss: 0.0157
Epoch 11/1000
1875/1875 [==============================] - 85s 45ms/step - loss: 0.0119
Epoch 12/1000
1875/1875 [==============================] - 90s 48ms/step - loss: 0.0120
Epoch 13/1000
1875/1875 [==============================] - 92s 49ms/step - loss: 0.0120
Epoch 14/1000
1875/1875 [==============================] - 91s 48ms/step - loss: 0.0146
Epoch 15/1000
1875/1875 [==============================] - 84s 45ms/step - loss: 0.0118
Epoch 16/1000
1875/1875 [==============================] - 80s 42ms/step - loss: 0.0128
Epoch 17/1000
1875/1875 [==============================] - 83s 44ms/step - loss: 0.0110
Epoch 18/1000
1875/1875 [==============================] - 81s 43ms/step - loss: 0.0112
Epoch 19/1000
1875/1875 [==============================] - 80s 43ms/step - loss: 0.0110
Epoch 20/1000
1875/1875 [==============================] - 81s 43ms/step - loss: 0.0111
Epoch 21/1000
1875/1875 [==============================] - 80s 43ms/step - loss: 0.0121
Epoch 22/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0090
Epoch 23/1000
1875/1875 [==============================] - 78s 42ms/step - loss: 0.0106
Epoch 24/1000
1875/1875 [==============================] - 78s 41ms/step - loss: 0.0070
Epoch 25/1000
1875/1875 [==============================] - 80s 43ms/step - loss: 0.0110
Epoch 26/1000
1875/1875 [==============================] - 81s 43ms/step - loss: 0.0092
Epoch 27/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0097
Epoch 28/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0075
Epoch 29/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0100
Epoch 30/1000
1875/1875 [==============================] - 76s 41ms/step - loss: 0.0106
Epoch 31/1000
1875/1875 [==============================] - 77s 41ms/step - loss: 0.0135
Epoch 32/1000
1875/1875 [==============================] - 76s 41ms/step - loss: 0.0134
Epoch 33/1000
1875/1875 [==============================] - 76s 41ms/step - loss: 0.0076
Epoch 34/1000
1875/1875 [==============================] - 78s 42ms/step - loss: 0.0091
Epoch 35/1000
1875/1875 [==============================] - 81s 43ms/step - loss: 0.0114
Epoch 36/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0082
Epoch 37/1000
1875/1875 [==============================] - 81s 43ms/step - loss: 0.0080
Epoch 38/1000
1875/1875 [==============================] - 81s 43ms/step - loss: 0.0134
Epoch 39/1000
1875/1875 [==============================] - 78s 42ms/step - loss: 0.0110
Epoch 40/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0095
Epoch 41/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0113
Epoch 42/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0133
Epoch 43/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0094
Epoch 44/1000
1875/1875 [==============================] - 80s 43ms/step - loss: 0.0114
Epoch 45/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0095
Epoch 46/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0094
Epoch 47/1000
1875/1875 [==============================] - 80s 43ms/step - loss: 0.0095
Epoch 48/1000
1875/1875 [==============================] - 78s 42ms/step - loss: 0.0111
Epoch 49/1000
1875/1875 [==============================] - 80s 43ms/step - loss: 0.0135
Epoch 50/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0110
Epoch 51/1000
1875/1875 [==============================] - 81s 43ms/step - loss: 0.0130
Epoch 52/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0113
Epoch 53/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0082
Epoch 54/1000
1875/1875 [==============================] - 80s 43ms/step - loss: 0.0107
Epoch 55/1000
1875/1875 [==============================] - 80s 42ms/step - loss: 0.0110
Epoch 56/1000
1875/1875 [==============================] - 80s 43ms/step - loss: 0.0117
Epoch 57/1000
1875/1875 [==============================] - 80s 43ms/step - loss: 0.0111
Epoch 58/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0114
Epoch 59/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0114
Epoch 60/1000
1875/1875 [==============================] - 82s 44ms/step - loss: 0.0094
Epoch 61/1000
1875/1875 [==============================] - 86s 46ms/step - loss: 0.0118
Epoch 62/1000
1875/1875 [==============================] - 86s 46ms/step - loss: 0.0106
Epoch 63/1000
1875/1875 [==============================] - 85s 45ms/step - loss: 0.0101
Epoch 64/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0182
Epoch 65/1000
1875/1875 [==============================] - 85s 46ms/step - loss: 0.0082
Epoch 66/1000
1875/1875 [==============================] - 82s 44ms/step - loss: 0.0077
Epoch 67/1000
1875/1875 [==============================] - 85s 46ms/step - loss: 0.0133
Epoch 68/1000
1875/1875 [==============================] - 85s 45ms/step - loss: 0.0108
Epoch 69/1000
1875/1875 [==============================] - 84s 45ms/step - loss: 0.0107
Epoch 70/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0100
Epoch 71/1000
1875/1875 [==============================] - 83s 44ms/step - loss: 0.0092
Epoch 72/1000
1875/1875 [==============================] - 82s 44ms/step - loss: 0.0084
Epoch 73/1000
1875/1875 [==============================] - 95s 51ms/step - loss: 0.0104
Epoch 74/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0121
Epoch 75/1000
1875/1875 [==============================] - 80s 42ms/step - loss: 0.0101
Epoch 76/1000
1875/1875 [==============================] - 81s 43ms/step - loss: 0.0110
Epoch 77/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0096
Epoch 78/1000
1875/1875 [==============================] - 77s 41ms/step - loss: 0.0071
Epoch 79/1000
1875/1875 [==============================] - 78s 42ms/step - loss: 0.0123
Epoch 80/1000
1875/1875 [==============================] - 80s 42ms/step - loss: 0.0092
Epoch 81/1000
1875/1875 [==============================] - 77s 41ms/step - loss: 0.0113
Epoch 82/1000
1875/1875 [==============================] - 77s 41ms/step - loss: 0.0095
Epoch 83/1000
1875/1875 [==============================] - 76s 41ms/step - loss: 0.0089
Epoch 84/1000
1875/1875 [==============================] - 77s 41ms/step - loss: 0.0082
Epoch 85/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0137
Epoch 86/1000
1875/1875 [==============================] - 79s 42ms/step - loss: 0.0065
Epoch 87/1000
1875/1875 [==============================] - 78s 41ms/step - loss: 0.0119
Epoch 88/1000
1875/1875 [==============================] - 78s 42ms/step - loss: 0.0106
Epoch 89/1000
1875/1875 [==============================] - 87s 46ms/step - loss: 0.0107
Epoch 90/1000
1875/1875 [==============================] - 94s 50ms/step - loss: 0.0102
Epoch 91/1000
1875/1875 [==============================] - 100s 53ms/step - loss: 0.0102
Epoch 92/1000
1875/1875 [==============================] - 87s 46ms/step - loss: 0.0095
Epoch 93/1000
1875/1875 [==============================] - 81s 43ms/step - loss: 0.0115
Epoch 94/1000
1875/1875 [==============================] - 89s 48ms/step - loss: 0.0116
Epoch 95/1000
1875/1875 [==============================] - 90s 48ms/step - loss: 0.0104
Epoch 96/1000
1554/1875 [=======================>......] - ETA: 14s - loss: 0.0152
In [45]:
loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam()
In [46]:
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")
In [49]:
@tf.function
def train_step(images,labels):
with tf.GradientTape() as tape:
predictions=model(images)
loss = loss_object(labels,predictions)
gradients = tape.gradient(loss,model.trainable_variables)
optimizer.apply_gradients(zip(gradients,model.trainable_variables))
train_loss(loss)
train_accuracy(labels,predictions)
In [50]:
@tf.function
def test_step(images,labels):
predictions = model(images)
t_loss=loss_object(labels,predictions)
test_loss(t_loss)
test_accuracy(labels,predictions)
In [52]:
for epoch in range(2):
for image,label in train_ds:
train_step(image,label)
for test_images,test_labels in test_ds:
test_step(test_images,test_labels)
template = 'Epoch {}, Loss : {} , Accuracy : {}, Test Loss {}, Test Accuracy : {}'
print(template.format(epoch+1,
train_loss.result(),
train_accuracy.result() * 100 ,
test_loss.result(),
test_accuracy.result() * 100))
Epoch 1, Loss : 0.0040123965591192245 , Accuracy : 99.95625305175781, Test Loss 0.09324337542057037, Test Accuracy : 99.47000122070312
Epoch 2, Loss : 0.003479651641100645 , Accuracy : 99.96094512939453, Test Loss 0.09521465748548508, Test Accuracy : 99.40499877929688
In [56]:
model.evaluate(test_x,test_y,batch_size=batch_size)
157/157 [==============================] - 2s 11ms/step - loss: 0.0973
Out[56]:
0.09734144061803818
In [61]:
test_image=test_x[0,:,:]
test_image.shape
Out[61]:
(28, 28)
In [62]:
plt.title(test_y[0])
plt.imshow(test_image,'gray')
plt.show()
In [65]:
pred = model.predict(test_image.reshape(1,28,28,1))
In [68]:
np.argmax(pred) #정답이 7로 나왔다
Out[68]:
7
In [69]:
test_batch = test_x[:32]
test_batch.shape
Out[69]:
(32, 28, 28)
In [70]:
preds=model.predict(test_batch)
In [72]:
preds.shape
Out[72]:
(32, 10)
In [73]:
np.argmax(preds,-1)
Out[73]:
array([7, 2, 1, 0, 4, 1, 4, 9, 5, 9, 0, 6, 9, 0, 1, 5, 9, 7, 3, 4, 9, 6,
6, 5, 4, 0, 7, 4, 0, 1, 3, 1], dtype=int64)
In [78]:
plt.imshow(test_batch[31,:,:],'gray')
plt.show()
In [ ]:
728x90
반응형
'AI > 머신러닝' 카테고리의 다른 글
[빅데이터]AI 학습용 라벨링 된 빅데이터 무료 다운로드 받기(API도 제공) free BigData Download (1) | 2023.11.15 |
---|---|
[Python] 학습이 완료된 모델 저장해서 다른곳에서 사용하기 (1) | 2022.05.09 |
[Matplotlib] 번외. matplotpib 한글폰트 깨짐 해결 (0) | 2021.06.18 |
[Tensorflow] 3.tensorflow에서 레이어가 어떤식으로 동작하는지 (0) | 2021.06.18 |
[Tensorflow] 2.Tensorflow기본 DataSet (MNIST) (0) | 2021.06.18 |