1

I am a beginner in machine learning. Although, this question is similar to 1, 2, 3, but I am really confused in selecting the input shape for my data. I am using 1-D CNN on time series data. The dimension of data is (6400, 4). There are 4 features (columns) out of which one is target variable. After splitting:

dim(xtrain) -> 5000, 3
dim(ytrain) -> 5000, 1
dim(xtest) -> 1400, 3
dim(ytest) -> 1400, 1

I am confused in selecting the input shape for CNN. This is what I have tried (I have kept input shape = c(3, 1)):

model = keras_model_sequential() %>%
  layer_conv_1d(filters = 64, kernel_size = 2,
                input_shape = c(3, 1), activation = "relu") %>%
  layer_max_pooling_1d(pool_size = 2) %>%
  layer_flatten() %>%
  layer_dense(units = 32, activation = "relu") %>%
  layer_dropout(rate = 0.2) %>%
  layer_dense(units = 1, activation = "linear")

xtrain <- as.matrix(train[, c(1, 2, 3)])
ytrain <- as.matrix(train[, c(4)])
xtest = as.matrix(test[, c(1, 2, 3)])
ytest = as.matrix(test[, c(4)])

# Transforming 2-D matrix into 3-D matrix
xtrain = array(xtrain, dim = c(nrow(xtrain), 3, 1))
xtest = array(xtest, dim = c(nrow(xtest), 3, 1))

# fitting model
model %>% fit(xtrain, ytrain, epochs = 50, batch_size = 128, verbose = 1, validation_split = 0.20)

This executes well, but I am not sure whether it is correct or not. Please tell if this correct way to set the input shape.

Anup
  • 11
  • 2

1 Answers1

0

You did some kind of moving average analogy there. At the end it all depends on what you wanted to achieve and the nature of your time series.

Lets say your kernel weights are c(0.3,0.7), and your time series is c(1.2,1.3,1.25).

Your kernel would do 2 dim vector:

a = c( 0.3 * 1.2 + 0.7*1.3 , 0.3 * 1.3 + 0.7*1.25)

You can see similarity to moving averages here, then you would apply maximum and do RELU on it. Which in the example would return a[1].

Now if you believe that you can extract everything that there is from time series with pretty much linear transformations (ignoring the rest of the model), then it is OK. But if you believe that there may be some non-linear relations in your short time-series, perhaps try RNN or even LSTM.

On the technical view, if you have instance_1 = (1,2,3,4), instance_2 = (2,3,4,5) and so on, you want to do overlapping to create a target variable, which I think you did in some way. Again depends on what you are after.

JacobJacox
  • 917
  • 5
  • 14