Keras系列入门第五天

对imdb使用词嵌入

from keras.layers import Dense,Embedding,Flatten
import numpy as np
import keras.preprocessing as preprocessing
from keras.datasets import imdb
from keras.models import Sequential
max_features = 1000 #取常用的1000个单词数
maxlen = 20
(x_train,y_train),(x_test,y_test) = imdb.load_data(num_words=max_features)
# print(x_train) #一维列表有25000个元素 包含常用的1000个单词数
x_train = preprocessing.sequence.pad_sequences(x_train,maxlen=maxlen) #每20个元素截断一次
# print(x_train)
x_test = preprocessing.sequence.pad_sequences(x_test,maxlen=maxlen)
model = Sequential()
model.add(Embedding(max_features,8,input_length=maxlen))
model.add(Flatten())#维度变化为 (samples,maxlen*8)的二维张量
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['acc'])
model.summary()
histoty = model.fit(x_train,y_train,batch_size=32,epochs=10,validation_split=0.2)
print(histoty.history['acc'])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值