我想有一个CNN,它以原始信号作为输入,并首先处理短时傅立叶变换。因此,我想使用Keras创建自定义图层。
我遵循了here的说明,并将复杂性降低为以下代码:
class CreateSFTF(Layer):
def __init__(self, units=32,n_fft=1000,hop_length=0,log=True, **kwargs):
super(CreateSFTF, self).__init__(**kwargs)
self.units = units
self.n_fft = n_fft
self.hop_length = hop_length
self.log = log
def build(self, input_shape):
super(CreateSFTF, self).build(input_shape)
def call(self, inputs):
def _tf_log10(x):
numerator = tf.math.log(x)
denominator = tf.math.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator
stfts = tf.signal.stft(
input,
frame_length=self.n_fft,
frame_step=self.hop_length,
window_fn=tf.signal.hann_window,
pad_end=False
)
mag_stfts = tf.abs(stfts)
return tf.expand_dims(mag_stfts, 3)
def get_config(self):
config = super(CreateSFTF, self).get_config()
config.update({'units': self.units})
return config
我在这里使用图层:
def DefineCNNWithFFTAtBeginning(length_signal):
input = Input(shape = (length_signal,1))
x = CreateSFTF(n_fft=1000,hop_length=100,log=True)(input)
x = BatchNormalization()(x) # recommended
x = layers.Conv2D(8, (3, 3), activation='relu',padding='valid')(x)
x = layers.Conv2D(8, (3, 3),activation='relu', padding='valid')(x)
x = layers.MaxPooling2D(pool_size=(4, 4))(x)
x = layers.Conv2D(16, (5, 5),activation='relu', padding='valid')(x)
x = layers.Conv2D(4, (5, 5),activation='relu', padding='valid')(x)
x = layers.MaxPooling2D(pool_size=(5, 5))(x)
x = Flatten()(x)
x = layers.Dropout(0.5)(x)
x = Dense(1,activation='sigmoid')(x)
model = Model(input, x)
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.summary()
return model
我在这里打电话:
model = DefineCNNWithFFTAtBeginning(76000)
history =model.fit(X_train.values, y_train,
validation_data=(X_test.values, y_test),
epochs=50, batch_size=32,
shuffle = True)
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 76000)] 0
_________________________________________________________________
create_sftf (CreateSFTF) (None, 751, 513, 1) 0
_________________________________________________________________
batch_normalization (BatchNo (None, 751, 513, 1) 4
_________________________________________________________________
conv2d (Conv2D) (None, 749, 511, 8) 80
_________________________________________________________________
conv2d_1 (Conv2D) (None, 747, 509, 8) 584
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 186, 127, 8) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 182, 123, 16) 3216
_________________________________________________________________
conv2d_3 (Conv2D) (None, 178, 119, 4) 1604
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 35, 23, 4) 0
_________________________________________________________________
flatten (Flatten) (None, 3220) 0
_________________________________________________________________
dropout (Dropout) (None, 3220) 0
_________________________________________________________________
dense (Dense) (None, 1) 3221
=================================================================
Total params: 8,709
Trainable params: 8,707
Non-trainable params: 2
_________________________________________________________________
Train on 987 samples, validate on 247 samples
Epoch 1/50
32/987 [..............................] - ETA: 29s
X_train的形状是(xxxxx,76000)
旧错误消息
检查输入时出错:期望input_1具有3个尺寸,但是 形状为(987,1)的数组-已解决
有人知道解决方案吗?预先感谢。
更新
新错误消息
渴望执行功能的输入不能是Keras符号张量,而是找到tf.tensor'input_4:0'shape =(None,76000)dtype = float32
尝试
答案 0 :(得分:0)
“输入层”为批次大小添加尺寸:
输入形状
nD张量,形状为:(batch_size,...,input_dim)。最常见的情况是形状为(batch_size,input_dim)的2D输入。
因此,正如keras的文档所述(上文引用),您指定的批量大小为length_signal
。
尝试类似(length_signal, )
,( ,length_signal)
(或类似的词)的内容。
答案 1 :(得分:0)
您的模型期望形状张量(None,76000,1),但您正在传递(987,1)
您必须预处理数据以符合条件。
987对于该模型不是有效尺寸,必须使用76000 X1。如果不正确,则必须在模型中对其进行校正。
如果您想一次添加额外的尺寸或对单个火车数据进行训练,则可以使用以下代码扩展尺寸
x = np.random.randn((76000,1)) # shape = (76000 X 1)
x = x[None] # shape = (1 X 76000 X 1)