我得到这个错误:ValueError:必须通过2-d输入. < font =宋体>



我正在尝试制作一个基于LSTM的AI来制作音乐。但我总是得到这个错误,不知道这是什么意思。下面是代码:

networkInputShaped,networkOutputShaped = oversample(network_input,network_output, sequence_length=seqlength)

我遵循这个教程:https://medium.com/mlearning-ai/how-to-generate-music-using-machine-learning-72360ba4a085

我试图通过谷歌搜索这个错误来修复它,查看教程提供的源代码(由于某种原因,它与教程中的代码有点不同),随机操作,但它不起作用。

我是初学者,所以我真的不知道预期的输出。

请帮忙,谢谢。

解决这个问题的方法是使network_inputnetwork_output的形状相同。要做到这一点,更改prepare_sequences中的sequence_length变量将oversamplesequence_length参数更改为254。507 .

这是我为成功运行它所做的更改。

<<p>prepare_sequences函数/strong>
def prepare_sequences(notes, n_vocab):
""" Prepare the sequences used by the Neural Network """
sequence_length = 254
# Get all unique pitchnames
pitchnames = sorted(set(item for item in notes))
numPitches = len(pitchnames)
# Create a dictionary to map pitches to integers
note_to_int = dict((note, number) for number, note in enumerate(pitchnames))
network_input = []
network_output = []
# create input sequences and the corresponding outputs
for i in range(0, len(notes) - sequence_length, 1):
# sequence_in is a sequence_length list containing sequence_length notes
sequence_in = notes[i:i + sequence_length]
# sequence_out is the sequence_length + 1 note that comes after all the notes in
# sequence_in. This is so the model can read sequence_length notes before predicting
# the next one.
sequence_out = notes[i + sequence_length]
# network_input is the same as sequence_in but it containes the indexes from the notes
# because the model is only fed the indexes.
network_input.append([note_to_int[char] for char in sequence_in])
# network_output containes the index of the sequence_out
network_output.append(note_to_int[sequence_out])
# n_patters is the length of the times it was iterated 
# for example if i = 3, then n_patterns = 3
# because network_input is a list of lists
n_patterns = len(network_input)
# reshape the input into a format compatible with LSTM layers
# Reshapes it into a n_patterns by sequence_length matrix
print(len(network_input))

network_input = numpy.reshape(network_input, (n_patterns, sequence_length, 1))
# normalize input
network_input = network_input / float(n_vocab)
# OneHot encodes the network_output
network_output = np_utils.to_categorical(network_output)
return (network_input, network_output)

n_vocab = len(set(notes))
network_input, network_output = prepare_sequences(notes,n_vocab)
n_patterns = len(network_input)
pitchnames = sorted(set(item for item in notes))
numPitches = len(pitchnames)
<<p>oversample函数/strong>
def oversample(network_input,network_output,sequence_length=15):
n_patterns = len(network_input)
# Create a DataFrame from the two matrices
new_df = pd.concat([pd.DataFrame(network_input),pd.DataFrame(network_output)],axis=1)
# Rename the columns to numbers and Notes
new_df.columns = [x for x in range(sequence_length+1)]
new_df = new_df.rename(columns={sequence_length:'Notes'})
print(new_df.tail(20))
print('###################################################')
print(f'Distribution of notes in the preoversampled DataFrame: {new_df["Notes"].value_counts()}')
# Oversampling
oversampled_df = new_df.copy()
#max_class_size = np.max(oversampled_df['Notes'].value_counts())
max_class_size = 700
print('Size of biggest class: ', max_class_size)
class_subsets = [oversampled_df.query('Notes == ' + str(i)) for i in range(len(new_df["Notes"].unique()))] # range(2) because it is a binary class
for i in range(len(new_df['Notes'].unique())):
try:
class_subsets[i] = class_subsets[i].sample(max_class_size,random_state=42,replace=True)
except:
print(i)
oversampled_df = pd.concat(class_subsets,axis=0).sample(frac=1.0,random_state=42).reset_index(drop=True)
print('###################################################')
print(f'Distribution of notes in the oversampled DataFrame: {oversampled_df["Notes"].value_counts()}')
# Get a sample from the oversampled DataFrame (because it may be too big, and we also have to convert it into a 3D array for the LSTM)
sampled_df = oversampled_df.sample(n_patterns,replace=True) # 99968*32 has to be equals to (99968,32,1)
print('###################################################')
print(f'Distribution of notes in the oversampled post-sampled DataFrame: {sampled_df["Notes"].value_counts()}')
# Convert the training columns back to a 3D array
network_in = sampled_df[[x for x in range(sequence_length)]]
network_in = np.array(network_in)
# network_in = np.reshape(network_input, (n_patterns, sequence_length, 1))
network_in = network_in / numPitches
print(network_in.shape)
print(sampled_df['Notes'].shape)
# Converts the target column into a OneHot encoded matrix
network_out = pd.get_dummies(sampled_df['Notes'])
print(network_out.shape)
return network_in,network_out
print(network_input[...,0].shape)
print(network_output.shape)
networkInputShaped,networkOutputShaped = oversample(network_input[...,0],network_output,sequence_length=507)
networkOutputShaped = np_utils.to_categorical(network_output)

最新更新