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. Linux学习笔记04—IP配置

    一.自动获取IP只有一种情况可以自动获取IP地址,那就是你的Linux所在的网络环境中有DHCP服务.只要你的真机可以自动获取IP,那么安装在虚拟机的Linux同样也可以自动获取IP. 方法很简单,只 ...

  2. Graph database_neo4j 底层存储结构分析(1)

    1       neo4j 中节点和关系的物理存储模型 1.1  neo4j存储模型 The node records contain only a pointer to their first pr ...

  3. What is CMSIS-DAP

    The mbed HDK and mbed-enabled hardware support the CMSIS-DAP debug interface, which consists of an a ...

  4. [Go] 单元测试/性能测试 (go test)

    特征 Golang 单元测试对文件名和方法名,参数都有很严格的要求.例如: 1.文件名必须以 _test.go 结尾 2.方法名必须是 Test 开头 3.方法参数必须是 t *testing.T 或 ...

  5. [廖雪峰] Git 分支管理(3):分支管理策略

    通常,合并分支时,如果可能,Git 会用 Fast forward 模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制 禁用 Fast forward 模式,Git 就会在 merge 时生 ...

  6. Snmp学习总结(三)——Win7安装和配置SNMP

    一.安装SNMP Win7操作系统默认情况下是不安装SNMP服务的,今天讲解一下在Win7操作系统下安装SNMP,具体安装步骤如下: WIN7操作系统下安装SNMP的步骤如下: 开始安装SNMP

  7. AngularJS自定义Directive与controller的交互

    有时候,自定义的Directive中需要调用controller中的方法,即Directive与controller有一定的耦合度. 比如有如下的一个controller: app.controlle ...

  8. .net连mysql数据库汇总

    另外MySql官方出了一个在csharp里面连接MySql的Connector,可以试试 http://dev.mysql.com/downloads/#connector-net <add n ...

  9. Windows Phone本地数据库(SQLCE):8、DataContext(翻译)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第八篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  10. Monitoring an IBM JVM with VisualVM

    Monitoring an IBM JVM with VisualVM 分类: Java 2013-06-09 16:15 250人阅读 评论(0) 收藏 举报 JDK6 update 7 and o ...