tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别

https://blog.csdn.net/u014365862/article/details/78238807

MachineLP的Github(欢迎follow):https://github.com/MachineLP

我的GitHub:https://github.com/MachineLP/train_cnn-rnn-attention 自己搭建的一个框架,包含模型有:vgg(vgg16,vgg19), resnet(resnet_v2_50,resnet_v2_101,resnet_v2_152), inception_v4, inception_resnet_v2等。

  1.  
    chunk_size = 256
  2.  
    chunk_n = 160
  3.  
    rnn_size = 256
  4.  
    num_layers = 2
  5.  
    n_output_layer = MAX_CAPTCHA*CHAR_SET_LEN # 输出层

单层rnn:

tf.contrib.rnn.static_rnn:

输入:[步长,batch,input]

输出:[n_steps,batch,n_hidden]

还有rnn中加dropout

  1.  
    def recurrent_neural_network(data):
  2.  
     
  3.  
    data = tf.reshape(data, [-1, chunk_n, chunk_size])
  4.  
    data = tf.transpose(data, [1,0,2])
  5.  
    data = tf.reshape(data, [-1, chunk_size])
  6.  
    data = tf.split(data,chunk_n)
  7.  
     
  8.  
    # 只用RNN
  9.  
    layer = {'w_':tf.Variable(tf.random_normal([rnn_size, n_output_layer])), 'b_':tf.Variable(tf.random_normal([n_output_layer]))}
  10.  
    lstm_cell = tf.contrib.rnn.BasicLSTMCell(rnn_size)
  11.  
    outputs, status = tf.contrib.rnn.static_rnn(lstm_cell, data, dtype=tf.float32)
  12.  
    # outputs = tf.transpose(outputs, [1,0,2])
  13.  
    # outputs = tf.reshape(outputs, [-1, chunk_n*rnn_size])
  14.  
    ouput = tf.add(tf.matmul(outputs[-1], layer['w_']), layer['b_'])
  15.  
     
  16.  
    return ouput

多层rnn:

tf.nn.dynamic_rnn:

输入:[batch,步长,input] 
输出:[batch,n_steps,n_hidden] 
所以我们需要tf.transpose(outputs, [1, 0, 2]),这样就可以取到最后一步的output

  1.  
    def recurrent_neural_network(data):
  2.  
    # [batch,chunk_n,input]
  3.  
    data = tf.reshape(data, [-1, chunk_n, chunk_size])
  4.  
    #data = tf.transpose(data, [1,0,2])
  5.  
    #data = tf.reshape(data, [-1, chunk_size])
  6.  
    #data = tf.split(data,chunk_n)
  7.  
     
  8.  
    # 只用RNN
  9.  
    layer = {'w_':tf.Variable(tf.random_normal([rnn_size, n_output_layer])), 'b_':tf.Variable(tf.random_normal([n_output_layer]))}
  10.  
    #1
  11.  
    # lstm_cell1 = tf.contrib.rnn.BasicLSTMCell(rnn_size)
  12.  
    # outputs1, status1 = tf.contrib.rnn.static_rnn(lstm_cell1, data, dtype=tf.float32)
  13.  
     
  14.  
    def lstm_cell():
  15.  
    return tf.contrib.rnn.LSTMCell(rnn_size)
  16.  
    def attn_cell():
  17.  
    return tf.contrib.rnn.DropoutWrapper(lstm_cell(), output_keep_prob=keep_prob)
  18.  
    # stack = tf.contrib.rnn.MultiRNNCell([attn_cell() for _ in range(0, num_layers)], state_is_tuple=True)
  19.  
    stack = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(0, num_layers)], state_is_tuple=True)
  20.  
    # outputs, _ = tf.nn.dynamic_rnn(stack, data, seq_len, dtype=tf.float32)
  21.  
    outputs, _ = tf.nn.dynamic_rnn(stack, data, dtype=tf.float32)
  22.  
    # [batch,chunk_n,rnn_size] -> [chunk_n,batch,rnn_size]
  23.  
    outputs = tf.transpose(outputs, (1, 0, 2))
  24.  
     
  25.  
    ouput = tf.add(tf.matmul(outputs[-1], layer['w_']), layer['b_'])
  26.  
     
  27.  
    return ouput

tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别的更多相关文章

  1. 深度学习原理与框架-递归神经网络-RNN网络基本框架(代码?) 1.rnn.LSTMCell(生成单层LSTM) 2.rnn.DropoutWrapper(对rnn进行dropout操作) 3.tf.contrib.rnn.MultiRNNCell(堆叠多层LSTM) 4.mlstm_cell.zero_state(state初始化) 5.mlstm_cell(进行LSTM求解)

    问题:LSTM的输出值output和state是否是一样的 1. rnn.LSTMCell(num_hidden, reuse=tf.get_variable_scope().reuse)  # 构建 ...

  2. 关于tensorflow里面的tf.contrib.rnn.BasicLSTMCell 中num_units参数问题

    这里的num_units参数并不是指这一层油多少个相互独立的时序lstm,而是lstm单元内部的几个门的参数,这几个门其实内部是一个神经网络,答案来自知乎: class TRNNConfig(obje ...

  3. tf.contrib.rnn.core_rnn_cell.BasicLSTMCell should be replaced by tf.contrib.rnn.BasicLSTMCell.

    For Tensorflow 1.2 and Keras 2.0, the line tf.contrib.rnn.core_rnn_cell.BasicLSTMCell should be repl ...

  4. tensorflow教程:tf.contrib.rnn.DropoutWrapper

    tf.contrib.rnn.DropoutWrapper Defined in tensorflow/python/ops/rnn_cell_impl.py. def __init__(self, ...

  5. tf.contrib.rnn.LSTMCell 里面参数的意义

    num_units:LSTM cell中的单元数量,即隐藏层神经元数量.use_peepholes:布尔类型,设置为True则能够使用peephole连接cell_clip:可选参数,float类型, ...

  6. tensorflow笔记6:tf.nn.dynamic_rnn 和 bidirectional_dynamic_rnn:的输出,output和state,以及如何作为decoder 的输入

    一.tf.nn.dynamic_rnn :函数使用和输出 官网:https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn 使用说明: A ...

  7. tf.nn.dynamic_rnn

    tf.nn.dynamic_rnn(cell,inputs,sequence_length=None, initial_state=None,dtype=None, parallel_iteratio ...

  8. TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例—Jason niu

    import tensorflow as tf # 22 scope (name_scope/variable_scope) from __future__ import print_function ...

  9. 第十六节,使用函数封装库tf.contrib.layers

    这一节,介绍TensorFlow中的一个封装好的高级库,里面有前面讲过的很多函数的高级封装,使用这个高级库来开发程序将会提高效率. 我们改写第十三节的程序,卷积函数我们使用tf.contrib.lay ...

随机推荐

  1. 在windows下安装配置python开发环境及Ulipad开发工具(转)

    最近开始学习Python,在网上寻找一下比较好的IDE.因为以前用C#做开发的,用Visual Studio作为IDE,鉴于用惯了VS这么强大的IDE,所以对IDE有一定的依赖性. Python的ID ...

  2. AutoFac简单入门

    AutoFac是.net程序下一个非常灵活易用,且功能强大的DI框架,本文这里简单的介绍一下使用方法. 安装: Install-Package Autofac 简单的示例: static void M ...

  3. 【Go命令教程】5. go clean

    执行 go clean 命令会删除掉执行其它命令时产生的一些文件和目录,包括: 在使用 go build 命令时在当前代码包下生成的与包名同名或者与Go源码文件同名的可执行文件.在Windows下,则 ...

  4. Apache Mina Filter

    Mina中的过滤器处于IoService与IoHandler之间,用于过滤每一个I/O事件.本文分析Mina中的过滤器是怎么串起来的? 前面提到了IoFilter,FilterChain等接口和类,在 ...

  5. python websocket-client connection

    参考:https://pypi.python.org/pypi/websocket-client/    https://www.cnblogs.com/saryli/p/6702260.html i ...

  6. Mysql5.6主从复制-基于binlog

    MySQL5.6开始主从复制有两种方式:基于日志(binlog):基于GTID(全局事务标示符). 此文章是基于日志方式的配置步骤 环境: master数据库IP:192.168.247.128sla ...

  7. AngularJS路由系列(6)-- UI-Router的嵌套State

    本系列探寻AngularJS的路由机制,在WebStorm下开发.本篇主要涉及UI-Route的嵌套State. 假设一个主视图上有两个部分视图,部分视图1和部分视图2,主视图对应着一个state,两 ...

  8. <c:otherwise>

    <c:if>没有<c:else>可以用<c:choose>来取代结构:<c:choose> <c:when test=""&g ...

  9. ExtJS动态设置表头

    if(document.getElementById("lxdj_radio").checked){ colQd = new Ext.grid.ColumnModel(colMAr ...

  10. python测试开发django-21.admin后台表名称和字段显示中文

    前言 admin后台页面表名称(默认会多加一个s)和字段名称是直接显示在后台的,如果我们想设置成中文显示需加verbose_name和verbose_name_plural属性 verbose_nam ...