Recap

input dim, hidden dim

from tensorflow.keras import layers

# $xw_{xh} + hw_{nn}$,3次
cell = layers.SimpleRNNCell(3)
cell.build(input_shape=(None, 4)) cell.trainable_variables
[<tf.Variable 'kernel:0' shape=(4, 3) dtype=float32, numpy=
array([[-0.5311725 , 0.7757399 , -0.19041312],
[ 0.90420175, -0.14276218, 0.1546886 ],
[ 0.81770146, -0.46731013, -0.05373603],
[ 0.49086082, 0.10275221, 0.10146773]], dtype=float32)>,
<tf.Variable 'recurrent_kernel:0' shape=(3, 3) dtype=float32, numpy=
array([[ 0.7557267 , -0.58395827, 0.2964283 ],
[-0.64145935, -0.56886935, 0.5147014 ],
[-0.13193521, -0.5791204 , -0.8044953 ]], dtype=float32)>,
<tf.Variable 'bias:0' shape=(3,) dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>]

SimpleRNNCell

  • \(out,h_1 = call(x,h_0)\)

    • x: [b,seq len,word vec]

    • \(h_0/h_1: [b,h dim]\)

    • out: [b,h dim]

Single layer RNN Cell

import tensorflow as tf

x = tf.random.normal([4, 80, 100])
ht0 = x[:, 0, :] cell = tf.keras.layers.SimpleRNNCell(64) out, ht1 = cell(ht0, [tf.zeros([4, 64])]) out.shape, ht1[0].shape
[]

(TensorShape([4, 64]), TensorShape([4, 64]))
id(out), id(ht1[0])  # same id
(4877125168, 4877125168)

Multi-Layers RNN

x = tf.random.normal([4, 80, 100])
ht0 = x[:, 0, :] cell = tf.keras.layers.SimpleRNNCell(64)
cell2 = tf.keras.layers.SimpleRNNCell(64)
state0 = [tf.zeros([4, 64])]
state1 = [tf.zeros([4, 64])] out0, state0 = cell(ht0, state0)
out2, state2 = cell2(out, state2) out2.shape, state2[0].shape
(TensorShape([4, 64]), TensorShape([4, 64]))

RNN Layer

self.run = keras.Sequential([
layers.SimpleRNN(units,dropout=0.5,return_sequences=Ture,unroll=True),
layers.SimpleRNN(units,dropout=0.5,unroll=True)
])
x=self.rnn(x)

RNNCell使用的更多相关文章

  1. PyTorch官方中文文档:torch.nn

    torch.nn Parameters class torch.nn.Parameter() 艾伯特(http://www.aibbt.com/)国内第一家人工智能门户,微信公众号:aibbtcom ...

  2. TensorFlow 常用函数汇总

    本文介绍了tensorflow的常用函数,源自网上整理. TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU ...

  3. TensorFlow 常用函数与方法

    摘要:本文主要对tf的一些常用概念与方法进行描述. tf函数 TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CP ...

  4. pytorch, LSTM介绍

    本文中的RNN泛指LSTM,GRU等等 CNN中和RNN中batchSize的默认位置是不同的. CNN中:batchsize的位置是position 0. RNN中:batchsize的位置是pos ...

  5. 新版seqseq接口说明

    attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(num_units=FLAGS.rnn_hidden_size, memory = ...

  6. 学习笔记CB014:TensorFlow seq2seq模型步步进阶

    神经网络.<Make Your Own Neural Network>,用非常通俗易懂描述讲解人工神经网络原理用代码实现,试验效果非常好. 循环神经网络和LSTM.Christopher ...

  7. LSTM编程所用函数

    1.Round函数返回一个数值,该数值是按照指定的小数位数进行四舍五入运算的结果.可是当保留位跟着的即使是5,有可能进位,也有可能舍去,机会各50% 2.python基础 (1)@property 特 ...

  8. tensorflow学习之(十一)RNN+LSTM神经网络的构造

    #RNN 循环神经网络 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data tf.se ...

  9. 深度学习框架PyTorch一书的学习-第四章-神经网络工具箱nn

    参考https://github.com/chenyuntc/pytorch-book/tree/v1.0 希望大家直接到上面的网址去查看代码,下面是本人的笔记 本章介绍的nn模块是构建与autogr ...

随机推荐

  1. bzoj 4813: [Cqoi2017]小Q的棋盘【树形dp】

    这么简单的dp我怎么没想到x2 f为从这个点出发后回到这个点最多能走过的点,g为从这个点出发后不回到这个点最多能走过的点,注意g有两种转移:g[u][k]=max(g[u][k],f[u][k-j-1 ...

  2. Sping Boot返回Json格式自定义

    转载请注明http://www.cnblogs.com/majianming/p/8491020.html 在写项目过程中,遇到了需要定义返回的json字段格式的问题 例如在实体属性中,我有一个字段是 ...

  3. React.js 简介

    React.js 是一个帮助你构建页面 UI 的库.如果你熟悉 MVC 概念的话,那么 React 的组件就相当于 MVC 里面的 View.如果你不熟悉也没关系,你可以简单地理解为,React.js ...

  4. PHP PDO事务处理及MYSQLengine=InnoDB

    如果出现“#skip-innodb”则将“#”去掉,重启MySQL: 如果第一条无法解决,加上配置:default-storage-engine=InnoDB 再重启MySQL. 进入MYsql数据据 ...

  5. UnixTime的时间戳的转换

    public static string XConvertDateTime(double unixTime) { System.DateTime time = System.DateTime.MinV ...

  6. [Luogu2901][USACO08MAR]牛慢跑Cow Jogging Astar K短路

    题目链接:https://daniu.luogu.org/problem/show?pid=2901 Astar的方程$f(n)=g(n)+h(n)$,在这道题中我们可以反向最短路处理出$h(n)$的 ...

  7. R Programming week1-Data Type

    Objects R has five basic or “atomic” classes of objects: character numeric (real numbers) integer co ...

  8. rabbitmq在storm中使用

    storm中只能进行任务计算,不能保存中间结果,最后结果. 这就有一个需求,保存计算结果,最好还是分布式的,因为storm也是分布式,大数据计算. 流行的分布式计算中使用队列保存数据居多. kafka ...

  9. C/C++ 数组、字符串、string

    1.定义数组时,数组中元素的个数不能是动态的,不能用变量表示(const变量可以),必须是已知的. 2.引用数组时只能引用数组中某个元素,不能引用整个数组. 3.定义二维数组时,若同时全部初始化,则可 ...

  10. Swift 性能相关

    起初的疑问源自于「在 Swift 中的, Struct:Protocol 比 抽象类 好在哪里?」.但是找来找去都是 Swift 性能相关的东西.整理了点笔记,供大家可以参考一下. 一些疑问 在正题开 ...