值错误:调用层"顺序"(类型顺序)时遇到异常



我正在编写一个AI,它将逐个预测自己的AI——例如,给出一个数组[22,1456,2],它将预测[33,1455,3]。但是当我运行这个代码时:

from music21 import converter, instrument, note, chord
import tensorflow as tf
from scipy.interpolate import UnivariateSpline
from tensorflow import keras
import numpy as np
from sklearn.model_selection import train_test_split
from keras_preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense, LSTM
import tensorflow as tf
import numpy as np
notes = []
file = "C:\Users\User\Desktop\titanic_guitar.mid"
# Получаем все ноты и аккорды из файла
midi = converter.parse(file)
parts = instrument.partitionByInstrument(midi)

if parts:
notes_to_parse = parts.parts[0].recurse()
else:
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element, note.Note):
# Добавляем "ноты, типа ля2-до3"
notes.append(str(element.pitch))
elif isinstance(element, chord.Chord):
# Добавляем аккорды
notes.append('.'.join(str(n) for n in element.pitches))
print(notes)
note_to_int = {note: number for number, note in enumerate(sorted(set(notes)))}
for i in range(len(notes)):
notes[i] = note_to_int.get(notes[i])
print(notes)
X = np.expand_dims(notes, axis = 0)
print(X)
y = np.array([1,1,1])
y = y.reshape(1,-1)
model = Sequential()
model.add(LSTM(4, return_sequences=False, input_shape=(None, X.shape[1])))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
r = model.fit(X,
y,
epochs=5
)

我得到这个错误:

ValueError: Exception encountered when calling layer 'sequential' (type Sequential).

Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 168)

问题出在哪里?

你有工作选择吗?

使用重塑数据

.resape(-1,1)

最新更新