zoukankan      html  css  js  c++  java
  • IMDB Classification on Keras

    IMDB Classification on Keras

    In the book of Deep Learning with Python, there is an example of IMDB move reviews sentiment classification.

    # encoding:utf8
    
    from keras.datasets import imdb
    from keras.preprocessing import sequence
    from keras.models import Sequential
    from keras.layers import Embedding, Dense, LSTM
    from sklearn.metrics import classification_report
    
    
    vocab_size = 1000
    maxlen = 100
    batch_size = 32
    embedding_dim = 16
    epochs = 1
    
    
    if __name__ == '__main__':
        (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)
        # The shape of x_train and x_test are (25000,)
        # The shape of y_train and y_test are (25000,)
        
        x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
        x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
        # The shape of x_train and x_test are (25000, 100)
    
        model = Sequential()
        model.add(Embedding(vocab_size, embedding_dim))
        model.add(LSTM(embedding_dim))
        model.add(Dense(1, activation='sigmoid'))
    
        model.compile(
            optimizer='rmsprop',
            loss='binary_crossentropy',
            metrics=['acc']
        )
        model.fit(
            x_train,
            y_train,
            epochs=epochs,
            batch_size=batch_size,
            validation_split=0.2
        )
    
        y_pred = model.predict_classes(x_test)
        print(classification_report(y_test, y_pred, target_names=['positive', 'negative']))
    
    
  • 相关阅读:
    软件工程结对作业02
    软件工程个人作业04
    第五周学习进度条
    软件工程中的形式化方法
    需求工程
    软件过程
    软件项目管理
    软件概论概述
    人月神话读后略有感想
    软件工程—理论、方法和实践 第一章:概述
  • 原文地址:https://www.cnblogs.com/fengyubo/p/11286464.html
Copyright © 2011-2022 走看看