哪个形状应该具有LSTM NN的输入和输出数据?

2024-05-26 19:53:45 发布

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

我有一个问题,为一个NN和一个LSTM层创建数据的方式。我有许多文件包含数百行。每个文件代表一首歌,每行代表一个有4个值的音符。我想让神经网络用10个音符的序列来读这些音符,这样它就能预测出下一个音符。如果需要的话,我们可以把每首歌的音符数定为5000个。在

所以我只想知道输入和输出数据的形状应该是哪个,以及如何定义第一个LSTM层。在

model = Sequential()
model.add(LSTM(32, input_shape=(5000, 4),return_sequences=True))

向上抽水:

  • 一个文件有5000行4列,代表一首歌。

  • 一个文件中的一行代表一个有4个值的注释。

谢谢你的帮助。在


Tags: 文件数据model定义方式代表序列神经网络
1条回答
网友
1楼 · 发布于 2024-05-26 19:53:45

第一个LSTM层的输入形状应该是(None, 10, 4)。 模型的输出形状将是(None, 4)。我使用None作为批量大小。在

我编写了一个简单的LSTM作为示例:

import numpy as np
from keras.layers import LSTM
from keras.models import Sequential

batch_size = 32
window_length = 10
note_dim = 4
n_samples = 5000

# Input data. TODO: Slide window and modify it to use real data
x = np.ones(shape=(n_samples, window_length, note_dim))
y = np.ones(shape=(n_samples, note_dim))

# Define model
model = Sequential()
model.add(LSTM(note_dim, input_shape=(window_length, note_dim))) # The batch dimension is implicit here

model.compile('sgd', 'mse')
model.fit(x=x, # Batch input shape is: (None, window_length, note_dim)
          y=y, # Batch output shape is: (None, note_dim)
          batch_size=batch_size)

如果您需要更复杂的模型(即2个LSTM层),可以这样定义:

^{pr2}$

更新:回复您的第一条评论。在

x应包含滑动窗口后的所有歌曲。例如,假设您有一个变量songs,其形状为(n_songs, notes_per_song, note_dim),其中包含所有歌曲。然后,可以创建x和{},如下所示:

# ...
# Input data    
# Suppose that variable ´songs´ is an array with shape: (n_songs, notes_per_song, note_dim). 
samples_per_song = notes_per_song-window_length
n_samples = n_songs*samples_per_song
x = np.zeros(shape=(n_samples, window_length, note_dim))
y = np.zeros(shape=(n_samples, note_dim))
for n, song in enumerate(songs):
    for i in range(samples_per_song):
        x[i+n*samples_per_song, :, :] = song[i:(i+window_length), :]
        y[i+n*samples_per_song, :, :] = song[i+window_length, :] # note that you want to predict
# ...
网友
2楼 · 发布于 2024-05-26 19:53:45

I want the NN read the notes with a sequence of 10 notes so that it can predict the next note from them.

我从未使用过keras,但我认为您应该先将这些注释转换为id。例如:(aa,bb,cc,dd)为1,(ab,bb,cc,dd)为2等

然后为编码器读取10个id/notes,然后添加一个投影,将最终状态投影到第11个音符。如果你想用歌曲中任意一个音符中的10个音符来测试模型,那么你就把第二个音符训练到第11个音符,然后把第12个音符作为投影后的目标。以此类推,直到最后一个音符成为目标。这是一首歌,所有的歌都重复这个。在

你完全可以通过身份证取回这些笔记。你可以建立一个词汇表来来回传递它。在

相关问题 更多 >

    热门问题