08.keras 实现电影评价预测

keras 实现电影评价预测

import tensorflow as tf
from tensorflow import keras
import numpy as np
from matplotlib import pyplot as plt
from keras import layers
from keras import regularizers
%matplotlib inline
data = keras.datasets.imdb 
max_word = 10000   #最大取1W个单词
(x_train, y_train),(x_test,y_test) = data.load_data(num_words=max_word) %加载数据集
word_index = data.get_word_index() %获取索引
index_word=dict((value, key) for key, value in word_index. items()) %将索引和文字调换位置
[index_word.get(index-3,'?') for index in x_train[0]]  #将第一条评论转换为文字

文本向量化,有两种方式,一种是独热编码(这里不太合适)
二是k-hot编码 即[0,0,……0] 1w个,对应位置有单词就是置1 即每条评论建立一个1*10000的向量,当有对应的单词出现时,相应位置置1

def k_hot(seqs, dim=10000):
    result = np.zeros((len(seqs),dim))   #建立一个全0二维向量
    for i, seq in enumerate(seqs):      #有对应位置的单词就置为1
        result[i, seq]=1
    return result
x_train = k_hot(x_train)
x_test = k_hot(x_test)

建立神经网络模型

model = keras.Sequential()
model.add(layers.Dense(32, input_dim=10000,activation='relu'))
model.add(layers.Dense(32,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))
model.compile(optimizer='adam',
              loss='binary_crossentropy',
             metrics=['acc'])
history = model.fit(x_train, y_train, epochs=15, batch_size=256,validation_data=(x_test,y_test) )

查看结果

plt.plot(history.epoch, history.history.get('loss'),c='r',label = 'loss')
plt.plot(history.epoch, history.history.get('val_loss'),c= 'b',label = 'val_loss')
plt.legend()

在这里插入图片描述
当训练loss一直减小,测试loss 反而增高,所有严重过拟合!思考一下如何调整吧!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值