最初の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
# ...
あなたは、入力データのサンプルを提供していただけますか? – grovina
入力データには何千もの行があるので、ここでは入力できません.4つの列があり、それぞれの値は0と1の間の数値です。 – Juan