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
No related branches found
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
```
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