I_Al-thamary
I_Al-thamary

Reputation: 3983

Convolution neural network using sequential model for non-image data

I need to apply convolutional neural network using sequential model for wind-forecasting which is in https://github.com/willfleury/wind-forecasting/blob/master/Forecasting.ipynb. I tried to use this code and it works well with image data but when I use the same example for the wind-forecasting example, I got an error message

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

I tried to search in google and I found several answers but none of these answers solve my problem. Moreover, I tried to use reshape.

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD


#data base information that I need to use 
#number of feautures 3 
#number of Cols 5 
#number of Rows 6143
# Generate dummy data
x_train = np.random.random((100, 100, 100, 3))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
x_test = np.random.random((20, 100, 100, 3))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)


# x_train = np.reshape(x_train, (x_train_scaled.shape[0], 1,x_train_scaled.shape[1]))
# x_test = np.reshape(x_test_scaled, (x_test_scaled.shape[0],1, x_test_scaled.shape[1]))
#
# y_train = np.reshape(y_train_scaled, (y_train_scaled.shape[0], 3))
# y_test = np.reshape(y_test_scaled, (y_test_scaled.shape[0], 3))

model = Sequential()
# input: 100x100 images with 3 channels -> (100, 100, 3) tensors.
# this applies 32 convolution filters of size 3x3 each.
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)

model.fit(x_train, y_train, batch_size=32, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=32)

Thanks In Advance.

Upvotes: 1

Views: 1401

Answers (3)

I_Al-thamary
I_Al-thamary

Reputation: 3983

I solved the problem by using loss='binary_crossentropy' and also padding='same'. Moreover, instead of 5 cols, I use just only 4 cols.

Upvotes: 0

End-2-End
End-2-End

Reputation: 921

Remember that CNNs are made for working with 2D data mainly (that contains at least 2 dimensions in Height and Width). Also, CNNs should only be used in cases where the order of your columns is important and can't be changed. For example, when you change the order of columns or shuffle the columns in an image, it would change the entire image. But the same is not the case for a typical ML classification problem (say Titanic-Survival Prediction). So be absolutely sure that the order of the columns for your forecasting data is important and can't be changed. If not, you're better off not using CNNs and sticking to standard RNN based forecasting methods.

Having said that, since you're going by a row-to-row basis in this case, you don't actually have a 2nd dimension. Try passing this to a 1D convolution operation. When you pass this to 1D conv, it actually adds an extra dimension and considers the height of the tensor to be 1, which works like a [1xn] shaped filter, striding across the entire row (only in horizontal direction).

Hope this helps.

Upvotes: 2

Minh-Tuan Nguyen
Minh-Tuan Nguyen

Reputation: 358

Conv2D requires 4 dimensions: sample_size, height, width, channel. So if your input has only 1 channel (for example grayscale image instead of RGB), you need to add another dimension at the end.

This could be done using built-in numpy expand_dims or reshape. https://docs.scipy.org/doc/numpy/reference/generated/numpy.expand_dims.html

Upvotes: 0

Related Questions