TF Keras: ValueError: อินพุต 0 ของลำดับเลเยอร์เข้ากันไม่ได้กับเลเยอร์: คาดหวัง ndim = 3 พบ ndim = 2

ดังนั้นฉันจึงมีลำดับของเวกเตอร์ 2 มิติที่สร้างรูปแบบ ฉันต้องการทำนายว่าลำดับจะดำเนินต่อไปอย่างไร ฉันมีอาร์เรย์ start_xy ที่ประกอบด้วยอาร์เรย์ตามลำดับ start_x และ start_y: เช่น [1, 2.4, 3.8] และเหมือนกันสำหรับ end_xy

ฉันต้องการฝึกโมเดลให้เป็นโมเดลการทำนายลำดับ:

import numpy as np
import pickle
import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.callbacks import ModelCheckpoint
import training_data_generator
tdg = training_data_generator.training_data_generator(500)
trainingdata = tdg.produceTrainingSequences()
print("Printing DATA!:")
start_xy =[]
end_xy =[]

for batch in trainingdata:
    for pattern in batch:
        order = 1
        for sequence in pattern:
            start = [order,sequence[0],sequence[1]] 
            start_xy.append(start)
            end = [order,sequence[2],sequence[3]]
            end_xy.append(end)
            order = order +1

    
    model = Sequential()
    model.add(LSTM(64, return_sequences=False, input_shape=(2,len(start_xy))))
    model.add(Dense(2, activation='relu'))
    model.compile(loss='mse', optimizer='adam')
    model.fit(start_xy,end_xy,batch_size=len(start_xy), epochs=5000,  verbose=2)

แต่ฉันได้รับข้อความแสดงข้อผิดพลาด:

 ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [320, 3]

ฉันสงสัยว่าฉันต้องปรับรูปแบบอินพุตของฉันใหม่ แต่ฉันยังไม่เข้าใจวิธีการ ฉันจะทำให้งานนี้สำเร็จได้อย่างไร? ฉันทำสิ่งนี้ถูกวิธีหรือยัง?


person Sandra Peters    schedule 14.08.2020    source แหล่งที่มา
comment
คุณช่วยโพสต์โค้ดทั้งหมดพร้อมชุดข้อมูลบน google colab หรือ github เพื่อให้ฉันแก้ไขจุดบกพร่องได้ไหม ฉันต้องการดูว่าข้อมูลที่คุณป้อนมีรูปร่างอย่างไร และเป้าหมายสุดท้ายของคุณคืออะไร   -  person pratsbhatt    schedule 15.08.2020
comment
ฉันได้พุชโค้ดไปที่ github.com/mylittlemachinelearning/sequenceprediction   -  person Sandra Peters    schedule 15.08.2020


คำตอบ (1)


ส่วนใหญ่คุณเพียงแค่ต้องแปลงข้อมูลของคุณเป็นอาร์เรย์ตัวเลขและปรับแต่งข้อมูลนั้นใหม่เพื่อให้โมเดลยอมรับข้อมูลนั้น

ขั้นแรกให้แปลง start_xy เป็นอาร์เรย์ numpy และปรับรูปร่างใหม่ให้มี 3 dims:

start_xy = np.array(start_xy)
start_xy = start_xy.reshape(*start_xy.shape, 1)

ถัดไปแก้ไขรูปร่างอินพุตสำหรับเลเยอร์ LSTM ให้เป็น [3, 1]:

model.add(LSTM(64, return_sequences=False, input_shape=start_xy.shape[1:]))

Let me know if the error persists or if another one comes up!
person Leon Shams    schedule 15.08.2020
comment
นั่นช่วยแก้ไขปัญหาดั้งเดิมของฉันได้ ดังนั้นมันจึงเป็นวิธีแก้ปัญหานี้อย่างแน่นอน :-) ในทางกลับกัน ฉันได้รับตอนนี้ Failed to find data adapter ที่สามารถจัดการอินพุต: ‹class 'numpy.ndarray'›, (‹class 'list'› ที่มี ค่าของประเภท {'(‹class \'list\'› ที่มีค่าประเภท {‹class \'int\'›, ‹class \'numpy.float64\'›})'}) แต่นั่นเป็นคำถามที่แตกต่างออกไป - person Sandra Peters; 15.08.2020