IMDB Classification on Keras
2024-09-02 16:06:03
IMDB Classification on Keras
In the book of Deep Learning with Python, there is an example of IMDB move reviews sentiment classification.
# encoding:utf8
from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Embedding, Dense, LSTM
from sklearn.metrics import classification_report
vocab_size = 1000
maxlen = 100
batch_size = 32
embedding_dim = 16
epochs = 1
if __name__ == '__main__':
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)
# The shape of x_train and x_test are (25000,)
# The shape of y_train and y_test are (25000,)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
# The shape of x_train and x_test are (25000, 100)
model = Sequential()
model.add(Embedding(vocab_size, embedding_dim))
model.add(LSTM(embedding_dim))
model.add(Dense(1, activation='sigmoid'))
model.compile(
optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['acc']
)
model.fit(
x_train,
y_train,
epochs=epochs,
batch_size=batch_size,
validation_split=0.2
)
y_pred = model.predict_classes(x_test)
print(classification_report(y_test, y_pred, target_names=['positive', 'negative']))
最新文章
- 图片轮播(bootstrap)与 圆角搜索框(纯css)
- 4-Spark高级数据分析-第四章 用决策树算法预测森林植被
- 64位系统里的IIS运行32位ODP.NET的方法
- Html 字体大小单位 px em pt
- Selenium Grid 学习笔记
- 06 SQL执行计划
- 正尝试在 OS 载入程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内执行托管代码,这样做会导致应用程序挂起。
- jquery知识 内部 外部插入元素
- STRUCTS 2 LABLE
- 自动开机和自动关机设定方法(包括linux和windows)
- 多线程学习之五超时模式Timer
- mongoDB3--mongoDB的基本操作。
- scroll抖动问题
- Linux基础(四)
- DDGScreenShot—截取图片的任意部分
- Entity Framework查询
- python---django中url访问方法
- 两条Find指令
- postman的使用方法详解!最全面的教程
- python初步学习-python函数(一)