Car*_*oen 5 python neural-network lstm keras tensorflow
我是神经网络的新手,有两个,可能是非常基本的问题.我正在建立一个通用的LSTM网络,以根据多个功能预测序列的未来.因此,我的训练数据具有形状(训练序列的数量,每个序列的长度,每个时间步长的特征量).或者使它更具体,类似于(2000,10,3).我试图预测一个特征的价值,而不是所有三个特征的价值.
如果我使我的网络更深和/或更宽,我得到的唯一输出是要预测的值的常数平均值.以此设置为例:
z0 = Input(shape=[None, len(dataset[0])])
z = LSTM(32, return_sequences=True, activation='softsign', recurrent_activation='softsign')(z0)
z = LSTM(32, return_sequences=True, activation='softsign', recurrent_activation='softsign')(z)
z = LSTM(64, return_sequences=True, activation='softsign', recurrent_activation='softsign')(z)
z = LSTM(64, return_sequences=True, activation='softsign', recurrent_activation='softsign')(z)
z = LSTM(128, activation='softsign', recurrent_activation='softsign')(z)
z = Dense(1)(z)
model = Model(inputs=z0, outputs=z)
print(model.summary())
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
history= model.fit(trainX, trainY,validation_split=0.1, epochs=200, batch_size=32,
callbacks=[ReduceLROnPlateau(factor=0.67, patience=3, verbose=1, min_lr=1E-5),
EarlyStopping(patience=50, verbose=1)])
Run Code Online (Sandbox Code Playgroud)
如果我只使用一层,如:
z0 = Input(shape=[None, len(dataset[0])])
z = LSTM(4, activation='soft sign', recurrent_activation='softsign')(z0)
z = Dense(1)(z)
model = Model(inputs=z0, outputs=z)
print(model.summary())
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
history= model.fit(trainX, trainY,validation_split=0.1, epochs=200, batch_size=32,
callbacks=[ReduceLROnPlateau(factor=0.67, patience=3, verbose=1, min_lr=1E-5),
EarlyStopping(patience=200, verbose=1)])
Run Code Online (Sandbox Code Playgroud)
预测有点合理,至少它们不再是常数.
为什么会这样?大约2000个样本并不多,但在过度拟合的情况下,我希望预测完全匹配......
我用的时候:
`test=model.predict(trainX[0])`
Run Code Online (Sandbox Code Playgroud)
为了获得第一个序列的预测,我得到一个维度错误:
"检查时出错:预期input_1有3个维度,但得到的数组有形状(3,3)"
我需要输入一系列序列,如:
`test=model.predict(trainX[0:1])`
Run Code Online (Sandbox Code Playgroud)
这是一种解决方法,但我不确定,这是否有更深层次的含义,或者只是一种语法...
这是因为您尚未标准化输入数据。
任何神经网络模型最初都会将权重归一化在零附近。由于您的训练数据集具有所有正值,因此模型将尝试调整其权重以仅预测正值。然而,激活函数(在你的例子中是softsign)会将其映射到1。因此模型除了添加偏差之外什么也做不了。这就是为什么你会在数据集的平均值周围得到一条几乎恒定的线。
为此,您可以使用sklearn等通用工具来预处理数据。如果您使用 pandas 数据框,这样的东西会有所帮助
data_df = (data_df - data_df.mean()) / data_df.std()
Run Code Online (Sandbox Code Playgroud)
或者为了让模型中有参数,您可以考虑向模型添加批量归一化层