lovedlim / tensorflow

파이썬 딥러닝 텐서플로 도서(정보문화사) 예제 파일입니다.
16 stars 24 forks source link

5.3_movie_review.ipynb 오류 #15

Closed yu901 closed 2 years ago

yu901 commented 2 years ago

image

학습

history = model.fit(train_padded, train_labels, validation_data=(valid_padded, valid_labels), callbacks=[early_stop, checkpoint], batch_size=64, epochs=10, verbose=2)

코드 실행 시 아래와 같은 오류가 발생합니다.

ValueError: logits and labels must have the same shape, received ((64, 74, 1) vs (64,)).

jasonyim2 commented 2 years ago

저도 9월 초에 이 코딩이 제대로 잘 돌아갔는데, 오늘 다시 해보니까 위의 캡쳐본과 동일한 에러가 발생했습니다. 제가 프로그래밍 실력이 일천해서 혹시 해결책이 있으면 알려주시면 감사하겠습니다.

혹시나 해서 5.4절처럼 Lable을 카테고리 벡터로 바꿔봤는데도 에러 메시지는 동일합니다.

아래 모델이 에러나는 것을 방지하기 위해 이렇게 고쳐보았으나 먹히지 않음

from tensorflow.keras.utils import to_categorical train_lables = to_categorical(train_labels, num_classes=2) # 이진 클래스 벡터로 변환 valid_lables = to_categorical(valid_labels, num_classes=2) # 이진 클래스 벡터로 변환

train_lables[:10] # 심지어 0과 1 벡터도 거꾸로 나옴 (이건 추후 해결하면 되지만...)

yu901 commented 2 years ago

@jasonyim2

hyeinhs님으로 부터 아래 메일을 받았습니다. 따라하면 오류가 해결됩니다.


같은 이슈로 찾다가 create_model() 함수에 sequential 마지막에 Flatten()을 추가하여 해결하였습니다. `import tensorflow as tf from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense, LSTM, Embedding, Bidirectional, Flatten

def create_model(): model = Sequential([ Embedding(vocab_size, 32), Bidirectional(LSTM(32, return_sequences=True)), Dense(32, activation='relu'), Dense(1, activation='sigmoid'), Flatten() ]) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model

model = create_model() model.summary()`

jasonyim2 commented 2 years ago

감사합니다. 저도 StackOverflow 등에서 flatten 층 도입 이야기는 언뜻 봤는데 그게 해결책이었네요^^

lovedlim commented 2 years ago

질문과 답변 감사합니다 :)

qualis2006 commented 2 years ago

flatten() 층 추가하면 에러는 나지 않는데 학습이 제대로 되지 않는 것 같습니다.

teddylee777 commented 2 years ago

Bidirectional(LSTM(32, return_sequences=True)) 에서 시퀀스를 return 하기 때문에 데이터셋 구성을 many-to-many로 구성해야 올바르게 학습이 됩니다. flatten()을 빼고 Bidirectional(LSTM(32)) 이렇게 return_sequences=True를 제거해 주셔서 many-to-one으로 데이터셋을 구성해 주셔도 됩니다.

qualis2006 commented 2 years ago

감사합니다^^