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 3629: [JLOI2014]聪明的燕姿【线性筛+dfs】

    数论+爆搜 详见这位大佬https://blog.csdn.net/eolv99/article/details/39644419 #include<iostream> #include& ...

  2. hdu1115【多边形求重心模板】

    1.质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心(∑( xi×mi ) / ∑mi, ∑( yi×mi ) / ∑mi) 2.质量分布均匀.这个题就是这一类型,算法和上面的不同. ...

  3. 【react-native】持续踩坑总结

    陆陆续续的已经接触了RN快3个月,整体的感受...感觉在调试兼容andorid问题的时候就像回到了IE时代. 本来想按自己踩坑的路径持续更新一些记录,但是,现实是坑太多,还是统一写一篇汇总一下吧(鉴于 ...

  4. [ZPG TEST 110] 多边形个数【DP】

    1. 多边形个数 (polygons.pas/c/cpp) [问题描述] 给定N线段,编号1到n.并给出这些线段的长度,用这些线段组成一个K边形,并且每个线段做多使用一次.若使用了一条不同编号的线段, ...

  5. 题解报告:hdu 2086 A1 = ?

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2086 Problem Description 有如下方程:Ai = (Ai-1 + Ai+1)/2 - ...

  6. 为HttpClient和HttpURLConnection添加中国移动代理

    转自: http://www.2cto.com/kf/201111/112100.html 在android中,一般需要联网的时候前,都要做一次网络的判断,判断当前的网络状态!然后开始请求网络 当我们 ...

  7. Kali linux 2016.2(Rolling)里的枚举服务

    前言 枚举是一类程序,它允许用户从一个网络中收集某一类的所有相关服务.

  8. spring实现模板文件下载

    前台 <form id="batchModel0" method="post" action="/common/download-file&qu ...

  9. Android开源项目:GifView——Android显示GIF动画

    下载:http://code.google.com/p/gifview/downloads/list 简介:android中现在没有直接显示gif的view,只能通过mediaplay来显示,且还常常 ...

  10. 使用VirtualBox的时候虚拟机无法ping通windows主机,但是主机可以ping通虚拟机

    问题原因是windows开启了防火墙导致的,将windows的防火墙关闭即可. 关闭windows防火墙后会有警告的信息出现,直接无视即可.