哪种形状应该具有 LSTM NN 的输入和输出数据?
Which shape should have input and output data for a LSTM NN?
我对使用 LSTM 层为 NN 创建数据的方式有疑问。我有 许多文件,其中包含数百行。 每个文件代表一首歌曲,每行代表一个具有 4 个值的音符。我希望 NN 读取带有 10 个音符序列 的音符,以便它可以根据它们预测下一个音符。如果需要的话,我们可以把音符数固定在每首歌5000个。
所以我只想知道我的输入和输出数据应该具有哪种形状,以及如何定义第一个 LSTM 层。
model = Sequential()
model.add(LSTM(32, input_shape=(5000, 4),return_sequences=True))
总结一下:
一个文件5000行4列代表1首歌曲
文件中的一行代表一个有 4 个值的音符。
感谢您的帮助。
I want the NN read the notes with a sequence of 10 notes so that it can predict the next note from them.
我从未使用过keras,但我认为你应该先将这些笔记转换成ids。例如:(aa, bb, cc, dd) 为 1,(ab, bb, cc, dd) 为 2 等等
然后你为encoder读10ids/notes然后加一个projection把最终状态投射到11音符。如果你想用歌曲中任何音符的 10 个音符来测试模型,你可以训练第二个到第 11 个音符,并将第 12 个音符作为投影后的目标。依此类推,直到最后一个音符作为目标。这是一首歌曲,对所有歌曲重复此操作。
你绝对可以通过id找回笔记。你可以建立一个词汇来来回传递它。
第一个 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 层),您可以这样定义它:
# ...
# Define model
hidden_size = 50
model = Sequential()
model.add(LSTM(hidden_size, input_shape=(window_length, note_dim), return_sequences=True)) # The batch dimension is implicit here
model.add(LSTM(note_dim))
# ...
更新: 回复您的第一条评论。
x
应该包含滑动 window 后的所有歌曲。例如,假设您有一个形状为 (n_songs, notes_per_song, note_dim)
的变量 songs
,其中包含您所有的歌曲。然后,您可以创建 x
和 y
,如下所示:
# ...
# 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
# ...
我对使用 LSTM 层为 NN 创建数据的方式有疑问。我有 许多文件,其中包含数百行。 每个文件代表一首歌曲,每行代表一个具有 4 个值的音符。我希望 NN 读取带有 10 个音符序列 的音符,以便它可以根据它们预测下一个音符。如果需要的话,我们可以把音符数固定在每首歌5000个。
所以我只想知道我的输入和输出数据应该具有哪种形状,以及如何定义第一个 LSTM 层。
model = Sequential()
model.add(LSTM(32, input_shape=(5000, 4),return_sequences=True))
总结一下:
一个文件5000行4列代表1首歌曲
文件中的一行代表一个有 4 个值的音符。
感谢您的帮助。
I want the NN read the notes with a sequence of 10 notes so that it can predict the next note from them.
我从未使用过keras,但我认为你应该先将这些笔记转换成ids。例如:(aa, bb, cc, dd) 为 1,(ab, bb, cc, dd) 为 2 等等
然后你为encoder读10ids/notes然后加一个projection把最终状态投射到11音符。如果你想用歌曲中任何音符的 10 个音符来测试模型,你可以训练第二个到第 11 个音符,并将第 12 个音符作为投影后的目标。依此类推,直到最后一个音符作为目标。这是一首歌曲,对所有歌曲重复此操作。
你绝对可以通过id找回笔记。你可以建立一个词汇来来回传递它。
第一个 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 层),您可以这样定义它:
# ...
# Define model
hidden_size = 50
model = Sequential()
model.add(LSTM(hidden_size, input_shape=(window_length, note_dim), return_sequences=True)) # The batch dimension is implicit here
model.add(LSTM(note_dim))
# ...
更新: 回复您的第一条评论。
x
应该包含滑动 window 后的所有歌曲。例如,假设您有一个形状为 (n_songs, notes_per_song, note_dim)
的变量 songs
,其中包含您所有的歌曲。然后,您可以创建 x
和 y
,如下所示:
# ...
# 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
# ...