python学习日记18keras训练发生了奇怪的事情

使用

train_history = model.fit(x=x_train_normalize,
y=y_trainOnehot,
validation_split = 0.2,
epochs=10,
batch_size=200,
verbose=1,
validation_data=(x_test_normalize,y_testOnehot))

训练,因为在anaconda中,

Layer (type) Output Shape Param #
===========================================================
dense (Dense) (None, 256) 200960


dense_1 (Dense) (None, 10) 2570
===========================================================

不会自动清理变量内存,加入了tf.reset_default_graph()之后,连执行结果都变了
另外理论上应该train的精度高误差小,但是实际执行的结果却是相反的,test效果更好。如果epoch增加的20-25,在最后train才会有微弱优势。
keras官网有说明,https://keras.io/getting-started/faq/#why-is-the-training-loss-much-higher-than-the-testing-loss

Why is the training loss much higher than the testing loss?
A Keras model has two modes: training and testing. Regularization mechanisms, such as Dropout and L1/L2 weight regularization, are turned off at testing time.
Besides, the training loss is the average of the losses over each batch of training data. Because your model is changing over time, the loss over the first batches of an epoch is generally higher than over the last batches. On the other hand, the testing loss for an epoch is computed using the model as it is at the end of the epoch, resulting in a lower loss.

对keras内部略有困惑
需要用tensorflow做出同样的分析图,然后对比,才能确定是数据本身的原因,还是训练方法的原因。
尝试tensorflow和keras做同样的训练后,考虑到过拟合时,train比test效果好。同时有如下现象:
1keras不太稳定,不用tf.reset_default_graph(),acc和loss曲线比较平滑。用了之后有些明显的折线。有时修改了图的隐层tensor数量,报错。修改回来也仍然报错,清除图和变量也没用。重新启动spyder,就会有正常结果。
2在对比tensorflow和keras时,二者使用的层数和tensor数即使相同,结果趋势也有明显差异。选择时要综合考虑开发速度和可控性。
3有些训练结果是train优于test,和epoch也有关系。二者逐渐趋近。
结果如图举例
在这里插入图片描述
在这里插入图片描述
参考文档:https://keras.io/models/sequential/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值