Home>
Problems and what I want to achieve
Since loss became nan in Tensorflow, I thought about solving it by using an error function.
However, as a result of learning with my own error function, learning does not proceed at all.
I would like you to point out anything that is wrong.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import tensorflow as tf
import datetime
datagen = ImageDataGenerator (
normalize with rescale = 1./255, #rescale
Validation_split = 0.3 #validation_split can be used to split the validation dataset
)
aug_datagen = ImageDataGenerator (
normalize with rescale = 1./255, #rescale
rotation_range = 20, # Randomly rotate within ± 20 degrees
width_shift_range = 10, # Randomly move left and right within ± 8px range
height_shift_range = 10 # Randomly move up and down within ± 4px range
)
batch_size = 10
train_generator = datagen.flow_from_directory (
'/ content/drive/My Drive/my_data_set/train /',
target_size = (224, 224),
class_mode ='categorical',
batch_size = batch_size,
subset ='training',
)
aug_train_generator = aug_datagen.flow_from_directory (
'/ content/drive/My Drive/my_data_set/train /',
target_size = (224, 224),
class_mode ='categorical',
batch_size = batch_size,
subset ='training',
)
val_generator = datagen.flow_from_directory (
'/ content/drive/My Drive/my_data_set/train /',
target_size = (224, 224),
class_mode ='categorical',
batch_size = batch_size,
subset ='validation'
)
% load_ext tensorboard
os.chdir ('/ content/drive/My Drive/my_data_set /')
! rm -rf ./logs/
class MyModel (tf.keras.Model):
def __init __ (self):
super (MyModel, self) .__ init__ ()
self.conv1 = tf.keras.layers.Conv2D (8, (1,1), activation ='relu', input_shape = (224, 224, 3))
self.conv1_2 = tf.keras.layers.Conv2D (32, (5,5), activation ='relu')
self.conv2 = tf.keras.layers.Conv2D (16, (1,1), activation ='relu')
self.conv2_2 = tf.keras.layers.Conv2D (64, (3,3), activation ='relu')
self.conv3 = tf.keras.layers.Conv2D (32, (1,1), activation ='relu')
self.conv3_2 = tf.keras.layers.Conv2D (128, (3,3), activation ='relu')
self.pooling1 = tf.keras.layers.MaxPooling2D ((2,2), strides = None, padding ='valid')
self.pooling2 = tf.keras.layers.MaxPooling2D ((2,2), strides = None, padding ='valid')
self.flatten = tf.keras.layers.Flatten ()
self.fc1 = tf.keras.layers.Dense (256, activation ='relu')
self.fc2 = tf.keras.layers.Dense (9, activation ='relu')
self.dropout = tf.keras.layers.Dropout (0.2)
def call (self, x, training = False):
x = self.conv1 (x)
x = self.conv1_2 (x)
x = self.pooling1 (x)
x = self.conv2 (x)
x = self.conv2_2 (x)
x = self.pooling2 (x)
x = self.conv3 (x)
x = self.conv3_2 (x)
x = self.pooling2 (x)
x = self.flatten (x)
x = self.fc1 (x)
x = self.dropout (x, training = training)
x = self.fc2 (x)
return x
model = MyModel ()
Adam = tf.keras.optimizers.Adam (learning_rate = 0.001, clipvalue = 1.0)
def custom_loss (y_true, y_pred):
error = -tf.reduce_sum (y_true * tf.math.log (tf.nn.softmax (y_pred) + 1e-10))
return error
model.compile (optimizer = Adam,
loss = custom_loss,
metrics = ['accuracy'])
log_dir = "logs/fit /" + datetime.datetime.now (). strftime ("% Y% m% d-% H% M% S")
tensorboard_callback = tf.keras.callbacks.TensorBoard (log_dir = log_dir, histogram_freq = 1, write_graph = True)
early_stopping_call_back = tf.keras.callbacks.EarlyStopping (monitor ='accuracy', patience = 10, verbose = 0, mode ='auto')
set_seed (0)
model.fit (train_generator, epochs = 40)
history = model.fit (
aug_train_generator,
epochs = 50,
validation_data = val_generator,
callbacks = [tensorboard_callback, early_stopping_call_back],
)
What I tried
I wondered if some additions or changes were needed to support multi-class classification, but I have no idea how to solve it now.
Execution environmentgooglecolab
tensorflow 2.3.0
-
Answer # 1
Related articles
- python 3x - when using tensorflow-gpu, i get an error if the descriptor cannot be loaded
- python - i get an error when using an application created with django's startapp
- python - i want to change the value of the list using a function
- python - i want to fix garbled characters when using a function
- python - mute function error in async io of discordpy
- [python] i get a "syntax error" even if i enter a function according to the textbook
- python - i want to get rid of the error that the function is defined but not defined
- please explain the function using the python dictionary
- [python] await outside function error cannot be fixed
- error when using t-sne python
- python - error in image binarization using cv2adaptivethreshold function
- python - attributeerror:'series' object has no attribute'flags' error
- python - selenium: element specification an error occurs in the code that can be executed once
- i want to adjust the execution result using the while statement in python as expected
- python - about the fibonacci sequence using recursion
- specifying the range of the graph using python date
- syntaxerror: invalid syntax error python
- python 3x - an error occurs after registering a dictionary with a read-aloud bot on discordpy
- python - aggregation processing using pandas
- about the cause of the error in python pdto_datetime
Trends
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
It seems that softmax is not included in the error function in tesorflow, and it was solved by adding the softmax function at the end in the network definition.