这是一个序列预测问题,最好用循环或长期短期记忆网络来解决。
以下可能是一个很好的开始:
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout
import numpy as np
#assuming all 4 columns correspond to 1 song
data_dim = 4
#so one song would be 10x4 2D array
number_of_notes_per_song = 10
nsongs_train = 100
#tunable parameter
batch_size = 32
epochs = 5
# I generated dummy data, but you have your own...
x_train = np.random.random((nsongs_train, number_of_notes_per_song, data_dim)).reshape(nsongs_train*number_of_notes_per_song,data_dim)
#this is a supervised learning problem, but your dataset has no labels..
#we can use last note in each song as a label when training LSTM
X = x_train[np.mod(np.arange(x_train.shape[0]),number_of_notes_per_song)!=0].reshape(nsongs_train,number_of_notes_per_song-1,data_dim)
y = x_train[::number_of_notes_per_song].reshape(nsongs_train,data_dim)
model = Sequential()
model.add(LSTM(32, input_shape=(number_of_notes_per_song-1, data_dim),return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(64))
model.add(Dropout(0.2))
model.add(Dense(data_dim, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit(X,y,batch_size=batch_size, epochs=epochs)
#predict on unseen data, expects tensors of shape (None, number_of_notes_per_song-1, data_dim)
model.predict(...)
请注意,这是一个有监督的机器学习问题,但您的数据集没有标签。我们可以通过使用每首歌曲中的最后一个音符作为标签来解决这个问题。这有效地将序列长度减少了 1 个音符。
另外请注意,如果您的歌曲有数百个音符,最好将它们以子序列的形式提供给 LSTM,而不是在歌曲结束之前重置状态。 Here 是使用 Keras 进行有状态训练的示例。
如果需要预测整首歌曲(而不仅仅是下一个字符),您需要在所有 LSTM 层中设置 return_sequences=True 并在输出处使用 TimeDistributed 密集层。