东方耀AI技术分享
标题:
58、TensorFlow中RNN(LSTM、GRU) API的使用_整体把控_笔记
[打印本页]
作者:
东方耀
时间:
2019-4-10 11:30
标题:
58、TensorFlow中RNN(LSTM、GRU) API的使用_整体把控_笔记
58、TensorFlow中RNN(LSTM、GRU) API的使用_整体把控_笔记
# -*- coding: utf-8 -*-
__author__ = u'东方耀 微信:dfy_88888'
__date__ = '2019/4/10 11:40'
__product__ = 'PyCharm'
__filename__ = 'rnn_api'
import tensorflow as tf
"""
TensorFlow中和RNN相关的API主要位于两个package:
tf.nn.rnn_cell(主要定义RNN的常见的几种细胞cell Dropout操作)、
tf.nn(RNN相关的计算执行操作)
"""
# rnn api
# tf.nn.rnn_cell. 下面放了很多类型的cell
# tf.nn. 辅助的rnn的计算工具
# RNN的中的细胞Cell(BasicRNNCell RNNCell BasicLSTMCell LSTMCell GRUCell MultiRNNCell)
# tf.nn.rnn_cell.BasicRNNCell()
# tf.nn.rnn_cell.RNNCell()
#
# tf.nn.rnn_cell.BasicLSTMCell()
# tf.nn.rnn_cell.LSTMCell()
#
# tf.nn.rnn_cell.GRUCell()
#
# tf.nn.rnn_cell.MultiRNNCell()
#
# tf.nn.rnn_cell.DropoutWrapper()
#
# tf.nn.dynamic_rnn()
# tf.nn.bidirectional_dynamic_rnn()
# 定义第一层的LSTM细胞
cell_1 = tf.nn.rnn_cell.LSTMCell(num_units=128, use_peepholes=False, state_is_tuple=True, name='cell_1')
# num_units:给定一个细胞中的各个神经层次中的神经元数目(状态维度和输出的数据维度和num_units一致)
print(cell_1.output_size)
print(cell_1.state_size)
cell_1 = tf.nn.rnn_cell.DropoutWrapper(cell_1, 0.8)
# 定义第二层的LSTM细胞 BasicLSTMCell与LSTMCell可以一起
cell_2 = tf.nn.rnn_cell.LSTMCell(num_units=256, state_is_tuple=True, name='cell_2')
cell_2 = tf.nn.rnn_cell.DropoutWrapper(cell_2, 0.7)
cells = [cell_1, cell_2]
# 将多层的LSTM合并封装成一个MultiRNNCell 作为单一的cell来处理
cell = tf.nn.rnn_cell.MultiRNNCell(cells=cells, state_is_tuple=True)
print(cell.state_size)
print(cell.output_size)
# 第一种情况:某个时刻(单个时刻)的LSTM计算
# 100表示的是每个批次或每个时刻有100个样本输入 batch_size=100
# 64表示每个样本具有64个特征属性 x1 x2 x3 .... x64
inputs = tf.placeholder(shape=(100, 64), dtype=tf.float32)
initial_state = cell.zero_state(batch_size=100, dtype=tf.float32)
cur_inp, new_states = cell.call(inputs=inputs, state=initial_state)
# cur_inp.shape [batch_size, 最后一层LSTM的细胞中神经元数量]
print(cur_inp)
print(new_states[0])
print(new_states[1])
# 第二种情况:多个时刻(序列式)的LSTM计算
inputs = tf.placeholder(shape=(100, 10, 64), dtype=tf.float32)
# inputs:一组序列(从t=0到t=T), 格式要求:[batch_size, time_steps, input_feature_num]
# 100表示的是每个批次或每个时刻有100个样本输入 batch_size=100
# 第二个维度10表示序列长度(时间长度) 一共有10个时刻 时间长度为10的序列 t1 t2 t3 .... t10
# 64表示每个样本具有64个特征属性 x1 x2 x3 .... x64 每个样本的维度数量
initial_state = cell.zero_state(batch_size=100, dtype=tf.float32)
rnn_outputs, middle_hidden_state = tf.nn.dynamic_rnn(cell=cell, inputs=inputs, initial_state=initial_state)
# rnn_outputs.shape: [batch_size, 时序数, 最后一层LSTM的细胞中神经元数量]
print('rnn_outputs shape:', rnn_outputs.shape)
print('middle_hidden_state[0]:', middle_hidden_state[0])
print('middle_hidden_state[1]:', middle_hidden_state[1])
last = rnn_outputs[:, -1, :]
print('last shape: ', last.shape)
"""
tf.nn.dynamic_rnn和tf.nn.static_rnn
一般用tf.nn.dynamic_rnn
tf.nn.dynamic_rnn:表示在每个批次中动态的构建rnn执行结构,
可以允许在不同时刻传入的数据的特征维度不同,
eg: 第一时刻传入的数据格式为:[batch_size, 10],
第二时刻传入的数据格式为:[batch_size, 12],
第三个时刻传入的数据格式为: [batch_size, 8]......;默认就是填0.
tf.nn.static_rnn: 在网络执行前,就构建好rnn的执行结构,
要求传入的数据长度必须一致,而且传入的数据必须是tensor的list集合;
构建的时候比较慢,但是执行相对比较快。
tf.nn.bidirectional_dynamic_rnn 双向动态rnn
tf.nn.static_bidirectional_rnn 双向静态rnn
"""
# tf.nn.static_rnn
#
# tf.nn.bidirectional_dynamic_rnn
# tf.nn.static_bidirectional_rnn
复制代码
作者:
gzqreder
时间:
2019-7-2 14:06
多谢分享
欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/)
Powered by Discuz! X3.4