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

loss_function_custom

parent 29faa206
Branches
No related tags found
No related merge requests found
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (base) (2)" project-jdk-type="Python SDK" />
<component name="PyCharmDSProjectLayout">
<option name="id" value="JupyterRightHiddenStructureLayout" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/opensource.iml" filepath="$PROJECT_DIR$/.idea/opensource.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ 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
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)
```
%% 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()
```
%% 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'
%% 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',
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',
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.Dropout(0.5)(x)
x = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
model.summary()
return model
model = create_model(img_cols, img_rows, num_classes)
model.summary()
```
%% Output
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
_________________________________________________________________
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
_________________________________________________________________
%% Cell type:code id: tags:
``` python
```
%% 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()
```
%% 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):
def ccee(y_true, y_pred):
delta = 1e-7
log_pred = np.log(predict + delta)
log_pred = tf.math.log(y_pred + delta)
return -(np.sum(np.sum(label * log_pred, axis = 1)))/label.shape[0]
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.to_categorical(y_train, num_classes)
y_test = keras.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 = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test = keras.utils.np_utils.to_categorical(y_test, num_classes)
```
%% 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'
%% 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
input_1 (InputLayer) [(None, 28, 28, 1)] 0
conv1d (Conv1D) (None, 75, 64) 20544
conv2d (Conv2D) (None, 28, 28, 32) 832
c3 (Conv1D) (None, 75, 128) 41088
max_pooling2d (MaxPooling2D (None, 14, 14, 32) 0
)
c4 (Conv1D) (None, 75, 128) 82048
conv2d_1 (Conv2D) (None, 14, 14, 64) 8256
c6 (Conv1D) (None, 75, 256) 164096
max_pooling2d_1 (MaxPooling (None, 7, 7, 64) 0
2D)
c7 (Conv1D) (None, 75, 256) 327936
dropout (Dropout) (None, 7, 7, 64) 0
c9 (Conv1D) (None, 75, 512) 655872
flatten (Flatten) (None, 3136) 0
c10 (Conv1D) (None, 75, 512) 1311232
dense (Dense) (None, 1000) 3137000
conv1d_1 (Conv1D) (None, 75, 256) 655616
dropout_1 (Dropout) (None, 1000) 0
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
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
```
%% 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
6000/6000 [==============================] - 96s 16ms/step - loss: 0.1236 - accuracy: 0.9616 - val_loss: 0.0493 - val_accuracy: 0.9853
Epoch 2/10
6000/6000 [==============================] - 150s 25ms/step - loss: 0.0680 - accuracy: 0.9796 - val_loss: 0.0501 - val_accuracy: 0.9845
Epoch 3/10
6000/6000 [==============================] - 186s 31ms/step - loss: 0.0557 - accuracy: 0.9835 - val_loss: 0.0439 - val_accuracy: 0.9856
Epoch 4/10
6000/6000 [==============================] - 206s 34ms/step - loss: 0.0517 - accuracy: 0.9848 - val_loss: 0.0337 - val_accuracy: 0.9890
Epoch 5/10
6000/6000 [==============================] - 180s 30ms/step - loss: 0.0480 - accuracy: 0.9861 - val_loss: 0.0295 - val_accuracy: 0.9912
Epoch 6/10
6000/6000 [==============================] - 159s 26ms/step - loss: 0.0436 - accuracy: 0.9869 - val_loss: 0.0415 - val_accuracy: 0.9880
Epoch 7/10
6000/6000 [==============================] - 162s 27ms/step - loss: 0.0492 - accuracy: 0.9863 - val_loss: 0.0433 - val_accuracy: 0.9876
Epoch 8/10
6000/6000 [==============================] - 139s 23ms/step - loss: 0.0454 - accuracy: 0.9875 - val_loss: 0.0372 - val_accuracy: 0.9901
Epoch 9/10
6000/6000 [==============================] - 138s 23ms/step - loss: 0.0424 - accuracy: 0.9883 - val_loss: 0.0314 - val_accuracy: 0.9905
Epoch 10/10
6000/6000 [==============================] - 133s 22ms/step - loss: 0.0421 - accuracy: 0.9885 - val_loss: 0.0401 - val_accuracy: 0.9893
%% Cell type:code id: tags:
``` python
hist
```
%% 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