tensorflow学习之static 您所在的位置:网站首页 tf之static tensorflow学习之static

tensorflow学习之static

2024-07-01 23:51| 来源: 网络整理| 查看: 265

tf.nn.static_bidirectional_rnn

Aliases:

tf.contrib.rnn.static_bidirectional_rnntf.nn.static_bidirectional_rnn

创建双向循环神经网络。

与单向循环神经网络类似,只不过双向循环神经网络同时接受前向和反向的RNN神经元,最终对前向和反向的输出进行深度级联,输出的格式如: [time][batch][cell_fw.output_size + cell_bw.output_size].前向和反向神经元的input_size必须匹配。默认情况下,前向和反向的初始状态为0,并且不反回任何中间状态。如果给定序列的长度,网络完全展开,如果没有给定长度,则不展开。

tf.nn.static_bidirectional_rnn(     cell_fw,     cell_bw,     inputs,     initial_state_fw=None,     initial_state_bw=None,     dtype=None,     sequence_length=None,     scope=None )

参数说明:

cell_fw:前向神经元,如BasicRNNCell.cell_bw:反向神经元input:网络输入,一个长度为T的list,list中的每个Tensor元素shape为[batch_size,input_size]initial_state_fw:可选参数。前向RNN的初始RNN,必须是合适类型的Tensor以及shape为[batch_size,cell_fw.state_size]。Initial_state_bw:可选参数。同initial_state_fw。dtype:初始状态的数据类型。sequence_length:一个int32/int64的向量,长度为[batch_size],包含每个序列的实际长度。scope:默认为”bidirectional_rnn

返回:

一个(outputs,output_state_fw,output_state_bw)的元组,其中,outputs是一个长度为T的list,list中的每个元素对应每个时间步的输出,它们是深度级联的前向和反向输出。output_state_fw是前向RNN的最终状态,output_state_bw是反向RNN的最终状态。

代码实例:

import tensorflow as tf fw_cell = tf.nn.rnn_cell.BasicRNNCell(128) bw_cell = tf.nn.rnn_cell.BasicRNNCell(256) inputs = tf.Variable(tf.random_normal([100, 40, 300]))  # [batch_size,timestep,embedding_dim] inputs = tf.unstack(inputs, 40, axis=1) outputs, fw, bw = tf.nn.static_bidirectional_rnn(fw_cell, bw_cell, inputs, dtype=tf.float32) print(len(outputs))  # 40,40个时间步 print(outputs[0].shape)  # (100, 384),每个时间步的输出神经元为384=128+256 print(outputs[1].shape)  # (100, 384) print(fw.shape)  # (100, 128),前向RNN隐藏层 print(bw.shape)  # (100, 128),后向RNN传播隐藏层

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有