第一个 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 应该包含滑动窗口后的所有歌曲。例如,假设您有一个变量songs,其形状为(n_songs, notes_per_song, note_dim),其中包含您所有的歌曲。然后,您可以创建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
# ...