Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)
David Cecchini
Data Scientist
# Build and compile the model model = Sequential()model.add(Embedding(10000, 128))model.add(LSTM(128, dropout=0.2))model.add(Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
동일한 아키텍처를 사용할 수 있습니다
# Build the model model = Sequential() model.add(Embedding(10000, 128)) model.add(LSTM(128, dropout=0.2))# Output layer has `num_classes` units and uses `softmax` model.add(Dense(num_classes, activation="softmax"))# Compile the model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) ...
20 News Groups 데이터셋
sklearn.datasets import fetch_20newsgroups에서 제공됨# Import the function to load the data from sklearn.datasets import fetch_20newsgroups# Download train and test sets news_train = fetch_20newsgroups(subset='train')news_test = fetch_20newsgroups(subset='test')
데이터의 주요 속성:
news_train.DESCR: 문서.news_train.data: 텍스트 데이터.news_train.filenames: 디스크상의 파일 경로.news_train.target: 클래스의 숫자 인덱스.news_train.target_names: 클래스 고유 이름.# Import modules from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical# Create and fit the tokenizer tokenizer = Tokenizer() tokenizer.fit_on_texts(news_train.data)# Create the (X, Y) variables X_train = tokenizer.texts_to_sequences(news_train.data) X_train = pad_sequences(X_train, maxlen=400) Y_train = to_categorical(news_train.target)
학습 데이터로 모델 학습
# Train the model
model.fit(X_train, Y_train,
batch_size=64, epochs=100)
# Evaluate on test data
model.evaluate(X_test, Y_test)
Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)