我已经尝试过将列表从here更改为元组,并且已经阅读了this,但这没有帮助。
以下代码基于this。
from __future__ import division
from __future__ import print_function
from pandas import concat
from pandas import DataFrame
import keras
import numpy as np
import tensorflow as tf
import pandas as pd
from keras.models import Model
from keras.layers import Dense, Input, LSTM, Dropout
from keras.optimizers import Adam
from keras.losses import binary_crossentropy
#https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/
def series_to_supervised(data, n_lags=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = DataFrame(data)
cols, names = list(), list()
#input sequence t-n, ..., t-1
for i in range(n_lags, 0, -1): #for i in 3 to 0 not including 0
cols.append(df.shift(i))
names += [('var%d(t-%d)' % (j+1, i)) for j in range (n_vars)]
#forecast sequence t, t+1, ..., t+n
for i in range(0, n_out):
cols.append(df.shift(-i))
if i==0:
names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
else:
names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
#put it all together
agg = concat(cols, axis=1)
agg.columns = names
#drop rows with NaN values
if dropnan:
agg.dropna(inplace=True)
#print('agg: ', agg)
return agg
def data_gen(sample_IDs, batch_size, nn, num_features, n_lags, n_vars):
while True:
in_1 = np.empty((batch_size, n_vars), dtype=float)
in_2 = np.empty((batch_size, int(n_vars*num_features)), dtype=int)
for count in range(batch_size):
in_1[count,] = np.arange(n_vars)
in_2[count,] = np.arange(int(n_vars*num_features))
count = count + 1
in_1_s2s = np.asarray(series_to_supervised(in_1, n_lags))
in_1_data = in_1_s2s[:, :int(n_vars*n_lags)] #expect shape (29, 20*3)
target = in_1_s2s[:, int(n_vars*n_lags):] #expect shape (29, 20)
in_1_data = in_1_data.reshape((in_1_data.shape[0], n_lags, n_vars))
#Replicate in_2 over time.
in_2 = in_2[n_lags:, :] #to match in_1_data shape; expect shape (29, 100)
in_2 = np.repeat(in_2, n_lags, axis=0)
in_2 = in_2.reshape(((batch_size-n_lags), n_lags, n_vars*num_features))
yield (np.asarray(in_1), np.asarray(in_2), np.asarray(target))
if __name__=='__main__':
nn = 10
n_vars = 20
num_features = 5
n_lags = 3
batch_size = 32
sample_IDs = np.arange(1, 34) #1-32 inclusive and 2-33 inclusive; 2 batches?
steps_val = int(np.ceil(sample_IDs.shape[0]/batch_size))
my_gen = data_gen(sample_IDs, batch_size, nn, num_features, n_lags, n_vars)
#Build a toy model
adj_input = Input(shape=(n_lags, n_vars))
feat_input = Input(shape=(n_lags, num_features*n_vars))
y_input = Input(shape=(n_lags, n_vars))
concat_1 = keras.layers.concatenate([adj_input, feat_input])
dense_1 = Dense(4, activation='relu')(concat_1)
enc_1 = LSTM(4, activation='relu')(dense_1)
drop_1 = Dropout(0.2)(enc_1)
y_dense = Dense(4, name='y_dense')(y_input)
model = Model([adj_input, feat_input, y_input], [drop_1, y_dense])
print(model.summary())
#Compile and fit_generator
model.compile(optimizer=Adam(lr=0.001), loss=binary_crossentropy)
model.fit_generator(my_gen, steps_per_epoch=steps_val, shuffle=False, epochs=2, verbose=2)
打印出模型摘要后,会出现以下错误:
Traceback (most recent call last):
File "Nov26SO.py", line 113, in <module>
model.fit_generator(my_gen, steps_per_epoch=steps_val, shuffle=False, epochs=2, verbose=2)
File "/user/pkgs/anaconda2/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/user/pkgs/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1418, in fit_generator
initial_epoch=initial_epoch)
File "/user/pkgs/anaconda2/lib/python2.7/site-packages/keras/engine/training_generator.py", line 217, in fit_generator
class_weight=class_weight)
File "/user/pkgs/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1211, in train_on_batch
class_weight=class_weight)
File "/user/pkgs/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 751, in _standardize_user_data
exception_prefix='input')
File "/user/pkgs/anaconda2/lib/python2.7/site-packages/keras/engine/training_utils.py", line 102, in standardize_input_data
str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), but instead got the following list of 1 arrays: [array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
13., 14., 15., 16., 17., 18., 19.],
[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
1...
问题:为什么我不将三个numpy数组传递给模型?我以为yield语句会产生三个numpy数组。
编辑:
这与问题here不同,尽管我很感谢指针。我遇到的问题是我有三个输入:in_1,in_2和目标。当我将yield
行更改为yield [np.asarray(in_1), np.asarray(in_2), np.asarray(target)]
时,会遇到完全相同的问题。