Keras是如何对LSTM/时间序列问题进行维数论证的?

2024-04-19 16:57:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎找不到具体的答案来回答如何将数据输入Keras的问题。大多数例子似乎都处理图像/文本数据,并且有明确定义的数据点。在

我正在尝试将音乐输入LSTM神经网络。我想让电视台播放大约3秒钟的音乐,然后指定2秒钟。我把我的音乐准备成.wav文件,并分成5秒的间隔,然后分解成X(前3秒)和Y(最后2秒)。我以44100Hz的频率采样了我的音乐,所以我的X是132300个观察值“长”,Y是“88200”个观察值。在

但我不知道如何将Keras与我的数据结构连接起来。我使用的是Tensorflow后端。在

为了概括问题和答案,我将使用A,B,C来表示维数。这个示例数据和我的实际数据之间的唯一区别是这些是从0到1分布的随机值,而我的数据是一个整数数组。在

import numpy as np
#using variables to make it easy to generalize the answer

#a = the number of observations I have
a       = 411

#b = the duration of the sample, 44.1k observations per second of music
b_train = 132300
b_test  = 88200

#c = the number of channels in the music, this is 2 channel stereo
c       = 2

#now create sample data with the dimensionality given above:
X = np.random.rand(a,b_train,c)
y = np.random.rand(a,b_test ,c)

#split the data
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.20, random_state=42)    

然而,我真的不知道如何配置一个模型来理解“第一”(a)维包含观察值,我想或多或少地通过通道(C)来分解音乐(B)。在

我知道把它转换成单声道(和一个二维问题)可能会更容易,但我很想知道这是否有一个“简单”的解决方案-是否主要采用我下面的形式,或者我是否应该用另一种方式来思考这个模型。在

主要的问题是:如何构建一个模型,使我能够将X数据转换为Y数据?在

理想情况下,答案将显示如何修改下面的模型以适应上面的数据结构。在

^{pr2}$

但是,这会产生一个错误(在模型=。。。步骤):

 ValueError: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=4

我不知道Keras从哪里得到ndim=4数据的期望值。此外,我不知道如何确保将数据输入模型,使模型“理解”观测值分布在A轴上,而数据本身分布在B轴和C轴上。在

如果有什么不清楚的地方,请留言。我会一直关注到9月17日左右,我一定会更新这个问题,以反映所留下的建议/意见。在

谢谢!在


Tags: ofthe数据答案模型test数据结构音乐
1条回答
网友
1楼 · 发布于 2024-04-19 16:57:49

Keras惯例是批处理维度通常在input_shape参数中省略。从guide

Pass an input_shape argument to the first layer. This is a shape tuple (a tuple of integers or None entries, where None indicates that any positive integer may be expected). In input_shape, the batch dimension is not included.

所以改变model = build_model([132300,2])应该可以解决这个问题。在

相关问题 更多 >