Tensorflow(三十四) —— GRU

Tensorflow(三十四) —— GRU

import tensorflow as tf 
from tensorflow import keras
import numpy as np
tf.compat.v1.disable_eager_execution()
# 加载数据集
total_words = 10000
(x_train,y_train),(x_test,y_test) = keras.datasets.imdb.load_data(num_words=total_words)
print("train_shape:",x_train.shape,y_train.shape,x_test.shape,y_test.shape)
# 数据预处理
max_len = 80
x_train = keras.preprocessing.sequence.pad_sequences(x_train,maxlen=max_len,\
                                                     padding="post",truncating = "post")
"""
post 为从末尾开始阶段
"""
x_test = keras.preprocessing.sequence.pad_sequences(x_test,maxlen = max_len,\
                                                   padding = "post",truncating = "post")
db_train = tf.data.Dataset.from_tensor_slices((x_train,y_train))
db_test = tf.data.Dataset.from_tensor_slices((x_test,y_test))

def preprocess(x,y):
    x = tf.cast(x,dtype=tf.float32)
    y = tf.cast(y,dtype=tf.int32)
    return x,y

db_train = db_train.map(preprocess).shuffle(10000).batch(128,drop_remainder = True)
db_test = db_test.map(preprocess).shuffle(10000).batch(128,drop_remainder = True)
# 自定义GRU类
embedding_len = 100
class MyRNN(keras.Model):
    def __init__(self,units):
        super(MyRNN,self).__init__()
        self.embedding = keras.layers.Embedding(total_words,embedding_len,input_length=max_len)
        self.cell_1 = keras.layers.GRUCell(units)
        self.state_1 = [tf.zeros([128,units])]
        self.cell_2 = keras.layers.GRUCell(units)
        self.state_2 = [tf.zeros([128,units])]
        self.fc = keras.layers.Dense(1,activation=tf.nn.sigmoid)
        
    def call(self,inputs,training = None):
        # [b,words] => [b,words,c]
        x = inputs
        x = self.embedding(x)
        state_1 = self.state_1
        state_2 = self.state_2
        # [b,words,c] => [b,units]
        for word in tf.unstack(x,axis = 1):
            out_1,state_1 = self.cell_1(word,state_1,training)
            out_2,state_2 = self.cell_2(out_1,state_2,training)
        
        # [b,units] => [b,1]
        prob = self.fc(out_2)
        return prob
rnn_net = MyRNN(64)
rnn_net.compile(loss=tf.losses.BinaryCrossentropy(),\
                optimizer=keras.optimizers.Adam(lr = 1e-3),\
               metrics = ["accuracy"])
rnn_net.fit(db_train,epochs=10,validation_data=db_test,validation_freq=1)
print("test:",rnn_net.evaluate(db_test))
# 高阶API
embedding_len = 100
class MyRNN(keras.Model):
    def __init__(self,units):
        super(MyRNN,self).__init__()
        self.embedding = keras.layers.Embedding(total_words,embedding_len,input_length=max_len)
        self.gru = keras.Sequential([
            keras.layers.GRU(units,dropout = 0.5,return_sequences = True),
            keras.layers.GRU(units,dropout = 0.5)
        ])
        self.fc = keras.layers.Dense(1,activation=tf.nn.sigmoid)
        
    def call(self,inputs,training = None):
        # [b,words] => [b,words,c]
        x = inputs
        x = self.embedding(x)
        out = self.gru(x)
        
        # [b,units] => [b,1]
        prob = self.fc(out)
        return prob
rnn_net = MyRNN(64)
rnn_net.compile(loss=tf.losses.BinaryCrossentropy(),\
                optimizer=keras.optimizers.Adam(lr = 1e-3),\
               metrics = ["accuracy"])
rnn_net.fit(db_train,epochs=10,validation_data=db_test,validation_freq=1)
print("test:",rnn_net.evaluate(db_test))

本文为参考龙龙老师的“深度学习与TensorFlow 2入门实战“课程书写的学习笔记

by CyrusMay 2022 04 18

你可能感兴趣的:(深度学习(神经网络)专题,python,深度学习,tensorflow,算法,人工智能)