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. glance镜像服务

    一.glance介绍: 因为云平台是提供Iass层的基础设施服务,我们拿到的是一台虚拟机,那么要用虚拟机的话必须有底层的镜像做支撑,所以说镜像的话也有一个服务来管理.但是我们云平台用的镜像不是装操作系 ...

  2. python3 写CSV文件多一个空行的解决办法

    Python文档中有提到: open('eggs.csv', newline='') 也就是说,打开文件的时候多指定一个参数.Python文档中也有这样的示例: import csvwith open ...

  3. 数论+DP HDOJ 4345 Permutation

    题目传送门 题意:一个置换群,经过最少k次置换后还原.问给一个N个元素,在所有的置换群里,有多少个不同的k. 分析:这道题可以转化成:N = Σ ai ,求LCM ( ai )有多少个不同的值.比如N ...

  4. get和post中文乱码原理相关博客

    博客一:  http://blog.csdn.net/saygoodbyetoyou/article/details/16834395 博客二: http://www.jb51.net/article ...

  5. 189 Rotate Array 旋转数组

    将包含 n 个元素的数组向右旋转 k 步.例如,如果  n = 7 ,  k = 3,给定数组  [1,2,3,4,5,6,7]  ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...

  6. 微信里去掉下拉select的边框

    <select name="gender" id="" class=" " style="  -webkit-appeara ...

  7. MySQL-时间(time、date、datetime、timestamp和year)

    情景进入 情境进入: 今天调试某查询页面,偶尔发现一个问题,刚刚插入的数据,没有正常排序显示,经过后台调试sql,发现一个问题??? 经过上面红色对比,不知道你发现问题没,Order by 只是多一个 ...

  8. 最新最强短视频SDK——来自RDSDK.COM

    北京锐动天地信息技术有限公司成立于2007年9月.多年来一直专注于音视频领域核心技术的研发, 拥有Windows.iOS.Android全平台自主知识产权的领先技术产品. 2011年获得新浪战略投资, ...

  9. Scala基础篇-04 try表达式

    1.try表达式 定义 try{} catch{} finally{} //例子 try{ Integer.parseInt("dog") }catch { }finally { ...

  10. nginx负载均衡浅析

    熟悉Nginx的小伙伴都知道,Nginx是一个非常好的负载均衡器.除了用的非常普遍的Http负载均衡,Nginx还可以实现Email,FastCGI的负载均衡,甚至可以支持基于Tcp/UDP协议的各种 ...