ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문장 입력 다중클래스분류 모델 레시피(순환 컨볼루션 신경망 모델)
    Keras 2018. 1. 6. 21:35

    # 1. 패키지 불러오기

    import matplotlib.pyplot as plt
    from keras.datasets import reuters
    from keras.utils import np_utils
    from keras.preprocessing import sequence
    from keras.models import Sequential
    from keras.layers import Dense, Embedding, LSTM, Flatten, Dropout
    from keras.layers import Conv1D, MaxPooling1D
    %matplotlib inline


    # 2. 데이터 생성하기
    max_features = 15000
    text_max_words = 120


    # 2.1 훈련 데이터, 시험 데이터 불러오기
    (x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=max_features)


    # 2.2 훈련 데이터와 검증 데이터 분리하기
    x_val = x_train[7000:]
    y_val = y_train[7000:]
    x_train = x_train[:7000]
    y_train = y_train[:7000]


    # 2.3 데이터 전처리 : 문장 길이 맞추기
    x_train = sequence.pad_sequences(x_train, maxlen = text_max_words)
    x_val = sequence.pad_sequences(x_val, maxlen = text_max_words)
    x_test = sequence.pad_sequences(x_test, maxlen = text_max_words)


    # 2.4 One-hot 인코딩
    y_train = np_utils.to_categorical(y_train)
    y_val = np_utils.to_categorical(y_val)
    y_test = np_utils.to_categorical(y_test)



    # 3. 모델 구성하기
    model = Sequential()
    model.add(Embedding(max_features, 128, input_length = text_max_words))
    model.add(Dropout(0.2))
    model.add(Conv1D(256, 3, padding = "valid", activation = "relu", strides = 1))
    model.add(MaxPooling1D())
    model.add(LSTM(128))
    model.add(Dense(46, activation='softmax'))



    # 4. 모델 학습과정 설정하기
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])



    # 5. 모델 학습시키기
    hist = model.fit(x_train, y_train, epochs = 10, batch_size = 64, validation_data = (x_val, y_val))



    # 6. 학습과정 살펴보기
    fig, loss_ax = plt.subplots()
    acc_ax = loss_ax.twinx()

    loss_ax.plot(hist.history["loss"], "blue", label = "Train Loss")
    loss_ax.plot(hist.history["val_loss"], "red", label = "Validation Loss")
    loss_ax.set_ylim([-0.2, 1.2])

    acc_ax.plot(hist.history["acc"], "purple", label = "Train Accuracy")
    acc_ax.plot(hist.history["val_acc"], "green", label = "Validation Accuracy")
    acc_ax.set_ylim([-0.2, 1.2])

    loss_ax.set_xlabel("epoch")
    loss_ax.set_ylabel("loss")
    acc_ax.set_ylabel("accurach")

    loss_ax.legend(loc = "upper left")
    acc_ax.legend(loc = "lower left")

    plt.show()

    자동 대체 텍스트를 사용할 수 없습니다.


    # 7. 모델 평가하기
    loss_and_metrics = model.evaluate(x_test, y_test, batch_size = 64)
    print("## Evaluation Loss and Metrics ##")
    print(loss_and_metrics)

    자동 대체 텍스트를 사용할 수 없습니다.


    [출처] 블록과 함께하는 파이썬 딥러닝 케라스, 김태영 지음, DigitalBooks, p296~299



Designed by Tistory.