数据不适合



谁能告诉我为什么我的神经网络不适合ys当我测试任何值在x ?有人能帮我画一下吗

import tensorflow as tf
import numpy as np
from tensorflow import keras
keras.layers.Dense(10, activation="relu", name="layer7"),
keras.layers.Dense(5, activation="relu", name="layer8"),
keras.layers.Dense(1, name="layer9"),
])
model.compile(optimizer='RMSprop',loss='mean_squared_error',metrics=['mae'])
a= float(input("enter the wanted value to convert : "))
xs=np.array([443707.401,455897.072,
396833.899,407426.699,435646.069,
419953.188,436349.443,633372.629,
572704.102,506379.29,596808.359,
622705.893,521749.843,500965.861,
558482.399,672648.564,739873.87,
459092.199,485007.612,579586.959,
509713.739,725009.687,727394.13,
658740.26,485686.823,461640.1,502495.219,
625584.252,680222.202,760907.585,
738381.47,712415.6], dtype=float)
ys=np.array([443417.925,455608.023,
397588.538,407135.987,435356.29,
419662.842,436059.613,633089.564,
572418.983,506091.937,596524.026,
622422.445,521463.004,500678.296,
558196.649,672366.797,739594.302,
458803.188,484719.43,579302.029,
509426.339,724729.561,727114.01,
658457.788,485398.691,461351.171,
502207.616,625300.699,679940.373,
760628.252,738101.41,712134.732],dtype=float)
model.fit(xs,ys,epochs=298,verbose=1)
result= (model.predict([a]))
print(result)

假设我们有xs和ys,那么我假设你的代码看起来像:

import tensorflow as tf, numpy as np
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(10,'relu'),
tf.keras.layers.Dense(5,'relu'),
tf.keras.layers.Dense(1),
])
model.compile('RMSprop','mse',metrics=['mae'])
model.fit(xs,ys,epochs=298,verbose=1)

这是非常不可能收敛到你正在寻找的答案。特别是,你的数据很容易在两个变量中拟合线性方程。这个模型有81个随机变量,它们正在慢慢被调整以试图降低MSE。81维的情节丝毫不关心是否存在一个单一(最佳)的解决方案。它只知道随机变量的轻微调整可以稍微降低MSE,如果它能平衡数据间的误差。因此,该模型收敛到预测所有输入的完全相同的值。

在运行上面的模型并等待它完成训练之后,您可以探索您的模型。例如,

model.summary()                  #print the summary of the layers and variables
print(model.predict(xs))         #print all the predictions for all the xs
import matplotlib.pyplot as plt
plt.plot(xs,ys)                  #Graph what your current data looks like
plt.plot(xs,model.predict(xs))   #Graph what your model is predicting

一般来说,张量流模型不打算找到单一的精确解;它的目的是估计一个可能的解决方案,它可能在预测方面做得很好,也可能做得不好。一般来说,keras模型需要处理10,000个变量,以尝试找到预测问题的近似答案。与使用直接公式形成鲜明对比的是:

import scipy.stats
scipy.stats.linregress(xs,ys) 

上面的精确公式找到产生最小均方误差的确切斜率和y-int。

最新更新