我试图将线性模型应用于MNIST数据集,它可以工作,但它是'不稳定的',即每次重新运行代码都会在准确性方面产生截然不同的结果。我知道神经网络以“随机”方式学习权重,也许解决方案收敛到不同的局部最小值,但也许有某种方法可以使它更“稳定”?Keras:线性模型不稳定的结果
这里是我的模型定义:
def get_model():
w=28
h=28
input_shape = (w*h,)
model = Sequential()
model.add(Dense(n_classes, activation='linear', input_shape=input_shape))
model.add(Activation('softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
print(model.summary())
return model
更新:似乎将转正是有效的答案在精度方面的解决方案不稳定的最初的问题。
def get_model():
w=28
h=28
input_shape = (w*h,)
model = Sequential()
#v1 - Not valid, because applying softmax to one layer network is meaningless
#model.add(Dense(n_classes, init='uniform', activation='linear',
# kernel_regularizer=regularizers.l2(0.01),
# activity_regularizer=regularizers.l1(0.01),
# input_shape=input_shape))
#model.add(Dropout(0.2))
#model.add(Activation('softmax'))
#v2
model.add(Dense(n_classes, init='uniform', activation='softmax',
kernel_regularizer=regularizers.l2(0.01),
activity_regularizer=regularizers.l1(0.01),
input_shape=input_shape))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
#print(model.summary())
return model
为什么你的激活线性? –
您的模型中没有正规化 –
@Coldspeed不确定在Softmax之前是否可以用'Relu'代替'linear',无论如何'model.add(Dense(n_classes,activation ='softmax',input_shape = input_shape))'执行类似。 – mrgloom