博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
电影评论分类--二分类问题
阅读量:6344 次
发布时间:2019-06-22

本文共 3295 字,大约阅读时间需要 10 分钟。

from keras.datasets import imdb(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words = 10000)print(train_data[0])print(len(train_data[0]))print(train_data[1])print(train_labels)max([max(sequence) for sequence in train_data])"""将评论解码为英文单词"""word_index = imdb.get_word_index()reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])print(decoded_review)"""将整数序列编码为二进制矩阵"""import numpy as npdef vectorize_sequences(sequences, dimension = 10000):    results = np.zeros((len(sequences), dimension))    for i, sequence in enumerate(sequences):        results[i, sequence] = 1.    return resultsx_train = vectorize_sequences(train_data)x_test = vectorize_sequences(test_data)x_train[0]"""将标签向量化"""y_train = np.array(train_labels).astype('float32')y_test = np.array(test_labels).astype('float32')"""构建网络"""from keras import modelsfrom keras import layersmodel = models.Sequential()model.add(layers.Dense(16, activation = 'relu', input_shape = (10000,)))model.add(layers.Dense(16, activation = 'relu'))model.add(layers.Dense(1, activation = 'sigmoid'))"""编译模型"""#model.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = ['accuracy'])"""配置优化器"""from keras import optimizersmodel.compile(optimizers.RMSprop(lr = 0.001), loss = 'binary_crossentropy', metrics = ['accuracy'])"""使用自定义的损失和指标"""from keras import lossesfrom keras import metricsmodel.compile(optimizer = optimizers.RMSprop(lr = 0.001), loss = losses.binary_crossentropy, metrics = [metrics.binary_accuracy])"""验证集"""x_val = x_train[:10000]partial_x_train = x_train[10000:]y_val = y_train[:10000]partial_y_train = y_train[10000:]"""训练模型"""model.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = ['acc'])history = model.fit(partial_x_train, partial_y_train, epochs = 20, batch_size = 512, validation_data = (x_val, y_val))"""调用model.fit()返回了一个History对象。这个对象有一个成员history,它是一个字典,包含训练过程中的所有数据。"""history_dict = history.historyprint(history_dict.keys())"""绘制训练损失和验证损失"""import matplotlib.pyplot as plthistory_dict = history.historyloss_values = history_dict['loss']val_loss_values = history_dict['val_loss']epochs = range(1, len(loss_values) + 1)plt.plot(epochs, loss_values, 'bo', label = 'Training loss')plt.plot(epochs, val_loss_values, 'b', label = 'Validation loss')plt.title('Training and validation loss')plt.xlabel('Epochs')plt.ylabel('Loss')plt.legend()plt.show()"""绘制训练精度和验证精度"""plt.clf()acc = history_dict['acc']val_acc = history_dict['val_acc']plt.plot(epochs, acc, 'bo', label = 'Training acc')plt.plot(epochs, val_acc, 'b', label = 'Validation acc')plt.title('Training and validation acuracy')plt.xlabel('Epochs')plt.ylabel('Accuracy')plt.legend()plt.show()"""重新训练一个模型"""model = models.Sequential()model.add(layers.Dense(16, activation = 'relu', input_shape = (10000,)))model.add(layers.Dense(16, activation = 'relu'))model.add(layers.Dense(1, activation = 'sigmoid'))model.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = ['accuracy'])model.fit(x_train, y_train, epochs = 4, batch_size = 512)results = model.evaluate(x_test, y_test)results"""使用训练好的网络在新数据上生成预测结果"""model.predict(x_test)

 

转载于:https://www.cnblogs.com/wbloger/p/10198159.html

你可能感兴趣的文章
[工具类]视频音频格式转换
查看>>
GNS3与抓包工具Wireshark的关联
查看>>
groovy-语句
查看>>
VIM寄存器使用
查看>>
Java VisualVM远程监控JVM
查看>>
nasm预处理器(2)
查看>>
二叉排序树 算法实验
查看>>
Silverlight 5 beta新特性探索系列:10.浏览器模式下内嵌HTML+浏览器模式下创建txt文本文件...
查看>>
YourSQLDba 配置——修改备份路径
查看>>
nginx web服务理论与实战
查看>>
java 库存 进销存 商户 多用户管理系统 SSM springmvc 项目源码
查看>>
网易音乐版轮播-react组件版本
查看>>
ES6 - 函数与剩余运算符
查看>>
你对position了解有多深?看完这2道有意思的题你就有底了...
查看>>
WebSocket跨域问题解决
查看>>
ECMAScript6基本介绍
查看>>
世界经济论坛发布关于区块链网络安全的报告
查看>>
巨杉数据库加入CNCF云原生应用计算基金会,共建开源技术生态
查看>>
Ubuntu 16.04安装Nginx
查看>>
从 JS 编译原理到作用域(链)及闭包
查看>>