Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras 1D CNN: How to specify dimension correctly?

So, what I'm trying to do is to classify between exoplanets and non exoplanets using the kepler data obtained here. The data type is a time series with the dimension of (num_of_samples,3197). I figured out that this can be done by using 1D Convolutional Layer in Keras. But I keep messing the dimensions and get the following error

Error when checking model input: expected conv1d_1_input to have shape (None, 3197, 1) but got array with shape (1, 570, 3197)

So, the questions are:

1.Does the data (training_set and test_set) need to be converted into 3D tensor? If yes, what is the correct dimension?

2.What is the correct input shape? I know that I have 3197 timesteps for 1 feature but the documentation does not specify whether they use TF or theano backend so I'm still getting headaches.

By the way, I'm using TF backend. Any kind help is greatly appreciated! Thanks!

"""
Created on Wed May 17 18:23:31 2017

@author: Amajid Sinar
"""

import matplotlib.pyplot as plt
import pandas as pd
plt.style.use("ggplot")
import numpy as np

#Importing training set
training_set = pd.read_csv("exoTrain.csv")
X_train = training_set.iloc[:,1:].values
y_train = training_set.iloc[:,0:1].values

#Importing test set
test_set = pd.read_csv("exoTest.csv")
X_test = test_set.iloc[:,1:].values
y_test = test_set.iloc[:,0:1].values

#Scale the data
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)

#Convert data into 3d tensor
X_train = np.reshape(X_train,(1,X_train.shape[0],X_train.shape[1]))
X_test = np.reshape(X_test,(1,X_test.shape[0],X_test.shape[1]))


#Importing convolutional layers
from keras.models import Sequential
from keras.layers import Convolution1D
from keras.layers import MaxPooling1D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers.normalization import BatchNormalization

#Convolution steps
#1.Convolution
#2.Max Pooling
#3.Flattening
#4.Full Connection

#Initialising the CNN
classifier = Sequential()

#Input shape must be explicitly defined, DO NOT USE (None,shape)!!!
#1.Multiple convolution and max pooling
classifier.add(Convolution1D(filters=8, kernel_size=11, activation="relu", input_shape=(3197,1)))
classifier.add(MaxPooling1D(strides=4))
classifier.add(BatchNormalization())
classifier.add(Convolution1D(filters=16, kernel_size=11, activation='relu'))
classifier.add(MaxPooling1D(strides=4))
classifier.add(BatchNormalization())
classifier.add(Convolution1D(filters=32, kernel_size=11, activation='relu'))
classifier.add(MaxPooling1D(strides=4))
classifier.add(BatchNormalization())
#classifier.add(Convolution1D(filters=64, kernel_size=11, activation='relu'))
#classifier.add(MaxPooling1D(strides=4))


#2.Flattening
classifier.add(Flatten())


#3.Full Connection
classifier.add(Dropout(0.5))
classifier.add(Dense(64, activation='relu'))
classifier.add(Dropout(0.25))
classifier.add(Dense(64, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))

#Configure the learning process
classifier.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

#Train!
classifier.fit_generator(X_train, steps_per_epoch=X_train.shape[0], epochs=1, validation_data=(X_test,y_test))

score = classifier.evaluate(X_test, y_test)
like image 636
Amajid Sinar Avatar asked May 20 '17 17:05

Amajid Sinar


People also ask

What is dimension in CNN?

The layers of a CNN have neurons arranged in 3 dimensions: width, height and depth. Where each neuron inside a convolutional layer is connected to only a small region of the layer before it, called a receptive field.

What is the output of 1D CNN?

Input and output data of 1D CNN is 2 dimensional. Mostly used on Time-Series data. In 2D CNN, kernel moves in 2 directions. Input and output data of 2D CNN is 3 dimensional.

What is convolution1d?

1D convolution layer (e.g. temporal convolution). This layer creates a convolution kernel that is convolved with the layer input over a single spatial (or temporal) dimension to produce a tensor of outputs. If use_bias is True, a bias vector is created and added to the outputs.


1 Answers

  1. Yes, your dataset should be a 3d tensor.

  2. The correct input shape (for tensorflow backend) is (sample_number,sample_size,channel_number). You can check that from your error message, "the expected dimension was (None, 3197, 1)".

'None' refers to an arbitrary size dimension, as it is expected to the number of samples used in training.

So in your situation the correct shape is (570, 3197, 1).

If you happen to use theano backend you should put your channel dimension first: (sample_number,channel_number,sample_size) or in your paricular case

(570,1, 3197)

like image 180
maz Avatar answered Sep 26 '22 07:09

maz