TensorFlow 中用于时间序列分类的一维 CNN
1D CNN in TensorFlow for Time Series Classification
我的时间序列是 30000 x 500 table,表示来自三种不同类型图形的点:线性、二次和三次正弦曲线。因此,线性图有 10000 行,二次方程有 10000 行,三次方程有 10000 行。我从每张图中抽取了 500 个点。这是一张图片来说明我的观点:
我已经使用 TensorFlow 构建了一个准确率为 98% 的 2D CNN,但现在我想使用 TensorFlow 构建一个 1D CNN。我是否只用 Conv1D
替换每个 Conv2D
层?如果是这样,我的 filters
和 kernel_size
会是什么?我什至不知道如何导入我的 1D pandas 数据框。我的 2D CNN 具有以下架构:
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)
])
如果有人能提供帮助,那就太好了。谢谢你。下面是一个 MWE,我的 2D CNN 是 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)]
)
Conv1D 等效代码。 Conv1D 层需要 3D 输入并输出 3D 形状。 Maxpooling2D 需要 4D 输入。您需要使用 maxpooling1D 层。
示例代码
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()
输出
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
我的时间序列是 30000 x 500 table,表示来自三种不同类型图形的点:线性、二次和三次正弦曲线。因此,线性图有 10000 行,二次方程有 10000 行,三次方程有 10000 行。我从每张图中抽取了 500 个点。这是一张图片来说明我的观点:
我已经使用 TensorFlow 构建了一个准确率为 98% 的 2D CNN,但现在我想使用 TensorFlow 构建一个 1D CNN。我是否只用 Conv1D
替换每个 Conv2D
层?如果是这样,我的 filters
和 kernel_size
会是什么?我什至不知道如何导入我的 1D pandas 数据框。我的 2D CNN 具有以下架构:
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)
])
如果有人能提供帮助,那就太好了。谢谢你。下面是一个 MWE,我的 2D CNN 是 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)]
)
Conv1D 等效代码。 Conv1D 层需要 3D 输入并输出 3D 形状。 Maxpooling2D 需要 4D 输入。您需要使用 maxpooling1D 层。
示例代码
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()
输出
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