I made a model as below at first:
from tensorflow.keras.layers import Dense, Flatten, Conv2D, Dropout, BatchNormalization,
AveragePooling2D, ReLU, Activation
from tensorflow.keras import Model
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv = Conv2D(4, (3,3), padding = 'same', activation = 'linear'
,input_shape = x_train.shape[1:])
self.bn = BatchNormalization()
self.RL = ReLU()
self.FL = Flatten()
self.d1 = Dense(4, activation = 'relu')
self.d2 = Dense(100, activation = 'softmax')
def call(self,x):
x = self.conv(x)
x = self.bn(x)
x = self.RL(x)
x = self.FL(x)
x = self.d1(x)
return self.d2(x)
However, this model did not work well. The accuracy is just 1% which means it learned nothing. (I trained this model with CIFAR100 - simplicity is for just checking the code) But as I changed the code as below, it worked.
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv = Conv2D(4, (3,3), padding = 'same', activation = 'linear'
,input_shape = x_train.shape[1:])
self.bn = BatchNormalization()
# The below code is changed from ReLU() -> Activation('relu')
self.RL = Activation('relu')
self.FL = Flatten()
self.d1 = Dense(4, activation = 'relu')
self.d2 = Dense(100, activation = 'softmax')
def call(self,x):
x = self.conv(x)
x = self.bn(x)
x = self.RL(x)
x = self.FL(x)
x = self.d1(x)
return self.d2(x)
Why is it happened? I don't know the problem. Thank you for reading.