按类型输入数据
x_train = array([[1.3590000e+03, 1.3180000e+03, 1.7082020e+07, 1.2000000e+03], [4.0380000e+03, 4.6170000e+03, 1.7082020e+07, 1.2000000e+03], [2.6300000e+03, 3.9840000e+03, 1.7082020e+07, 1.0540000e+03], [3.4460000e+03, 4.5310000e+03, 1.8102014e+07, 2.1610000e+03], [9.1500000e+02, 4.5310000e+03, 1.8102014e+07, 2.1610000e+03], [3.4460000e+03, 4.4570000e+03, 1.8102014e+07, 2.1610000e+03]])
y_train = array([[ 1., 2., 2.], [ 1., 2., 2.], [ 1., 2., 2.], [16., 2., 1.], [16., 4., 1.], [16., 0., 1.]])
но результат [[1616849.4 1550158.4 54270.72 ]
[1616645. 1549958.6 54247.965]
[1530179.1 1467063. 51288.938]
[1529552.4 1466477.8 51272.31 ]
[1531608.9 1468432.5 51389.453]
[1529679.2 1466598. 51271.816]
[1529510.4 1466437.1 51267.277]
[1531926. 1468735.9 51406.277]
[1529213.1 1466153.1 51250.47 ]
[1529175.5 1466111.1 51226.37 ]
[1529424.9 1466356.1 51264.93 ]
[1531726. 1468537.9 51373.17 ]
[1531182.4 1468027.1 51378.06 ]
[1532010.6 1468815.6 51411.83 ]
[1531546.9 1468376.6 51398.44 ]
[1531727.9 1468543.2 51385.023]
[1447227.9 1387525.2 48541.914]
[1447429.9 1387724.8 48575.203]
[1447145.4 1387455.6 48571.164]
[1444912. 1385329.5 48432.594]
[1446700.2 1387032. 48556.8 ]
[1446735.2 1387061.6 48540.684]
[1445760.2 1386125.1 48466.57 ]]# как обучить нейроную сеть близку к результату y_train ?
编码:
x_train,y_train = np.load('datsx.npy'),np.load('datsy.npy')
wb = None
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
import h5py
# Среднее значение
mean = x_train.mean(axis=0)
# Стандартное отклонение
std = x_train.std(axis=0)
x_train -= mean
x_train /= std
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(x_train.shape[1],)))#shape 1
model.add(Dense(100))
model.add(Dense(100))
model.add(Dense(3))
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
model.fit(x_train, y_train, epochs=100, batch_size=1, verbose=2)
x_test, y_test = importdata()
mse, mae = model.evaluate(x_test, y_test, verbose=0)
print("Средняя абсолютная ошибка (тысяч долларов):", mae)
pred = model.predict(x_test)
print("Предсказанная стоимость:", pred*-1, ", правильная стоимость:", y_test)
所以你正在规范化
x_train
,x_test
而不是规范化。训练和测试输入数据的预处理必须匹配,否则事实证明是无稽之谈。是否有必要再计算
mean
一遍std
是x_text
个不小的问题,但理论上,如果数据来自同一个普通人群,这些值应该相差不大。通常,为了变换测试数据,使用在训练数据上“训练”过的变换函数,也就是说,它们不需要重新计算。但你可以尝试任何一种方式。我不排除你已经在函数中进行了规范化
importdata
,但是从上面的代码中是不可能发现的。虽然,从你的预测来看,你肯定没有对测试数据进行标准化。另外,不清楚您为什么要输入
pred*-1
(实际上是-pred
),而不仅仅是pred
.我在规范化的版本中对其进行了测试
x_test, y_test = x_train.copy(), y_train.copy()
,我得到了一个可以容忍的错误:如果您增加 epoch 的数量(以提高准确性)和批量大小(以提高计算速度),那么结果通常非常好(尽管每次运行时变化很大):