Tensorflow 损失收敛但模型甚至无法预测训练数据
Tensorflow loss converging but model fails to predict even on train data
使用 ANN 和 Tensorflow 训练一个简单的已知方程 Y=Sin(X) 或 Y=Cos(X)。我的损失函数收敛得很好。
Loss function convergence graph。如果损失函数收敛,则表示模型已经很好地拟合了我的训练数据集。
但是,当我预测传入参数训练集本身时,模型甚至无法预测奇怪的训练数据。
Here it can be seen that after 200th value there model shows no training at all
如果损失已经收敛,那么模型应该完美地适合训练数据集,但这里并没有发生这种情况。我的代码有什么问题?
X = np.linspace(0,10*np.pi,1000)
Y = np.sin(X)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(500,input_shape=(1,),activation='relu'))
model.add(tf.keras.layers.Dense(1))
opt = tf.keras.optimizers.Adam(0.01)
model.compile(optimizer=opt,loss='mse')
r= model.fit(X.reshape(-1,1),Y,epochs=100)
plt.plot(r.history['loss'])
Yhat = model.predict(X.reshape(-1,1)).flatten()
plt.plot(Y)
plt.plot(Yhat)
这是您数据的性质。
It made me remember the old paper which showed that the ANN can't compute even the XOR
无论如何,这里的原因是你的模型很浅,浅层网络的效率远低于深层网络。透视如下模型
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(20,input_shape=(1,),activation='relu'))
model.add(tf.keras.layers.Dense(20,activation='relu'))
model.add(tf.keras.layers.Dense(1))
即使它只有 1/3
原始模型的参数,也可能会表现得更好,这是因为你走得越深,模型可以创建的表示就越复杂。要记住的核心是
THE DEEP LEARNING MODEL DON'T BUILD NON-LINEAR DECISION BOUNDARIES as EACH AND EVERY
UNIT IS FUNDAMENTALLY DESIGNED TO CREATE SOME LINEAR DECISION BOUNDARY. so what does
it do? IT FROM STACKING THOSE LINEAR DECISION BOUNDARIES MAKE A REPRESENTATION OF
DATA WHICH IS LINEARLY SEPARABLE.
另外,最重要的是了解你的数据。在这种情况下,使用 Probabilistic Models
将给出近乎完美的结果。您可以使用 TensorFlow probability
.
轻松实现这些
使用 ANN 和 Tensorflow 训练一个简单的已知方程 Y=Sin(X) 或 Y=Cos(X)。我的损失函数收敛得很好。 Loss function convergence graph。如果损失函数收敛,则表示模型已经很好地拟合了我的训练数据集。
但是,当我预测传入参数训练集本身时,模型甚至无法预测奇怪的训练数据。 Here it can be seen that after 200th value there model shows no training at all 如果损失已经收敛,那么模型应该完美地适合训练数据集,但这里并没有发生这种情况。我的代码有什么问题?
X = np.linspace(0,10*np.pi,1000)
Y = np.sin(X)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(500,input_shape=(1,),activation='relu'))
model.add(tf.keras.layers.Dense(1))
opt = tf.keras.optimizers.Adam(0.01)
model.compile(optimizer=opt,loss='mse')
r= model.fit(X.reshape(-1,1),Y,epochs=100)
plt.plot(r.history['loss'])
Yhat = model.predict(X.reshape(-1,1)).flatten()
plt.plot(Y)
plt.plot(Yhat)
这是您数据的性质。
It made me remember the old paper which showed that the ANN can't compute even the XOR
无论如何,这里的原因是你的模型很浅,浅层网络的效率远低于深层网络。透视如下模型
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(20,input_shape=(1,),activation='relu'))
model.add(tf.keras.layers.Dense(20,activation='relu'))
model.add(tf.keras.layers.Dense(1))
即使它只有 1/3
原始模型的参数,也可能会表现得更好,这是因为你走得越深,模型可以创建的表示就越复杂。要记住的核心是
THE DEEP LEARNING MODEL DON'T BUILD NON-LINEAR DECISION BOUNDARIES as EACH AND EVERY UNIT IS FUNDAMENTALLY DESIGNED TO CREATE SOME LINEAR DECISION BOUNDARY. so what does it do? IT FROM STACKING THOSE LINEAR DECISION BOUNDARIES MAKE A REPRESENTATION OF DATA WHICH IS LINEARLY SEPARABLE.
另外,最重要的是了解你的数据。在这种情况下,使用 Probabilistic Models
将给出近乎完美的结果。您可以使用 TensorFlow probability
.