1D classification using Keras

5,193 views
Skip to first unread message

Ik Pot

unread,
Mar 7, 2016, 10:24:40 AM3/7/16
to Keras-users
I have a dataset 10000x128 =records x no of features. The labels are represented through vector labels=10000 x 1 . labels have one of 3 possible classes 0,1,2
Therefore we have a 1D dataset (1x128) with 10000 cases. It is NOT time-series. It is just 1D dataset. I need to classify it with a convolutional neural net. It must be cnn not something else.
Does anybody have a clue?? a simple example with 1D dataset (not time-series)

thank you so much
I.

Klemen Grm

unread,
Mar 7, 2016, 1:56:59 PM3/7/16
to Keras-users
Without knowing your data, I can't recommend a particular architecture (or even know whether a CNN is a good fit for your application), but here is an example of a CNN that will fit data of that shape:

from keras.models import Sequential
from keras.layers import Convolution2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.utils import np_utils
import numpy as np

# import your data here instead
# X - inputs, 10000 samples of 128-dimensional vectors
# y - labels, 10000 samples of scalars from the set {0, 1, 2}

X
= np.random.rand(10000, 128).astype("float32")
y
= np.random.randint(3, size=(10000,1))

# process the data to fit in a keras CNN properly
# input data needs to be (N, C, X, Y) - shaped where
# N - number of samples
# C - number of channels per sample
# (X, Y) - sample size

X
= X.reshape((10000, 1, 128, 1))

# output labels should be one-hot vectors - ie,
# 0 -> [0, 0, 1]
# 1 -> [0, 1, 0]
# 2 -> [1, 0, 0]
# this operation changes the shape of y from (10000,1) to (10000, 3)

y
= np_utils.to_categorical(y)

# define a CNN
# see http://keras.io for API reference

cnn
= Sequential()
cnn
.add(Convolution2D(64, 3, 1,
    border_mode
="same",
    activation
="relu",
    input_shape
=(1, 128, 1)))
cnn
.add(Convolution2D(64, 3, 1, border_mode="same", activation="relu"))
cnn
.add(MaxPooling2D(pool_size=(2,1)))

cnn
.add(Convolution2D(128, 3, 1, border_mode="same", activation="relu"))
cnn
.add(Convolution2D(128, 3, 1, border_mode="same", activation="relu"))
cnn
.add(Convolution2D(128, 3, 1, border_mode="same", activation="relu"))
cnn
.add(MaxPooling2D(pool_size=(2,1)))
   
cnn
.add(Convolution2D(256, 3, 1, border_mode="same", activation="relu"))
cnn
.add(Convolution2D(256, 3, 1, border_mode="same", activation="relu"))
cnn
.add(Convolution2D(256, 3, 1, border_mode="same", activation="relu"))
cnn
.add(MaxPooling2D(pool_size=(2,1)))
   
cnn
.add(Flatten())
cnn
.add(Dense(1024, activation="relu"))
cnn
.add(Dropout(0.5))
cnn
.add(Dense(3, activation="softmax"))

# define optimizer and objective, compile cnn

cnn
.compile(loss="categorical_crossentropy", optimizer="adam")

# train

cnn
.fit(X, y, nb_epoch=20, show_accuracy=True)

Ik Pot

unread,
Mar 8, 2016, 11:02:06 AM3/8/16
to Keras-users
Thank you for your reply. It works for me. I kind of expected the Convolution1D and Maxpooling1D. But if you believe they are equivalent it is fine.
Thnx again

Vinayakumar R

unread,
Sep 29, 2016, 1:38:58 PM9/29/16
to Keras-users
Could you please give me a sample example for convolution1d example

Vinayakumar R

unread,
Sep 29, 2016, 1:40:24 PM9/29/16
to Keras-users
I have data set train 10000*20 test 1000*20 19 features and 1 class label having 5 classes. how could i chand imdb_cnn example for my data set

Vinayakumar R

unread,
Sep 29, 2016, 10:47:18 PM9/29/16
to Keras-users
I have tried implementing cnn-lstm for the above example but getting an error. the error is shape error. Could you please tell how to solve this?
the code is given below

from keras.models import Sequential
from keras.layers import Convolution2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.utils import np_utils
import numpy as np
from keras.layers import LSTM, GRU, SimpleRNN


# import your data here instead
# X - inputs, 10000 samples of 128-dimensional vectors
# y - labels, 10000 samples of scalars from the set {0, 1, 2}

X = np.random.rand(10000, 128).astype("float32")
y = np.random.randint(3, size=(10000,1))

# process the data to fit in a keras CNN properly
# input data needs to be (N, C, X, Y) - shaped where
# N - number of samples
# C - number of channels per sample
# (X, Y) - sample size

X = X.reshape((10000, 1, 128, 1))

# output labels should be one-hot vectors - ie,
# 0 -> [0, 0, 1]
# 1 -> [0, 1, 0]
# 2 -> [1, 0, 0]
# this operation changes the shape of y from (10000,1) to (10000, 3)

y = np_utils.to_categorical(y)


lstm_output_size = 70
cnn = Sequential()
cnn.add(Convolution2D(64, 3, 1,
    border_mode="same",
    activation="relu",
    input_shape=(1, 128, 1)))
cnn.add(Convolution2D(64, 3, 1, border_mode="same", activation="relu"))
cnn.add(MaxPooling2D(pool_size=(2,1)))
cnn.add(LSTM(lstm_output_size))

Macarena Floriano

unread,
Jan 9, 2017, 2:08:07 PM1/9/17
to Keras-users
Hi, 
I have a similar problem with my cnn, but I've tried the solution given by Klemen Grm and it didn't work to me. I have read that it solved your problem and I was wondering If you could help me. (My problem is about regression, but I have a problem with cnn before introduce my targets).

My training dataset is composed by 240 features and I have 1730 samples, so my X has dimensions of (1730L, 240L). I have tried with Convolution1D and Convolution2D, but I always get the problem at the same point: input_shape. I have followed all the steps in the solution of Klemen Grm but in that case I have a problem when I try to define the Flatten layer, and it tells me that I have not fully complete the input_shape argument in my first layer.

I have reshape my X:

X=X.reshape(1730,1,240,1)

So my firts layer is:

model.add(Convolution2D(120,3,1,input_shape(1,240,1), border_mode='same', activation='relu'))

I don't know if I am missing somethin about the input_shape arguments but I don't get it. If you could help me I would be thankfull. 

mirosl...@gmail.com

unread,
Feb 7, 2018, 4:37:05 AM2/7/18
to Keras-users
You can try to exchange 

cnn.add(MaxPooling2D(pool_size=(2,1))) 
to
cnn.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="th"))

worked for me.

Dňa piatok, 30. septembra 2016 4:47:18 UTC+2 Vinayakumar R napísal(-a):

potamitis

unread,
Feb 7, 2018, 4:40:26 AM2/7/18
to mirosl...@gmail.com, Keras-users
1D classification of short audio files
--
You received this message because you are subscribed to a topic in the Google Groups "Keras-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/keras-users/SBQBYGqFmAA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/2e1732fc-265b-4c7a-8c48-d7dec064fd17%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages