【问题标题】:Classifying text using RNN keras使用 RNN keras 对文本进行分类
【发布时间】:2018-06-12 10:47:39
【问题描述】:

我试图理解RNN for text classification using keras/tensorflow。目前,它可以对正面/负面情绪进行分类。我怎么能把它改成其他类?例如 2 个类,QuestionNot-Question

    # LSTM for sequence classification in the IMDB dataset
    import numpy
    from keras.datasets import imdb
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.layers import LSTM
    from keras.layers.embeddings import Embedding
    from keras.preprocessing import sequence
    # fix random seed for reproducibility
    numpy.random.seed(7)

    # load the dataset but only keep the top n words, zero the rest
    top_words = 5000

    (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)

    # truncate and pad input sequences
    max_review_length = 500
    X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)

    X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
    # create the model
    embedding_vecor_length = 32
    model = Sequential()
    model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
    model.add(LSTM(100))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    print(model.summary())
    model.fit(X_train, y_train, epochs=3, batch_size=64)
    # Final evaluation of the model
    scores = model.evaluate(X_test, y_test, verbose=0)
    print("Accuracy: %.2f%%" % (scores[1]*100))

【问题讨论】:

    标签: python tensorflow keras rnn


    【解决方案1】:

    当你在最后一层有Dense(1, activation='sigmoid')loss='binary_crossentropy' 时,你正在做一个二元、2 类分类。因此,您可以使用相同的模型来学习有问题、无问题的设置。

    如果您想要超过 2 个类别,那么我们经常使用 Dense(num_classes, activation='softmax')loss='categorical_crossentropy',它们会生成可能类别的概率分布。实际上,二元分类只是Dense(2, activation='softmax')的一个特例,类为[1,0]和`[0,1]',即目标类的one-hot编码。

    【讨论】:

    • 好的。我有两个名为“问题”和non-question 的目录,用于测试和培训。我怎样才能加载这些数据?
    猜你喜欢
    • 2017-05-10
    • 2017-08-02
    • 2016-12-27
    • 2018-08-20
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多