问题描述
很抱歉,如果这是一个菜鸟问题。这是我第一次尝试使用Spyder IDE和Git Fork完成我自己的项目。
我正在使用Music21将MIDI文件转换为音符,然后准备序列并将它们传递到网络中。 (基于https://github.com/Skuldur/Classical-Piano-Composer)
def generate():
""" Generate a piano midi file """
#load the notes used to train the model
with open('data/notes','rb') as filepath:
notes = pickle.load(filepath)
# Get all pitch names
pitchnames = sorted(set(item for item in notes))
# Get all pitch names
n_vocab = len(set(notes))
network_input,normalized_input = prepare_sequences(notes,pitchnames,n_vocab)
model = create_network(normalized_input,n_vocab)
prediction_output = generate_notes(model,network_input,n_vocab)
create_midi(prediction_output)
def prepare_sequences(notes,n_vocab):
""" Prepare the sequences used by the Neural Network """
# map between notes and integers and back
note_to_int = dict((note,number) for number,note in enumerate(pitchnames))
sequence_length = 100
network_input = []
output = []
for i in range(0,len(notes) - sequence_length,1):
sequence_in = notes[i:i + sequence_length]
sequence_out = notes[i + sequence_length]
network_input.append([note_to_int[char] for char in sequence_in])
output.append(note_to_int[sequence_out])
n_patterns = len(network_input)
# reshape the input into a format compatible with LSTM layers
normalized_input = numpy.reshape(network_input,(n_patterns,sequence_length,1))
# normalize input
normalized_input = normalized_input / float(n_vocab)
return (network_input,normalized_input)
def create_network(network_input,n_vocab):
""" create the structure of the neural network """
model = Sequential()
model.add(LSTM(
512,input_shape=(network_input.shape[1],network_input.shape[2]),recurrent_dropout=0.3,return_sequences=True
))
model.add(LSTM(512,return_sequences=True,))
model.add(LSTM(512))
model.add(Batchnorm())
model.add(Dropout(0.3))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Batchnorm())
model.add(Dropout(0.3))
model.add(Dense(n_vocab))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer='rmsprop')
# Load the weights to each node
model.load_weights('weights.hdf5')
return model
培训工作正常,但随后预测我得到了: ValueError:形状(256、263)和(256、279)不兼容
解决方法
找到了解决问题的方法:本质上,我使用的是预先训练的模型权重,该权重利用的是另一个导致值错误的数据集。要解决此问题,只需根据您自己的回调调用特定的重量文件: 例如
filepath = "weights-improvement-{epoch:02d}-{loss:.4f}-bigger.hdf5"