rb3652
rb3652

Reputation: 445

1D CNN in TensorFlow for Time Series Classification

My Time-Series is a 30000 x 500 table representing points from three different types of graphs: Linear, Quadratic, and Cubic Sinusoidal. Thus, there are 10000 Rows for Linear Graphs, 10000 for Quadratics, and 10000 for Cubics. I have sampled 500 points from every graph. Here's an image to illustrate my point:

enter image description here

I've built a 98% accurate 2D CNN using TensorFlow, but now I want to build a 1D CNN using TensorFlow. Do I just replace every Conv2D layer with Conv1D? If so, what would my filters and kernel_size be? I don't even know how to import my 1D pandas dataframe. My 2D CNN has the following architecture:

model = tf.keras.Sequential([
  tf.keras.layers.experimental.preprocessing.Rescaling(1./255),
  tf.keras.layers.Conv1D( 32, 3, activation='relu', input_shape=input_shape[2:])(x), #32 FILTERS and square stride of size 3
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(num_classes)
])

If anyone can help, that would be great. Thank you. Below is an MWE and my 2D CNN is here.

num_classes = 3
model = tf.keras.Sequential([
  tf.keras.layers.experimental.preprocessing.Rescaling(1./255),
  tf.keras.layers.Conv2D(32, 3, activation='relu'), #32 FILTERS and square stride of size 3
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(num_classes)
])


epochs = 5

initial_learning_rate = 1
decay = initial_learning_rate / epochs

def lr_time_based_decay(epoch, lr):
    return lr * 1 / (1 + decay * epoch)

history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs= epochs,
  callbacks= [tensorboard_callback, tf.keras.callbacks.LearningRateScheduler(lr_time_based_decay, verbose=1)]
)

Upvotes: 5

Views: 2753

Answers (1)

user11530462
user11530462

Reputation:

Conv1D equivalent code. Conv1D layer expects 3D input and outputs 3D shape. Maxpooling2D expects 4D input. You need to use maxpooling1D layer.

Sample code

import tensorflow as tf
input_shape = (4, 7, 10, 128)
num_classes = 3

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv1D(filters= 32, kernel_size=3, activation='relu',padding='same',input_shape= input_shape[2:]))
model.add(tf.keras.layers.MaxPooling1D())
model.add(tf.keras.layers.Conv1D(filters=32, kernel_size=3,padding='same',activation='relu'))
model.add(tf.keras.layers.MaxPooling1D())
model.add(tf.keras.layers.Conv1D(filters=32, kernel_size=3,padding='same',activation='relu'))
model.add(tf.keras.layers.MaxPooling1D())
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))

model.summary()

Output

Model: "sequential_13"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d_35 (Conv1D)          (None, 10, 32)            12320     
                                                                 
 max_pooling1d_20 (MaxPoolin  (None, 5, 32)            0         
 g1D)                                                            
                                                                 
 conv1d_36 (Conv1D)          (None, 5, 32)             3104      
                                                                 
 max_pooling1d_21 (MaxPoolin  (None, 2, 32)            0         
 g1D)                                                            
                                                                 
 conv1d_37 (Conv1D)          (None, 2, 32)             3104      
                                                                 
 max_pooling1d_22 (MaxPoolin  (None, 1, 32)            0         
 g1D)                                                            
                                                                 
 flatten_8 (Flatten)         (None, 32)                0         
                                                                 
 dense_15 (Dense)            (None, 128)               4224      
                                                                 
 dense_16 (Dense)            (None, 3)                 387       
                                                                 
=================================================================
Total params: 23,139
Trainable params: 23,139
Non-trainable params: 0

Upvotes: 4

Related Questions