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

readme

parent 7abd6412
Branches
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: %% Cell type:code id: tags:
``` python ``` python
import sys import sys
import tensorflow as tf import tensorflow as tf
import keras import keras
from keras.models import Sequential from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.utils import np_utils
import numpy as np import numpy as np
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() (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: %% Cell type:code id: tags:
``` python ``` python
img_rows = 28 img_rows = 28
img_cols = 28 img_cols = 28
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 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_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
x_train = x_train.astype('float32') / 255. x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255. x_test = x_test.astype('float32') / 255.
print('x_train shape:', x_train.shape) print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples') print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples') print(x_test.shape[0], 'test samples')
``` ```
%% Output %% Output
x_train shape: (60000, 28, 28, 1) x_train shape: (60000, 28, 28, 1)
60000 train samples 60000 train samples
10000 test samples 10000 test samples
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` 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 num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes) y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, 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: %% Cell type:code id: tags:
``` python ``` python
import random y_train[0]
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()
``` ```
%% Output %% Output
--------------------------------------------------------------------------- array([0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], dtype=float32)
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'
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def create_model( img_rows,img_cols, num_classes): def create_model_( img_rows,img_cols, num_classes):
inputs = tf.keras.Input(shape=(img_rows, img_cols, 1)) inputs = tf.keras.Input(shape=(28, 28, 1))
x = tf.keras.layers.Conv2D(32, kernel_size = (5,5), name='c1', padding='same', x = tf.keras.layers.Conv2D(32, kernel_size = (5,5), padding='same',
activation='relu')(inputs) activation='relu')(inputs)
x = tf.keras.layers.MaxPool2D(pool_size = (2,2),strides = (2,2))(x) 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) activation='relu')(x)
x = tf.keras.layers.MaxPool2D(pool_size = (2,2))(x) x = tf.keras.layers.MaxPool2D(pool_size = (2,2))(x)
x = tf.keras.layers.Dropout(0.25)(x) x = tf.keras.layers.Dropout(0.25)(x)
x = tf.keras.layers.Flatten(name='flatten')(x) x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(1000, name='fc3', activation='leaky_relu')(x) x = tf.keras.layers.Dense(1000, activation='leaky_relu')(x)
x = tf.keras.layers.Dropout(0.5)(x) x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(num_classes, activation='softmax')(x) outputs = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
model.summary()
return model
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() model.summary()
``` ```
%% Output %% Output
Model: "as3s6g-confscore-center" 28 28
Model: "image_class"
_________________________________________________________________ _________________________________________________________________
Layer (type) Output Shape Param # Layer (type) Output Shape Param #
================================================================= =================================================================
input_1 (InputLayer) [(None, 75, 3)] 0 input_1 (InputLayer) [(None, 28, 28, 1)] 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 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 dense_1 (Dense) (None, 10) 10010
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 Total params: 3,156,098
Trainable params: 4,635,196 Trainable params: 3,156,098
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
Non-trainable params: 0 Non-trainable params: 0
_________________________________________________________________ _________________________________________________________________
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` 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))
``` ```
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment