TensorFlow2.x 学习笔记(八)过拟合

过拟合与欠拟合

Model capacity

当模型的复杂度增强时,模型的表达性也随之增强,以多项式为例
y = β 0 + β 1 x + β 2 x 2 + . . . + β n x n + ϵ y = \beta_0 + \beta_1x + \beta_2x^2 + ... + \beta_nx^n + \epsilon y=β0+β1x+β2x2+...+βnxn+ϵ
随着阶数的增加,可表示的模型也就越多,模型的表示能力也就越强。

Underfitting

  • model capacity < ground truth
  • train loss/acc is bad
  • test loss/acc is bad as well

Overfitting

  • model capacity < ground truth
  • train loss/acc is good
  • test loss/acc is bad
  • generalization performance is bad

detect and reduce

Splitting

  • Train/Val/Test Set
(x, y), (x_test, y_test) = datasets.mnist.load_data()

x_train, x_val = tf.split(x, num_or_size_splits=[50000, 10000])
y_train, y_val = tf.split(y, num_or_size_splits=[50000, 10000])

K-fold cross-validation

  • randomly sample 1/k as val dataset
network.fit(tf.cast(x, dtype=tf.float32)/255., tf.one_hot(tf.cast(y, dtype=tf.int32), depth=10), batch_size=64, epochs=10, validation_split=0.1, validation_freq=2)

这里要注意,validation_split参数是不能用于以dataset格式的输入的
官网所述

The argument validation_split (generating a holdout set from the training data) is not supported when training from Dataset objects, since this features requires the ability to index the samples of the datasets, which is not possible in general with the Dataset API.

更多内容,参见官网训练与评估

Reduce

  • More Data
  • Consttraint model complexity
    • Shallow
    • Regularization
  • Dropout
  • Data augmentation
  • Early Stopping
Regularization

L 1 : J ( θ ) = 1 m ∑ l o s s + λ ∑ i = 1 n ∣ θ i ∣ L_1:J(\theta) = \frac{1}{m}\sum{loss} + \lambda\sum\limits_{i=1}^{n}{|\theta_i|} L1:J(θ)=m1loss+λi=1nθi
L 1 : J ( θ ) = 1 m ∑ l o s s + λ ∑ i = 1 n θ i 2 L_1:J(\theta) = \frac{1}{m}\sum{loss} + \lambda\sum\limits_{i=1}^{n}{\theta_i^2} L1:J(θ)=m1loss+λi=1nθi2

layers.Dense(256, kernel_regularizer=keras.regularizers.l2(0.001), activation='relu'),
Early Stopping

在验证集上达到峰值的时候就停下

Dropout

随机断连

layers.Dropout(0.5)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值