利用keras自带影评数据集进行评价正面与否的二分类训练

Posted rising-sun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用keras自带影评数据集进行评价正面与否的二分类训练相关的知识,希望对你有一定的参考价值。

 1 from keras.datasets import imdb
 2 from keras import layers
 3 from keras import models
 4 from keras import optimizers
 5 import matplotlib.pyplot as plt
 6 import numpy as np
 7 
 8 def vectorize_data(x, dim = 10000):
 9     res = np.zeros([len(x), dim])
10     for i, string in enumerate(x):
11         res[i, string] = 1
12     return res
13 def main():
14     (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
15     x_train = vectorize_data(train_data)
16     y_train = np.asanyarray(train_labels).astype(float32)
17     x_test = vectorize_data(test_data)
18     y_test = np.asarray(test_labels).astype(float32)
19 
20     network = models.Sequential()
21     network.add(layers.Dense(16, activation = relu, input_shape = (10000, )))
22     network.add(layers.Dense(16, activation = relu))
23     network.add(layers.Dense(1, activation = sigmoid))
24 
25     network.compile(optimizer = rmsprop, loss = binary_crossentropy, metrics = [accuracy])
26     history = network.fit(x_train, y_train, batch_size = 512, epochs = 4)
27 
28     loss, acc = network.evaluate(x_test, y_test)
29 
30     print(acc == , acc)
31 if __name__ == "__main__":
32     main()

 

以上是关于利用keras自带影评数据集进行评价正面与否的二分类训练的主要内容,如果未能解决你的问题,请参考以下文章

人工智能--使用神经网络分析电影评论

利用keras自带房价数据集进行房价预测

利用keras自带路透社数据集进行多分类训练

IMDB影评倾向分类 - N-Gram

循环神经网络实践—文本分类

循环神经网络实践—文本分类