我一开始做了一个模型如下:
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)
但是,这个模型效果不佳。准确率只有 1%,这意味着它什么也没学到。(我用 CIFAR100 训练了这个模型 - 简单只是为了检查代码)但是当我如下更改代码时,它起作用了。
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)
为什么会这样?我不知道问题所在。感谢您的阅读。