我是python,深度学习和keras的新手。我知道以前有很多人问过类似的问题,我试图通读它们,但我的问题仍然无法解决。有人可以帮我
我想构建6个输入和1个输出模型。以下是我的代码。您的帮助或提示将不胜感激。
输入和输出形状:
print(x_train.shape, y_train.shape)
输出:
(503, 6) (503, 1)
型号代码:
inputList={}
lstmList={}
for i in range (x_train.shape[1]):
inputList[varList[i]]=Input(shape=(x_train.shape[0], 1), name=varList[i])
lstmList[varList[i]]=LSTM(64, activation='relu', return_sequences=None, dropout=0.2)(inputList[varList[i]])
z=concatenate([lstmList[i] for i in varList])
output=Dense(next_number_prediction, activation='softmax')(z)
model = Model(inputs=[inputList[i] for i in varList], outputs=[output])
model.compile(optimizer='rmsprop',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
输出为:
Layer (type) Output Shape Param # Connected to
==================================================================================================
open (InputLayer) (None, 503, 1) 0
__________________________________________________________________________________________________
high (InputLayer) (None, 503, 1) 0
__________________________________________________________________________________________________
low (InputLayer) (None, 503, 1) 0
__________________________________________________________________________________________________
close (InputLayer) (None, 503, 1) 0
__________________________________________________________________________________________________
change (InputLayer) (None, 503, 1) 0
__________________________________________________________________________________________________
pct (InputLayer) (None, 503, 1) 0
__________________________________________________________________________________________________
lstm_7 (LSTM) (None, 64) 16896 open[0][0]
__________________________________________________________________________________________________
lstm_8 (LSTM) (None, 64) 16896 high[0][0]
__________________________________________________________________________________________________
lstm_9 (LSTM) (None, 64) 16896 low[0][0]
__________________________________________________________________________________________________
lstm_10 (LSTM) (None, 64) 16896 close[0][0]
__________________________________________________________________________________________________
lstm_11 (LSTM) (None, 64) 16896 change[0][0]
__________________________________________________________________________________________________
lstm_12 (LSTM) (None, 64) 16896 pct[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 384) 0 lstm_7[0][0]
lstm_8[0][0]
lstm_9[0][0]
lstm_10[0][0]
lstm_11[0][0]
lstm_12[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 1) 385 concatenate_1[0][0]
==================================================================================================
Total params: 101,761
Trainable params: 101,761
Non-trainable params: 0
__________________________________________________________________________________________________
数据处理和模型拟合:
Data={}
for i in range (x_train.shape[1]):
Data[varList[i]]=np.expand_dims(x_train[:, i], axis=0)
Data[varList[i]]=np.reshape(Data[varList[i]], (1,x_train.shape[0],1))
model.fit(
[Data[i] for i in varList],
[y_train],
epochs=10)
,错误是
ValueError Traceback (most recent call last)
<ipython-input-21-392e0052f15a> in <module>()
1 model.fit(
2 [Data[i] for i in varList],
----> 3 [y_train],
epochs=10)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
1534 steps_name='steps_per_epoch',
1535 steps=steps_per_epoch,
-> 1536 validation_split=validation_split)
1537
1538 # Prepare validation data.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split)
990 x, y, sample_weight = next_element
991 x, y, sample_weights = self._standardize_weights(x, y, sample_weight,
--> 992 class_weight, batch_size)
993 return x, y, sample_weights
994
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _standardize_weights(self, x, y, sample_weight, class_weight, batch_size)
1167 # Check that all arrays have the same length.
1168 if not self._distribution_strategy:
-> 1169 training_utils.check_array_lengths(x, y, sample_weights)
1170 if self._is_graph_network and not context.executing_eagerly():
1171 # Additional checks to avoid users mistakenly using improper loss fns.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_utils.py in check_array_lengths(inputs, targets, weights)
424 'the same number of samples as target arrays. '
425 'Found ' + str(list(set_x)[0]) + ' input samples '
--> 426 'and ' + str(list(set_y)[0]) + ' target samples.')
427 if len(set_w) > 1:
428 raise ValueError('All sample_weight arrays should have '
ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 503 target samples.
提要输入和输出尺寸
print (Data[varList[i]].shape)
print (np.array([Data[i] for i in varList]).shape)
print (y_train.shape)
输出:
(1, 503, 1)
(6, 1, 503, 1)
(503, 1)
尝试了新代码:
input = Input(shape=(x_train.shape))
lstm = LSTM(64, activation='relu', return_sequences=True, dropout=0.2)(input)
output = Dense(1)(lstm)
model2 = Model(inputs=input, outputs=output)
model2.compile(optimizer='rmsprop',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model2.fit(x_train[np.newaxis,:,:], y_train[np.newaxis,:,:])
给出了未经训练的模型:
Epoch 1/10
1/1 [==============================] - 4s 4s/step - loss: 0.0000e+00 - acc: 0.0000e+00
Epoch 2/10
1/1 [==============================] - 0s 385ms/step - loss: 0.0000e+00 - acc: 0.0000e+00
Epoch 3/10
1/1 [==============================] - 0s 387ms/step - loss: 0.0000e+00 - acc: 0.0000e+00
Epoch 4/10
1/1 [==============================] - 0s 386ms/step - loss: 0.0000e+00 - acc: 0.0000e+00
Epoch 5/10
1/1 [==============================] - 0s 390ms/step - loss: 0.0000e+00 - acc: 0.0000e+00
Epoch 6/10
1/1 [==============================] - 0s 390ms/step - loss: 0.0000e+00 - acc: 0.0000e+00
Epoch 7/10
1/1 [==============================] - 0s 390ms/step - loss: 0.0000e+00 - acc: 0.0000e+00
Epoch 8/10
1/1 [==============================] - 0s 389ms/step - loss: 0.0000e+00 - acc: 0.0000e+00
Epoch 9/10
1/1 [==============================] - 0s 387ms/step - loss: 0.0000e+00 - acc: 0.0000e+00
Epoch 10/10
1/1 [==============================] - 0s 391ms/step - loss: 0.0000e+00 - acc: 0.0000e+00
<tensorflow.python.keras.callbacks.History at 0x7f4c97583e80>
数据的最大值和最小值为:
print (max(y_train), x_train.max(axis=0))
print (min(y_train), x_train.min(axis=0))
输出:
[0.79951533] [0.79930947 0.79750822 0.79934846 0.79951533 0.72939786 0.99697845]
[0.19443386] [1.94643871e-01 1.96481512e-01 1.94604099e-01 1.94433856e-01
2.52289062e-04 3.70721060e-01]
答案 0 :(得分:0)
您的网络期望整个序列只有一个标签。如果我修改这样的代码,它将运行:
model.fit(
[Data[i] for i in varList],
[y_train[0:1]],
epochs=10)
当然,您需要确定这是否反映了您的注意力或是否需要重组网络,以便它为序列中的每个元素都接受一个标签。
顺便说一句:这就是我构建网络的方式。因此,如果您对此不熟悉,也许这就是您真正想要的架构:
input = Input(shape=(x_train.shape))
lstm = LSTM(64, activation='relu', return_sequences=True, dropout=0.2)(input)
output = Dense(1)(lstm)
model2 = Model(inputs=input, outputs=output)
model2.compile(optimizer='rmsprop',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model2.fit(x_train[np.newaxis,:,:], y_train[np.newaxis,:,:])