赞
踩
前言
SimpleRNNCell是更加底层的方法进行构造RNN神经网络来进行情感分析,能够帮助你更好的理解RNN网络
循环网络示意图:
采用了权值共享的思想
完整代码及相应注释如下:
- import os
- import tensorflow as tf
- import numpy as np
- from tensorflow import keras
- from tensorflow.keras import layers
-
- tf.random.set_seed(22)
- np.random.seed(22)
- os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
- assert tf.__version__.startswith('2.')
-
- batchsz = 128
- total_words = 10000 #常用的10000个,当出现生僻字时把它当成一个单词
- max_review_len = 80
- embedding_len = 100
- (x_train,y_train),(x_test,y_test)=keras.datasets.imdb.load_data(num_word=total_words) # 只给常见的10000个单词进行编码
- # 每个句子长度不等,将多个句子padding成相同长度的句子
- # x_train:[b,80]
- # x_test:[b,80]
- x_train = keras.preprocessing.sequence.pad_sequences(x_train,maxlen=max_review_len)
- x_test = keras.preprocessing.sequence.pad_sequences(x_test,maxlen=max_review_len)
-
- db_train = tf.data.Dataset.from_tensor_slices((x_train,y_train))
- db_train = db_train.shuffle(1000).batch(batchsz,drop_remainder=True) # 当最后一个batchsize小于128时,丢弃
- db_test = tf.data.Dataset.from_tensor_slices((x_test,y_test))
- db_test = db_test.batch(batchsz,drop_remainder=True)
- print('x_train shape:',x_train.shape,tf.reduce_max(y_train),tf.reduce_min(y_train))
- print('x_test:',x_test.shape)
-
-
- class MyRNN(keras.Model):
-
- def __init__(self,units):
- super(MyRNN,self).__init__()
-
- # [b,64]
- self.state0 = [tf.zeros([batchsz,units])]
- self.state1 = [tf.zeros([batchsz, units])]
- # [b,80] >> [b,80,100]
- self.embedding = layers.Embedding(total_words,embedding_len,input_len = max_review_len)
-
-
- # [b,80,100], h_dim:64
- # SimpleRNN
- self.rnn_cell0 = layers.SimpleRNNCell(units,dropout=0.2) #防止过拟合
- self.rnn_cell1 = layers.SimpleRNNCell(units,dropout=0.2)
-
-
- # fc [b,80,100]>>[b,64]>>[b,1]
- self.outlayer = layers.Dense(1)
-
-
- def call(self,inputs,training=None):
- '''
- net(x) net(x,training=True) :train mode
- nex(x,training = False) : test mode
- :param inputs: [b,80]
- :param training:
- :return:
- '''
- # [b,80]
- x = inputs
- # embedding : [b,80]>>[b,80,100]
- x = self.embedding(x)
- # rnn cell comute
- # [b,80,100]>>[b,64]
- state0 = self.state0
- state1 = self.state1
- for word in tf.unstack(x,axis=1): # word:[b,100] 希望在第二个维度或者单词维度展开
- # h1 = x*wxh+h*whh
- out0,state0 = self.rnn_cell0(word,state0,training) # 注意传入training参数,令其可以在不同模式下操作.及是否使用dropout
- out1,state1 = self.rnn_cell1(out0,state1)
-
-
- # out:[b,64]>>[b,1]
- x = self.outlayer(out1)
- # p(y is pos|x)
- prob = tf.sigmoid(x)
-
- return prob
-
-
-
-
- def main():
- units = 64
- epochs = 4
-
- model = MyRNN(units)
- model.compile(optimizer = keras.optimizers.Adam(0.001),
- loss = tf.losses.BinaryCrossentropy(),
- metrics = ['accuracy'])
- model.fit(db_train,epochs = epochs,validation = db_test)
-
- model.evaluate(db_test)
-
-
-
-
-
-
- if __name__ == '__main__':
- main()
本文代码和图片来自:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。