我正在尝试基于Keras的密集网络构建一个分类器。我的输入是(26,1)的向量,我希望输出为二元分类,1或0。
通过使用密集网络并结合hyperas进行一些优化,我达到了80%的准确率,这还不错,但我试图通过使用残差网络来提高网络的准确率。
我在各种论坛上找到了许多关于卷积网络的残差网络示例,但我没有找到关于残差网络的示例。
我尝试了以下代码来生成残差网络:
Input=keras.Input(shape=(X_train.shape[1],))x1=layers.Dense(512,activation='relu')(Input)x1=layers.Dropout(0.5)(x1)x1=layers.Dense(128,activation='relu')(x1)x1=layers.Dropout(0.4)(x1)# x1=layers.Dense(512,activation='relu')(x1)# x1=layers.Dropout(0.3)(x1)x2=layers.Dense(128,activation='relu')(Input)x2=layers.Dropout(0.5)(x2)added=layers.Add()([x1,x2])#pour faire du ResNetx3=layers.Dense(128,activation='relu')(added)final=layers.Dense(1,activation='sigmoid')(x3)model=Model(Input,final)model.compile(optimizer='RMSprop',loss='binary_crossentropy',metrics=['accuracy'])history=model.fit(X_train,Y_train,validation_data=(X_valid,Y_valid),epochs=100,batch_size=128,class_weight=class_weight)loss = history.history['acc']val_loss=history.history['val_acc']epochs=range(1,len(loss)+1)plt.plot(epochs,loss,'bo',label='Training accuracy')plt.plot(epochs,val_loss,'b',label='Validation accuracy')plt.title('Training and validation accuracy')plt.legend()plt.show()
我尝试了各种epoch的训练,但网络的准确率没有超过75%,这比之前还差。当然,我仍然可以使用hyperas再次提高准确率并调整超参数,但我最初期望的性能会更好一些。
问题:
- 我的代码中有没有缺陷?
- 有没有更好的方法来生成残差网络?主要是,我添加了一个跳跃层(仍然通过一个密集层),我能做得更好吗?我应该包含更多吗?
感谢您的建议
回答:
首先,残差网络是为更深层次的网络设计的。你的网络看起来太浅,无法获得最大的adding
优势。
更一般地说,残差网络是像VGGNet这样简单架构的进化版本,旨在能够“深入”。这并不意味着如果网络太浅,残差层总是会提高你的准确率。
增加更多的层应该会有所帮助。关键思想是通过允许跳跃连接来避免网络更深层中的梯度消失问题。