Categories
Misc

Model was constructed with shape (1, 16, 1), but it was called on an input with incompatible shape (1, 1, 1)

I’m new to deep learning and I trying to model a univariate time series using the sliding window approach with a LSTM model. My training dataset takes 16 values to predict the next 16. My code is writing in R.

I am getting a warning and I cannot understand what I am doing wrong. I think that the problem is when specifying the model.

I am totally new to this. So if you could help me would be great.

Bellow is the whole code. I got the warning at the very end, after predicting

train_sliding = create_dataset(data = kt_train_male_scaled, n_input = 16, n_out = 16)

X_train = train_sliding[[1]] #97, 16

y_train = train_sliding[[2]] #97, 16

#Array transformation to Keras LSTM

dim(X_train) = c(dim(X_train), 1)

dim(X_train) # 97, 16, 1

I think the problem should be in this chunk of code. I think I am building the model wrong

#Model in Keras

X_shape2 = dim(X_train)[2] #16

X_shape3 = dim(X_train)[3] #1

batch_size = 1

model <- keras_model_sequential()

model%>%

layer_lstm(units = 64, activation = “relu”, batch_size = batch_size, input_shape = c(dim(X_train)[2], dim(X_train)[3]),stateful= TRUE)%>%

#layer_lstm(units = 5, activation = “relu”, stateful= TRUE) %>%

layer_dense(units = 1)

summary(model)

model %>% compile(

loss = ‘mse’,

optimizer = optimizer_adam(lr= 0.01, decay = 1e-6 ),

metrics = c(‘mae’)

)

Epochs = 100

for(i in 1:Epochs ){

model %>% fit(X_train, y_train, epochs=1, batch_size=batch_size, verbose=1, shuffle=FALSE)

model %>% reset_states()

}

L = length(kt_test_male_scaled)

scaler = Scaled$scaler

predictions = numeric(L)

I get the warning after running this part. Also, all my 16 predictions have the same value. I also tried to use dim(X) = c(1,16,1) but it did not work

for(i in 1:L){

X = kt_test_male_scaled[i]

dim(X) = c(1,1,1)

yhat = model %>% predict(X, batch_size=batch_size)

# invert scaling

yhat = invert_scaling(yhat, scaler, c(-1, 1))

# invert differencing

#yhat = yhat + kt_male[(n+i)]

# store

predictions[i] <- yhat

}

Model was constructed with shape (1, 16, 1) for input KerasTensor(type_spec=TensorSpec(shape=(1, 16, 1), dtype=tf.float32, name=’lstm_107_input’), name=’lstm_107_input’, description=”created by layer ‘lstm_107_input'”), but it was called on an input with incompatible shape (1, 1, 1)

submitted by /u/rods2292
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *