第一个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
x = np.ones(shape=(n_samples, window_length, note_dim))
y = np.ones(shape=(n_samples, note_dim))
model = Sequential()
model.add(LSTM(note_dim, input_shape=(window_length, note_dim)))
model.compile('sgd', 'mse')
model.fit(x=x,
y=y,
batch_size=batch_size)
如果您需要更复杂的模型(例如2个LSTM层),可以按照以下方式定义:
hidden_size = 50
model = Sequential()
model.add(LSTM(hidden_size, input_shape=(window_length, note_dim), return_sequences=True))
model.add(LSTM(note_dim))
更新:回答您的第一个评论。
x
应该包含在它们上滑动窗口后的所有歌曲。例如,假设您有一个形状为 (n_songs, notes_per_song, note_dim)
的变量 songs
包含了所有您的歌曲。然后,您可以按以下方式创建 x
和 y
:
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, :]