1
$\begingroup$

I'm making a CNN-LSTM model to forecast multivariate time series:

       model = Sequential()
       #model.build((10,7,1))
       model.add(Conv1D(filters=64,  kernel_size=2, activation='relu',input_shape=(10,7),strides=1))
       model.add(Conv1D(filters=128, kernel_size=2, activation='relu',strides=1))
       model.add(MaxPooling1D(pool_size=2))
       #model.add(Flatten())
       model.add(LSTM(200,return_sequences=True, activation='relu', recurrent_activation="sigmoid"))
       model.add(Dense(32, activation='sigmoid'))
       model.add(Dense(1))
       model.compile(optimizer='RMSprop', loss='mse',metrics=['accuracy'])
       model.summary()
       print('a new model has been created')

I have as input 7 features ("Time series") and a single output.

I made a function (make_samples to sample the data into sliding window size 10 in code called as n_steps

def make_samples(self,file, n_steps):
    X, y = list(), list()
    data=pd.read_csv(file)
    for i in range(len(data)):
        # find the end of this pattern
        end_ix = i + n_steps
        # check if we are beyond the dataset
        if end_ix > len(data):
            break
        # gather input and output parts of the pattern
        seq_x = data[self.lista].values[i:end_ix]
        seq_y = data["Volume"].values[end_ix-1]
        X.append(seq_x)
        y.append(seq_y)

    return array(X).astype("float32"),array(y).astype("float32")

When I pass this data to the model I got the following error:

Error when checking target: expected dense_30 to have 3 dimensions, but got array with shape (659, 1))

The question is, why does this error arise? And, how do I go about fixing this?

Here is the summary of

_________________________________________________________________
Layer (type)                 Output Shape              Param    

conv1d_38 (Conv1D)           (None, 9, 64)             960       
_________________________________________________________________
conv1d_39 (Conv1D)           (None, 8, 128)            16512     
_________________________________________________________________
max_pooling1d_18 (MaxPooling (None, 4, 128)            0         
_________________________________________________________________
lstm_18 (LSTM)               (None, 4, 200)            263200    
_________________________________________________________________
dense_29 (Dense)             (None, 4, 32)             6432      
_________________________________________________________________
dense_30 (Dense)             (None, 4, 1)              33        

Many thanks in advance

$\endgroup$
1
  • 1
    $\begingroup$ Can you please show us a sample of the data after the make_sampels function. $\endgroup$
    – JahKnows
    Commented Jul 11, 2020 at 13:19

1 Answer 1

0
$\begingroup$

You might want to try to use the WindowGenerator class from TensorFlow documentation:

class WindowGenerator():
  def __init__(self, input_width, label_width, shift,
               train_df=train_df, val_df=val_df, test_df=test_df,
               label_columns=None):
    # Store the raw data.
    self.train_df = train_df
    self.val_df = val_df
    self.test_df = test_df

    # Work out the label column indices.
    self.label_columns = label_columns
    if label_columns is not None:
      self.label_columns_indices = {name: i for i, name in
                                    enumerate(label_columns)}
    self.column_indices = {name: i for i, name in
                           enumerate(train_df.columns)}

    # Work out the window parameters.
    self.input_width = input_width
    self.label_width = label_width
    self.shift = shift

    self.total_window_size = input_width + shift

    self.input_slice = slice(0, input_width)
    self.input_indices = np.arange(self.total_window_size)[self.input_slice]

    self.label_start = self.total_window_size - self.label_width
    self.labels_slice = slice(self.label_start, None)
    self.label_indices = np.arange(self.total_window_size)[self.labels_slice]

  def __repr__(self):
    return '\n'.join([
        f'Total window size: {self.total_window_size}',
        f'Input indices: {self.input_indices}',
        f'Label indices: {self.label_indices}',
        f'Label column name(s): {self.label_columns}'])
```
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.