gpt4 book ai didi

python - 如何将输入传递给 Keras 中的 2D Conv?

转载 作者:行者123 更新时间:2023-12-02 06:39:18 24 4
gpt4 key购买 nike

我已经使用 librosa 将语音转换为频谱图。频谱图的形状是 (257, 356),我已将其 reshape 为 (257, 356, 1)。

我已经创建了一个模型

from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=A.shape))
model.add(Flatten())
model.add(Dense(1, activation='softmax'))

拟合模型时,产生跟随误差

model.fit(A,validation_data=(A2), epochs=3)

其中 A2 是具有以下尺寸的另一个频谱图

ValueError: Error when checking input: expected conv2d_3_input to have 4 dimensions, but got array with shape (257, 356, 1)

模型摘要

_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_24 (Conv2D) (None, 255, 354, 64) 640
_________________________________________________________________
conv2d_25 (Conv2D) (None, 253, 352, 32) 18464
_________________________________________________________________
flatten_11 (Flatten) (None, 2849792) 0
_________________________________________________________________
dense_11 (Dense) (None, 10) 28497930
=================================================================
Total params: 28,517,034
Trainable params: 28,517,034
Non-trainable params: 0

A[0]的形状是

A[0].shape = (356, 1)

最佳答案

编辑:这是我的工作代码:

from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
import numpy as np

A = np.zeros((1,257,356,1)) # Only for illustration
A2 = np.zeros((1,1)) # Only for illustration

model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=A.shape[1:])) # input_shape ==> (257,356,1)
model.add(Flatten())
model.add(Dense(1, activation='softmax'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(A, A2, validation_data = (A, A2), epochs=3)

这是 3 个时期的输出:

Train on 1 samples, validate on 1 samples
Epoch 1/3
1/1 [==============================] - 0s 250ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 2/3
1/1 [==============================] - 0s 141ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 3/3
1/1 [==============================] - 0s 156ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000

<keras.callbacks.callbacks.History at 0x1d508dbb708>

关于python - 如何将输入传递给 Keras 中的 2D Conv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59578786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com