码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-10-03 01:08:53      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:cto   cti   optimizer   set   model   int   mod   __name__   for   

 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自带影评数据集进行评价正面与否的二分类训练

标签:cto   cti   optimizer   set   model   int   mod   __name__   for   

原文地址:https://www.cnblogs.com/rising-sun/p/11618859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!