1

I'm trying to test out Keras 1DConv CNNs to help predict time series/ stock data. Something like N stocks would have OHLCV data for n time-steps. As an example, for N=1 stock, I am trying to predict only the next period's close price. Say, using one stock, I have 100 periods with OHLCV values, so X.shape = (100, 5), while y.shape = (100, 1).

I'm trying to add the layer as an input:

model.add(Conv1D(filters=50, kernel_size=7,activation='relu', data_format='channels_last',input_shape=(100,5)))

but I keep getting errors with regards to input shape (when I try to fit the data) either not being the right dimensions or passing the shape in the wrong order. I've tried reshaping the arrays to add the extra dimension but so far nothing has worked. How should I format the data, and please let me know if you have any suggestions for anything else around this task re: scaling, parameters (loss, optimizer, activation, data_format). How do channels play into this?

Cheers!

model.fit(X_train, y_train, epochs=nb_epoch, validation_data=(X_test, y_test), batch_size=16)

ValueError: Error when checking target: expected conv1d_1 to have 3 dimensions, but got array with shape (100, 5)
Unity Indigo
  • 25
  • 1
  • 4
  • The first axis of `X` (i.e. train data) refers to samples or batch dimension. Therefore, you need to reshape `X` such that it has a shape of `(num_samples, num_steps, num_feats)` which in your case would be `(num_samples, 100, 5)`. The same thing applies to `y` (i.e. labels). Further, [this answer](https://stackoverflow.com/a/52353721/2099607) might help you to understand 1D convolution better. – today Sep 24 '18 at 09:29

1 Answers1

2

The Conv1D expects a 3D input.

You have a 2D input.

If you reshape like this

x = x.reshape(batch, steps, channels)

please see link for more information.

It should work.

If you want a more detailed answer, please add the rest of your code so it is easier to reproduce.

VegardKT
  • 1,226
  • 10
  • 21
  • 1
    Thanks guys, although I've tried both of these suggestions but it still doesn't work. I get the impression this might be because I have this as my input layer. I've used X_train = X_train.reshape(1,-1,5) to get it to that shape. Keras keeps complaining that I'm feeding a 2D array after I've reshaped, e.g. I reshape it to (1,100,5) and it errors array (100,1). Also tried not entering the parameter and it still gives the same 3D vs. 2D error. The batch/ samples parameter, is that in this example like the N stocks? – Unity Indigo Sep 26 '18 at 03:19
  • Is there something I'm missing with keras with the way it takes inputs? Even runnning non Conv1D layers I keep getting dimensionality issues. – Unity Indigo Sep 26 '18 at 03:39
  • 1
    Came back because I got it working! I was trying to start right away with the Conv1d layer instead of starting with an input layer. **For anyone else out there who had the same issue**, start with an input layer so: `input_layer = Input(shape=(5, 1))` -> (OHLCV "channels" x 1 timestep I believe) THEN `conv_layer = Conv1D(filters , kernel_size, etc.)`. This link helped: (https://stackoverflow.com/questions/43396572/dimension-of-shape-in-conv1d). `scores = model.evaluate(np.expand_dims(x_test, axis=2), y_test)` – Unity Indigo Sep 28 '18 at 04:45
  • Also check out Packt Practical Time Series Analysis for model structure guidance. – Unity Indigo Sep 28 '18 at 04:51
  • A sepcific input layer should not be nescessary. specifying the input shape in the first Conv1D layer should also work. Are you sure you did not change anything else? Did you reshape your data as well? – VegardKT Sep 28 '18 at 08:36
  • I used this "full fledged example" on the bottom of this link as a guide: (https://stackoverflow.com/questions/43396572/dimension-of-shape-in-conv1d). Seems like overkill but it got it working, parsing as a tensor rather than an array. I tried every combination of input shapes I could think of. I'm trying the conv1d causal dilated model but I doubt that's the issue. Keeps throwing up either "expected to have 3 dimensions but got array of shape (100,5)" or "expected (100,5) but got (5,100) or something. Even when reshaping to match it then reverts to the 3D error. – Unity Indigo Sep 29 '18 at 20:16
  • Current functional model shapes are input = (None,5,1), zero_pad = (None,7,1), conv1d = (None, 7, 30), pool = (None, 1, 30), etc. This seems odd to me that it only parses the 5 input values, pads them, and then makes a network based on the number of filters I apply x 7, but I need to study CNNs more. I'm wondering where in this process I should layer additional sets of conv1d and how to reshape that now (e.g. before or after the pool). – Unity Indigo Sep 29 '18 at 20:30