tensorflow2.0学习笔记: RNN 循环神经网络

基于IMDB数据实现文本情感分类循环神经网络:

  1. 简单的RNN:keras.layers.SimpleRNN()
  2. 双向RNN: keras.layers.Bidirectional(keras.layers.SimpleRNN())
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf

from tensorflow import keras

print(tf.__version__)
imdb = keras.datasets.imdb
vocab_size = 10000
index_from = 3
(train_data,train_labels),(test_data,test_labels) = imdb.load_data(
    num_words = vocab_size,index_from = index_from)
word_index = imdb.get_word_index()
print(len(word_index))
# print(word_index)
word_index = {k:(v+3) for k,v in word_index.items()}
word_index[''] = 0
word_index[''] = 1
word_index[''] = 2
word_index[''] = 3

reverse_word_index = dict([(value,key) for key,value in word_index.items()])

def decode_review(text_ids):
    return ''.join([reverse_word_index.get(word_id, '') 
                    for word_id in text_ids])
decode_review(train_data[0])
max_length = 500

train_data = keras.preprocessing.sequence.pad_sequences(
    train_data, #list of list
    value=word_index[''],
    padding='post',# post:放在后面,pre:放在前面
    maxlen = max_length
)

test_data = keras.preprocessing.sequence.pad_sequences(
    test_data, #list of list
    value=word_index[''],
    padding='post',# post:放在后面,pre:放在前面
    maxlen = max_length)

1. 简单RNN

embedding_dim = 16
batch_size = 128
single_rnn_model = keras.models.Sequential([
    
    keras.layers.Embedding(vocab_size,embedding_dim,input_length = max_length),
    
    keras.layers.SimpleRNN(units = 64, return_sequences = False),#False 只返回最后一层
    
    keras.layers.Dense(64,activation='relu'),
    keras.layers.Dense(1,activation='sigmoid')
])

single_rnn_model.summary()
single_rnn_model.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])

epochs = 30
history_rnn_single = single_rnn_model.fit(train_data,train_labels,epochs = epochs,
                    batch_size = batch_size,
                    validation_split = 0.2)

2. 双向RNN

embedding_dim = 16
batch_size = 128
model = keras.models.Sequential([
    keras.layers.Embedding(vocab_size,embedding_dim,input_length = max_length),
    keras.layers.Bidirectional(
        keras.layers.SimpleRNN(units = 64, return_sequences = True)),# True 返回多层
    keras.layers.Bidirectional(
        keras.layers.SimpleRNN(units = 64, return_sequences = False)),
    
    keras.layers.Dense(64,activation='relu'),
    keras.layers.Dense(1,activation='sigmoid')
])

model.summary()
model.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])

epochs = 30
history = model.fit(train_data,train_labels,epochs = epochs,
                    batch_size = batch_size,
                    validation_split = 0.2)

你可能感兴趣的:(tensorflow2,tensorflow,rnn,深度学习)