0
votes

I was trying to build a CNN model using Keras. Here is my code:

import keras
import tensorflow as tf
from keras.layers import Dense
from keras.models import Sequential
from keras.models import load_model
from keras.callbacks import EarlyStopping  
from sklearn.model_selection import train_test_split
from __future__ import print_function
import pandas as pd
import numpy as np
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D
from google.colab import drive
drive.mount('/content/drive')
                  ...

# Define x_train...data
x_data=df.iloc[:,1:10084].values-25 
# x_data = np.array(x_data).tolist()  
y_data=df[['type1','type2']].values
X_train, X_test, y_train, y_test = train_test_split(x_data, y_data,test_size=0.2)

model.fit(X_train, y_train,
          batch_size=100,
          epochs=100,
          verbose=1,
          validation_data=(X_test, y_test),
          callbacks=[history])

It returned the error

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (595, 10083)

After referring to other questions I tried to reshape the dimension of the data array using

X_train = X_train[np.newaxis, :, :, :]

which changed it to 3-dimentional and returned the error:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (1, 595, 10083)

How should I increase the dimension of the data to 4?

1
What data is this?gmds
There are too much details which may not fit in the comment, please see this for the datanilsinelabore
Why not use an RNN? If you want a CNN, you need a 1D CNN here.gmds
thanks @gmds It performed badly on my data as I joint various features of time series form into one so I'd like to see how it goes on CNNnilsinelabore
@gmds I changed Conv2D to Conv1D and had this ValueError: Error when checking input: expected conv2d_1_input to have shape (744, 10183, 1) but got array with shape (595, 10083, 1)nilsinelabore

1 Answers

0
votes

The first layer of the model, which a 2D convolution, is expecting (I believe) 2D image data with multiple channels. Something like:

(img_height, img_width, num_channels).

In addition, you need to have another dimension for the "batch" or "sample." Therefore the final shape will be:

(num_samples, img_height, img_width, num_channels).

So, you'll need to reshape your input data to be in the above pattern, with the various dimensions having the interpretation I've indicated, based on what I infer from your code.

You might look at functions like np.reshape and np.expand_dims, depending on how your input data is formatted in the first place.

You can create some test data like this:

x_train = np.random.random((4,32,32,3))

# then try out the model prediction

model.predict(x_train)  

which is four images of 32x32 size, and 3 channels, to verify your processing.

You'll also need to create something appropriate for the y_train when you do the actual fitting.

I hope this helps.