빅데이터/머신 러닝 with Python (old)

Keras를 이용한 RNN - 인터넷 영화 리뷰로 감정 분류 학습

언제나휴일 2020. 8. 19. 21:06
반응형

인터넷 영화 리뷰 데이터(IMDB) 구조 파악 코드

from keras.datasets import imdb

(x_train, y_train), (x_test, y_test) = imdb.load_data()
print("결과 종류:",max(y_train)+1) #0은 부정, 1은 긍정
len_result = [len(s) for s in x_train]
print(len_result)
word_to_index = imdb.get_word_index()
index_to_word={}
for key, value in word_to_index.items():
    index_to_word[value] = key
cnt=0
for iv in X_train[0]:
    print(index_to_word[iv],end=' ')
    cnt = cnt+1
    if cnt==10:
        cnt=0
        print()

 

감정 분석 학습 및 평가

from keras.datasets import imdb
from keras.preprocessing import sequence
from keras import layers,models
from keras import metrics

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=20000)
x_train = sequence.pad_sequences(x_train,maxlen=80)
x_test = sequence.pad_sequences(x_test,maxlen=80)


x = layers.Input((80,))
h = layers.Embedding(20000,128)(x)
lstm = layers.LSTM(128,dropout=0.2,recurrent_dropout=0.2)
h = lstm(h)
dense = layers.Dense(1,activation='sigmoid')
y = dense(h)
model = models.Model(x,y)

model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

model.fit(x_train,y_train,batch_size=32,epochs=3)
score,acc = model.evaluate(x_test,y_test,batch_size=32)
print("score:{0} acc:{1}".format(score,acc))
반응형