0

I try to create the samples, which are X_train and y_train.

Both of samples are similar to format of my real data.

And the codes is that I uses.

Here are my codes:

import matplotlib.pyplot as plt
import numpy as np
import time
import csv
import keras
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.layers.core import Masking
from keras.layers.wrappers import TimeDistributed
from openpyxl import load_workbook
from datetime import datetime

X_arryA = np.array([[1, 2],[3, 8],[9, 10],[6, 7]])
X_arryB = np.array([[1, 2],[3, 8]])
X_arryC = np.array([[1, 2],[3, 8],[9, 10],[6, 7],[9, 10],[6, 7]])
X_train = np.array([X_arryA,X_arryB,X_arryC])
y_arryA = np.array([1,5,3,4])
y_arryB = np.array([2,1])
y_arryC = np.array([6,7,4,2,3,1])
y_train = np.array([y_arryA,y_arryB,y_arryC])
model = Sequential()
layers = [2, 50, 100, 1]
model.add(LSTM(
    input_shape=(None, 2),
    output_dim=layers[1],
    return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(
    layers[2],
    return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(
    output_dim=layers[3]))
model.add(Activation("linear"))

start = time.time()
model.compile(loss="mse", optimizer="rmsprop")
#print "Compilation Time : ", time.time() - start
model.summary()
model.fit(X_train, y_train, batch_size=1, nb_epoch=1, validation_split=0.05)

I have check the model.summary().

I think the structure is be okay.

And some messages showed:

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\ipykernel_launcher.py:14: UserWarning: Update your `LSTM` call to the Keras 2 API: `LSTM(units=50, input_shape=(None, 2), return_sequences=True)`

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_77 (LSTM)               (None, None, 50)          10600     
_________________________________________________________________
dropout_65 (Dropout)         (None, None, 50)          0         
_________________________________________________________________
lstm_78 (LSTM)               (None, 100)               60400     
_________________________________________________________________
dropout_66 (Dropout)         (None, 100)               0         
_________________________________________________________________
dense_36 (Dense)             (None, 1)                 101       
_________________________________________________________________
activation_33 (Activation)   (None, 1)                 0         
=================================================================
Total params: 71,101
Trainable params: 71,101
Non-trainable params: 0
_________________________________________________________________
C:\Users\user\Anaconda3\envs\py35\lib\site-packages\ipykernel_launcher.py:23: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(units=1)`
C:\Users\user\Anaconda3\envs\py35\lib\site-packages\keras\models.py:848: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
  warnings.warn('The `nb_epoch` argument in `fit` '
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-509-c6f954bdb474> in <module>()
     28 #print "Compilation Time : ", time.time() - start
     29 model.summary()
---> 30 model.fit(X_train, y_train, batch_size=1, nb_epoch=1, validation_split=0.05)

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\keras\models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
    865                               class_weight=class_weight,
    866                               sample_weight=sample_weight,
--> 867                               initial_epoch=initial_epoch)
    868 
    869     def evaluate(self, x, y, batch_size=32, verbose=1,

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\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, **kwargs)
   1520             class_weight=class_weight,
   1521             check_batch_axis=False,
-> 1522             batch_size=batch_size)
   1523         # Prepare validation data.
   1524         do_validation = False

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
   1376                                     self._feed_input_shapes,
   1377                                     check_batch_axis=False,
-> 1378                                     exception_prefix='input')
   1379         y = _standardize_input_data(y, self._feed_output_names,
   1380                                     output_shapes,

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    130                                  ' to have ' + str(len(shapes[i])) +
    131                                  ' dimensions, but got array with shape ' +
--> 132                                  str(array.shape))
    133             for j, (dim, ref_dim) in enumerate(zip(array.shape, shapes[i])):
    134                 if not j and not check_batch_axis:

ValueError: Error when checking input: expected lstm_77_input to have 3 dimensions, but got array with shape (3, 1)

I spent 5 hours to solve the question, but it still doesn't work.

Any help. I appreciate it.

Simon
  • 79
  • 1
  • 7

1 Answers1

1

LSTM layers only accept shapes like (numberOfSequences, numberOfSteps, featuresPerStep)

These are the 3 dimensions expected that were mentioned in the error message. You need to prepare your data properly to fit those dimensions.

The problem is that numpy arrays cannot accept variable sizes. It must be a well defined matrix.

When you give 3 different length X_arry to a numpy array, the result is impossible to fit a numpy array, then it makes an array of arrays instead. (Keras can't handle this, it expects a single array).

Using variable length, you will have to either pad each array with dummy values and add a masking layer, or simply train each length individually.

X_arryLen4 = np.asarray([[[1, 2],[3, 8],[9, 10],[6, 7]]])
X_arryLen2 = np.asarray([[[1, 2],[3, 8]]])
X_arryLen6 = np.asarray([[[1, 2],[3, 8],[9, 10],[6, 7],[9, 10],[6, 7]]])

model.fit(X_arryLen4, .....)
model.fit(X_arryLen2, .....)
model.fit(X_arryLen6, .....)

Answers that may help:

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • Therefore, I have to put the same size, including column and row, in each samples? Right? – Simon Oct 04 '17 at 03:49
  • In each batch, yes. But you can have different batches, each batch with a "number of steps". – Daniel Möller Oct 04 '17 at 03:51
  • Or three way to solve, (1)pad each array with dummy values (2)masking layer (3) train each length individually. Do I understand correct? Thanks much. – Simon Oct 04 '17 at 03:51
  • Can I give the different "number of steps (NOF)" for each batch? Ex: 1st batch shape is (NOF=10, FPS=2), 2nd batch shape is (NOF=4, FPS=2)...and so on. FPS is featuresPerStep. – Simon Oct 04 '17 at 04:01
  • Yes, you can. :) - But the batches need shape `(NumberOfVideos, NOF, FeaturesPerStep)`. Both `NumberOfVideos` and `NOF` can be different for each batch. – Daniel Möller Oct 04 '17 at 04:18
  • 2 ways to solve, padding and masking should be together as one solution. The other solution is to separate batches. – Daniel Möller Oct 04 '17 at 04:19
  • I wonder that can I put different NOF in the only one batches? That each batch with different NOF are trained together without the above two solutions. Can I? – Simon Oct 04 '17 at 08:24
  • Just like my questions. The X_train are batches that the shape is (3, None, 2). The first batch is X_arryA that the shape is (4, 2) and the 2nd batch is X_arryB that the shape is (2, 2). And so on for X_arryC. Could I trained together without the above two way? Thank you for your help. – Simon Oct 04 '17 at 08:31
  • If you make all of them with shape (4,2) by adding dummy values at the end, and then add a Masking layer before everything, it's possible. – Daniel Möller Oct 04 '17 at 11:00
  • Thanks a lot. It seems that each batch in the batches should have the same shape. – Simon Oct 04 '17 at 12:28