Skip to content
Snippets Groups Projects
Commit e390f3d3 authored by yong hee won's avatar yong hee won
Browse files

readme

parent 7abd6412
No related branches found
No related tags found
No related merge requests found
# Loss function custom
## environment
- miniforge
- tensorflow 2.8.0
- matplotlib
## dataset
- mnist dataset
### Loss function
- categorical cross entropy : 범주형 데이터에 대한 Loss function
\ No newline at end of file
%% Cell type:code id: tags:
``` python
import sys
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.utils import np_utils
import numpy as np
```
%% Cell type:code id: tags:
``` python
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
```
%% Output
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 2s 0us/step
11501568/11490434 [==============================] - 2s 0us/step
%% Cell type:code id: tags:
``` python
img_rows = 28
img_cols = 28
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
```
%% Output
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
%% Cell type:code id: tags:
``` python
def ccee(predict, label):
delta = 1e-7
log_pred = np.log(predict + delta)
return -(np.sum(np.sum(label * log_pred, axis = 1)))/label.shape[0]
```
%% Cell type:code id: tags:
``` python
num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test = keras.utils.np_utils.to_categorical(y_test, num_classes)
```
%% Output
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Users/yongheewon/Documents/opensource/loss_function.ipynb Cell 4' in <cell line: 2>()
<a href='vscode-notebook-cell:/Users/yongheewon/Documents/opensource/loss_function.ipynb#ch0000006?line=0'>1</a> num_classes = 10
----> <a href='vscode-notebook-cell:/Users/yongheewon/Documents/opensource/loss_function.ipynb#ch0000006?line=1'>2</a> y_train = keras.utils.to_categorical(y_train, num_classes)
<a href='vscode-notebook-cell:/Users/yongheewon/Documents/opensource/loss_function.ipynb#ch0000006?line=2'>3</a> y_test = keras.utils.to_categorical(y_test, num_classes)
AttributeError: module 'keras.utils' has no attribute 'to_categorical'
%% Cell type:code id: tags:
``` python
import random
import matplotlib.pyplot as plt
predicted_result = model.predict(x_test)
predicted_labels = np.argmax(predicted_result, axis=1)
test_labels = np.argmax(y_test, axis=1)
count = 0
plt.figure(figsize=(12,8))
for n in range(16):
count += 1
plt.subplot(4, 4, count)
plt.imshow(x_test[n].reshape(28, 28), cmap='Greys', interpolation='nearest')
tmp = "Label:" + str(test_labels[n]) + ", Prediction:" + str(predicted_labels[n])
plt.title(tmp)
plt.tight_layout()
plt.show()
y_train[0]
```
%% Output
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
/Users/yongheewon/Documents/opensource/loss_function.ipynb Cell 4' in <cell line: 2>()
<a href='vscode-notebook-cell:/Users/yongheewon/Documents/opensource/loss_function.ipynb#ch0000003?line=0'>1</a> import random
----> <a href='vscode-notebook-cell:/Users/yongheewon/Documents/opensource/loss_function.ipynb#ch0000003?line=1'>2</a> import matplotlib.pyplot as plt
<a href='vscode-notebook-cell:/Users/yongheewon/Documents/opensource/loss_function.ipynb#ch0000003?line=3'>4</a> predicted_result = model.predict(x_test)
<a href='vscode-notebook-cell:/Users/yongheewon/Documents/opensource/loss_function.ipynb#ch0000003?line=4'>5</a> predicted_labels = np.argmax(predicted_result, axis=1)
ModuleNotFoundError: No module named 'matplotlib'
array([0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], dtype=float32)
%% Cell type:code id: tags:
``` python
def create_model( img_rows,img_cols, num_classes):
inputs = tf.keras.Input(shape=(img_rows, img_cols, 1))
x = tf.keras.layers.Conv2D(32, kernel_size = (5,5), name='c1', padding='same',
def create_model_( img_rows,img_cols, num_classes):
inputs = tf.keras.Input(shape=(28, 28, 1))
x = tf.keras.layers.Conv2D(32, kernel_size = (5,5), padding='same',
activation='relu')(inputs)
x = tf.keras.layers.MaxPool2D(pool_size = (2,2),strides = (2,2))(x)
x = tf.keras.layers.Conv2D(64, kernel_size = (2,2), name='c1', padding='same',
x = tf.keras.layers.Conv2D(64, kernel_size = (2,2),padding='same',
activation='relu')(x)
x = tf.keras.layers.MaxPool2D(pool_size = (2,2))(x)
x = tf.keras.layers.Dropout(0.25)(x)
x = tf.keras.layers.Flatten(name='flatten')(x)
x = tf.keras.layers.Dense(1000, name='fc3', activation='leaky_relu')(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(1000, activation='leaky_relu')(x)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
model.summary()
return model
outputs = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
model = keras.Model(inputs, outputs, name='image_class')
return model
model = create_model(img_cols, img_rows, num_classes)
print(img_cols, img_rows)
model = create_model_(img_rows,img_cols, num_classes)
model.summary()
```
%% Output
Model: "as3s6g-confscore-center"
28 28
Model: "image_class"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 75, 3)] 0
c1 (Conv1D) (None, 75, 64) 1024
c2 (Conv1D) (None, 75, 64) 20544
conv1d (Conv1D) (None, 75, 64) 20544
c3 (Conv1D) (None, 75, 128) 41088
c4 (Conv1D) (None, 75, 128) 82048
c6 (Conv1D) (None, 75, 256) 164096
c7 (Conv1D) (None, 75, 256) 327936
c9 (Conv1D) (None, 75, 512) 655872
input_1 (InputLayer) [(None, 28, 28, 1)] 0
c10 (Conv1D) (None, 75, 512) 1311232
conv2d (Conv2D) (None, 28, 28, 32) 832
conv1d_1 (Conv1D) (None, 75, 256) 655616
max_pooling2d (MaxPooling2D (None, 14, 14, 32) 0
)
conv1d_2 (Conv1D) (None, 75, 256) 65792
conv2d_1 (Conv2D) (None, 14, 14, 64) 8256
conv1d_3 (Conv1D) (None, 75, 128) 32896
max_pooling2d_1 (MaxPooling (None, 7, 7, 64) 0
2D)
conv1d_4 (Conv1D) (None, 75, 128) 16512
dropout (Dropout) (None, 7, 7, 64) 0
flatten (Flatten) (None, 9600) 0
flatten (Flatten) (None, 3136) 0
fc3 (Dense) (None, 128) 1228928
dense (Dense) (None, 1000) 3137000
dropout (Dropout) (None, 128) 0
dropout_1 (Dropout) (None, 1000) 0
fc4 (Dense) (None, 64) 8256
dropout_1 (Dropout) (None, 64) 0
dense (Dense) (None, 32) 2080
dropout_2 (Dropout) (None, 32) 0
dense_1 (Dense) (None, 16) 528
dropout_3 (Dropout) (None, 16) 0
fc5 (Dense) (None, 12) 204
reshape (Reshape) (None, 6, 2) 0
dense_1 (Dense) (None, 10) 10010
=================================================================
Total params: 4,635,196
Trainable params: 4,635,196
Non-trainable params: 0
_________________________________________________________________
Model: "as3s6g-confscore-center"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 75, 3)] 0
c1 (Conv1D) (None, 75, 64) 1024
c2 (Conv1D) (None, 75, 64) 20544
conv1d (Conv1D) (None, 75, 64) 20544
c3 (Conv1D) (None, 75, 128) 41088
c4 (Conv1D) (None, 75, 128) 82048
c6 (Conv1D) (None, 75, 256) 164096
c7 (Conv1D) (None, 75, 256) 327936
c9 (Conv1D) (None, 75, 512) 655872
c10 (Conv1D) (None, 75, 512) 1311232
conv1d_1 (Conv1D) (None, 75, 256) 655616
conv1d_2 (Conv1D) (None, 75, 256) 65792
conv1d_3 (Conv1D) (None, 75, 128) 32896
conv1d_4 (Conv1D) (None, 75, 128) 16512
flatten (Flatten) (None, 9600) 0
fc3 (Dense) (None, 128) 1228928
dropout (Dropout) (None, 128) 0
fc4 (Dense) (None, 64) 8256
dropout_1 (Dropout) (None, 64) 0
dense (Dense) (None, 32) 2080
dropout_2 (Dropout) (None, 32) 0
dense_1 (Dense) (None, 16) 528
dropout_3 (Dropout) (None, 16) 0
fc5 (Dense) (None, 12) 204
reshape (Reshape) (None, 6, 2) 0
=================================================================
Total params: 4,635,196
Trainable params: 4,635,196
Total params: 3,156,098
Trainable params: 3,156,098
Non-trainable params: 0
_________________________________________________________________
%% Cell type:code id: tags:
``` python
def ccee(y_true, y_pred):
delta = 1e-7
log_pred = tf.math.log(y_pred + delta)
return -(tf.reduce_sum(tf.reduce_sum(y_true * log_pred, axis = 1)))/y_true.shape[0]
```
%% Cell type:code id: tags:
``` python
model.compile( loss = ccee,optimizer='adam', metrics= ['accuracy'] )
hist = model.fit(x_train, y_train, batch_size=10, epochs= 10, verbose=1, validation_data=(x_test, y_test))
```
......
%% Cell type:code id: tags:
``` python
import sys
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.utils import np_utils
import numpy as np
```
%% Cell type:markdown id: tags:
%% Cell type:code id: tags:
``` python
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
```
%% Cell type:code id: tags:
``` python
img_rows = 28
img_cols = 28
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
```
%% Output
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
%% Cell type:code id: tags:
``` python
def ccee(y_true, y_pred):
delta = 1e-7
log_pred = tf.math.log(y_pred + delta)
return -(tf.reduce_sum(tf.reduce_sum(y_true * log_pred, axis = 1)))/y_true.shape[0]
```
%% Cell type:code id: tags:
``` python
num_classes = 10
y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test = keras.utils.np_utils.to_categorical(y_test, num_classes)
```
%% Cell type:code id: tags:
``` python
def create_model_( img_rows,img_cols, num_classes):
inputs = tf.keras.Input(shape=(28, 28, 1))
x = tf.keras.layers.Conv2D(32, kernel_size = (5,5), padding='same',
activation='relu')(inputs)
x = tf.keras.layers.MaxPool2D(pool_size = (2,2),strides = (2,2))(x)
x = tf.keras.layers.Conv2D(64, kernel_size = (2,2),padding='same',
activation='relu')(x)
x = tf.keras.layers.MaxPool2D(pool_size = (2,2))(x)
x = tf.keras.layers.Dropout(0.25)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(1000, activation='leaky_relu')(x)
x = tf.keras.layers.Dropout(0.5)(x)
outputs = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
model = keras.Model(inputs, outputs, name='image_class')
return model
print(img_cols, img_rows)
model = create_model_(img_rows,img_cols, num_classes)
model.summary()
```
%% Output
28 28
Model: "image_class"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 28, 28, 1)] 0
conv2d (Conv2D) (None, 28, 28, 32) 832
max_pooling2d (MaxPooling2D (None, 14, 14, 32) 0
)
conv2d_1 (Conv2D) (None, 14, 14, 64) 8256
max_pooling2d_1 (MaxPooling (None, 7, 7, 64) 0
2D)
dropout (Dropout) (None, 7, 7, 64) 0
flatten (Flatten) (None, 3136) 0
dense (Dense) (None, 1000) 3137000
dropout_1 (Dropout) (None, 1000) 0
dense_1 (Dense) (None, 10) 10010
=================================================================
Total params: 3,156,098
Trainable params: 3,156,098
Non-trainable params: 0
_________________________________________________________________
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
model.compile( loss = ccee,optimizer='adam', metrics= ['accuracy'] )
hist = model.fit(x_train, y_train, batch_size=10, epochs= 10, verbose=1, validation_data=(x_test, y_test))
```
%% Output
Epoch 1/10
1/6000 [..............................] - ETA: 17:42 - loss: 2.2814 - accuracy: 0.2000
2022-06-26 11:32:02.208949: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
2022-06-26 12:24:22.787706: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
6000/6000 [==============================] - 96s 16ms/step - loss: 0.1236 - accuracy: 0.9616 - val_loss: 0.0493 - val_accuracy: 0.9853
6000/6000 [==============================] - 68s 11ms/step - loss: 0.1259 - accuracy: 0.9608 - val_loss: 0.0342 - val_accuracy: 0.9888
Epoch 2/10
6000/6000 [==============================] - 150s 25ms/step - loss: 0.0680 - accuracy: 0.9796 - val_loss: 0.0501 - val_accuracy: 0.9845
6000/6000 [==============================] - 77s 13ms/step - loss: 0.0657 - accuracy: 0.9800 - val_loss: 0.0442 - val_accuracy: 0.9865
Epoch 3/10
6000/6000 [==============================] - 186s 31ms/step - loss: 0.0557 - accuracy: 0.9835 - val_loss: 0.0439 - val_accuracy: 0.9856
6000/6000 [==============================] - 81s 13ms/step - loss: 0.0554 - accuracy: 0.9827 - val_loss: 0.0312 - val_accuracy: 0.9902
Epoch 4/10
6000/6000 [==============================] - 206s 34ms/step - loss: 0.0517 - accuracy: 0.9848 - val_loss: 0.0337 - val_accuracy: 0.9890
6000/6000 [==============================] - 91s 15ms/step - loss: 0.0503 - accuracy: 0.9850 - val_loss: 0.0385 - val_accuracy: 0.9885
Epoch 5/10
6000/6000 [==============================] - 180s 30ms/step - loss: 0.0480 - accuracy: 0.9861 - val_loss: 0.0295 - val_accuracy: 0.9912
6000/6000 [==============================] - 110s 18ms/step - loss: 0.0481 - accuracy: 0.9862 - val_loss: 0.0359 - val_accuracy: 0.9887
Epoch 6/10
6000/6000 [==============================] - 159s 26ms/step - loss: 0.0436 - accuracy: 0.9869 - val_loss: 0.0415 - val_accuracy: 0.9880
6000/6000 [==============================] - 110s 18ms/step - loss: 0.0481 - accuracy: 0.9860 - val_loss: 0.0637 - val_accuracy: 0.9829
Epoch 7/10
6000/6000 [==============================] - 162s 27ms/step - loss: 0.0492 - accuracy: 0.9863 - val_loss: 0.0433 - val_accuracy: 0.9876
6000/6000 [==============================] - 108s 18ms/step - loss: 0.0444 - accuracy: 0.9873 - val_loss: 0.0417 - val_accuracy: 0.9903
Epoch 8/10
6000/6000 [==============================] - 139s 23ms/step - loss: 0.0454 - accuracy: 0.9875 - val_loss: 0.0372 - val_accuracy: 0.9901
6000/6000 [==============================] - 116s 19ms/step - loss: 0.0432 - accuracy: 0.9875 - val_loss: 0.0325 - val_accuracy: 0.9906
Epoch 9/10
6000/6000 [==============================] - 138s 23ms/step - loss: 0.0424 - accuracy: 0.9883 - val_loss: 0.0314 - val_accuracy: 0.9905
6000/6000 [==============================] - 103s 17ms/step - loss: 0.0430 - accuracy: 0.9886 - val_loss: 0.0329 - val_accuracy: 0.9898
Epoch 10/10
6000/6000 [==============================] - 133s 22ms/step - loss: 0.0421 - accuracy: 0.9885 - val_loss: 0.0401 - val_accuracy: 0.9893
6000/6000 [==============================] - 97s 16ms/step - loss: 0.0448 - accuracy: 0.9884 - val_loss: 0.0318 - val_accuracy: 0.9902
%% Cell type:code id: tags:
``` python
hist
from matplotlib import pyplot as plt
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
```
%% Output
<keras.callbacks.History at 0x16bb95e80>
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment