文章目录

  • 1.GRU的原理
  • 2.GRU实战
    • 1.layer
    • 2.cell

1.GRU的原理

2.GRU实战

1.layer

import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras import layerstf.random.set_seed(22)
np.random.seed(22)
assert tf.__version__.startswith('2.')batchsz = 128# the most frequest words
total_words = 10000
max_review_len = 80
embedding_len = 100
(x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data(num_words=total_words)
# x_train:[b, 80]
# x_test: [b, 80]
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_review_len)
x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_review_len)db_train = tf.data.Dataset.from_tensor_slices((x_train, y_train))
db_train = db_train.shuffle(1000).batch(batchsz, drop_remainder=True)
db_test = tf.data.Dataset.from_tensor_slices((x_test, y_test))
db_test = db_test.batch(batchsz, drop_remainder=True)
print('x_train shape:', x_train.shape, tf.reduce_max(y_train), tf.reduce_min(y_train))
print('x_test shape:', x_test.shape)class MyRNN(keras.Model):def __init__(self, units):super(MyRNN, self).__init__()# transform text to embedding representation# [b, 80] => [b, 80, 100]self.embedding = layers.Embedding(total_words, embedding_len,input_length=max_review_len)# [b, 80, 100] , h_dim: 64self.rnn = keras.Sequential([# layers.SimpleRNN(units, dropout=0.5, return_sequences=True, unroll=True),# layers.SimpleRNN(units, dropout=0.5, unroll=True)# unroll: Boolean (default False). If True, the network will be unrolled,# else a symbolic loop will be used.# Unrolling can speed-up a RNN, although it tends to be more memory-intensive.# Unrolling is only suitable for short sequences.layers.GRU(units, dropout=0.5, return_sequences=True, unroll=False),layers.GRU(units, dropout=0.5, unroll=False)])# fc, [b, 80, 100] => [b, 64] => [b, 1]self.outlayer = layers.Dense(1)def call(self, inputs, training=None):"""net(x) net(x, training=True) :train modenet(x, training=False): test:param inputs: [b, 80]:param training::return:"""# [b, 80]x = inputs# embedding: [b, 80] => [b, 80, 100]x = self.embedding(x)# rnn cell compute# x: [b, 80, 100] => [b, 64]x = self.rnn(x)# out: [b, 64] => [b, 1]x = self.outlayer(x)# p(y is pos|x)prob = tf.sigmoid(x)return probdef main():units = 64epochs = 4import timet0 = time.time()model = MyRNN(units)model.compile(optimizer=keras.optimizers.Adam(0.001),loss=tf.losses.BinaryCrossentropy(),metrics=['accuracy'],experimental_run_tf_function=False)model.fit(db_train, epochs=epochs, validation_data=db_test)model.evaluate(db_test)t1 = time.time()# Unroll=True# LSTM: 69.3 secnods, 83%# GRU: 100 seconds, 83.4%# Unroll=False# LSTM:23.71, 81.24# GRU 23.05, 83.11# 观测结果与官方文档不一致print('total time cost:', t1 - t0)if __name__ == '__main__':main()

2.cell

import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras import layerstf.random.set_seed(22)
np.random.seed(22)
assert tf.__version__.startswith('2.')batchsz = 128# the most frequest words
total_words = 10000
max_review_len = 80
embedding_len = 100
(x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data(num_words=total_words)
# x_train:[b, 80]
# x_test: [b, 80]
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_review_len)
x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_review_len)db_train = tf.data.Dataset.from_tensor_slices((x_train, y_train))
db_train = db_train.shuffle(1000).batch(batchsz, drop_remainder=True)
db_test = tf.data.Dataset.from_tensor_slices((x_test, y_test))
db_test = db_test.batch(batchsz, drop_remainder=True)
print('x_train shape:', x_train.shape, tf.reduce_max(y_train), tf.reduce_min(y_train))
print('x_test shape:', x_test.shape)class MyRNN(keras.Model):def __init__(self, units):super(MyRNN, self).__init__()# [b, 64]self.state0 = [tf.zeros([batchsz, units])]self.state1 = [tf.zeros([batchsz, units])]# transform text to embedding representation# [b, 80] => [b, 80, 100]self.embedding = layers.Embedding(total_words, embedding_len,input_length=max_review_len)# [b, 80, 100] , h_dim: 64# RNN: cell1 ,cell2, cell3# SimpleRNN# self.rnn_cell0 = layers.SimpleRNNCell(units, dropout=0.5)# self.rnn_cell1 = layers.SimpleRNNCell(units, dropout=0.5)self.rnn_cell0 = layers.GRUCell(units, dropout=0.5)self.rnn_cell1 = layers.GRUCell(units, dropout=0.5)# fc, [b, 80, 100] => [b, 64] => [b, 1]self.outlayer = layers.Dense(1)def call(self, inputs, training=None):"""net(x) net(x, training=True) :train modenet(x, training=False): test:param inputs: [b, 80]:param training::return:"""# [b, 80]x = inputs# embedding: [b, 80] => [b, 80, 100]x = self.embedding(x)# rnn cell compute# [b, 80, 100] => [b, 64]state0 = self.state0state1 = self.state1for word in tf.unstack(x, axis=1):  # word: [b, 100]# h1 = x*wxh+h0*whh# out0: [b, 64]out0, state0 = self.rnn_cell0(word, state0, training)# out1: [b, 64]out1, state1 = self.rnn_cell1(out0, state1, training)# out: [b, 64] => [b, 1]x = self.outlayer(out1)# p(y is pos|x)prob = tf.sigmoid(x)return probdef main():units = 64epochs = 4import timet0 = time.time()model = MyRNN(units)model.compile(optimizer=keras.optimizers.Adam(0.001),loss=tf.losses.BinaryCrossentropy(),metrics=['accuracy'],experimental_run_tf_function=False)model.fit(db_train, epochs=epochs, validation_data=db_test)model.evaluate(db_test)t1 = time.time()# LSTM: 64.3 seconds, 83.4%# GRU:  96.7s, 83.4%print('total time cost:', t1 - t0)if __name__ == '__main__':main()

深度学习2.0-41.GRU原理及实战相关推荐

  1. 深度学习 LSTM长短期记忆网络原理与Pytorch手写数字识别

    深度学习 LSTM长短期记忆网络原理与Pytorch手写数字识别 一.前言 二.网络结构 三.可解释性 四.记忆主线 五.遗忘门 六.输入门 七.输出门 八.手写数字识别实战 8.1 引入依赖库 8. ...

  2. 深度学习时间序列预测:GRU算法构建单变量时间序列预测模型+代码实战

    深度学习时间序列预测:GRU算法构建单变量时间序列预测模型+代码实战 GRU(Gate Recurrent Unit)是循环神经网络(Recurrent Neural Network, RNN)的一种 ...

  3. 深度学习时间序列预测:GRU算法构建多变量时间序列预测模型+代码实战

    深度学习时间序列预测:GRU算法构建多变量时间序列预测模型+代码实战 注意参考:深度学习多变量时间序列预测:GRU算法构建单变量时间序列预测模型+代码实战 GRU(Gate Recurrent Uni ...

  4. 深度学习笔记(41) 候选区域

    深度学习笔记(41) 候选区域 1. 无对象区域 2. 候选区域 3. Faster R-CNN 1. 无对象区域 记得滑动窗法吧,使用训练过的分类器 在这些窗口中全部运行一遍,然后运行一个检测器,看 ...

  5. halcon 深度学习标注_HALCON深度学习工具0.4 早鸟版发布了

    原标题:HALCON深度学习工具0.4 早鸟版发布了 HALOCN深度学习工具在整个深度学习过程中扮演着重要的作用,而且在将来将扮演更重要的辅助作用,大大加快深度学习的开发流程,目前发布版本工具的主要 ...

  6. halcon显示坐标_HALCON深度学习工具0.4 早鸟版发布了

    HALOCN深度学习工具在整个深度学习过程中扮演着重要的作用,而且在将来将扮演更重要的辅助作用,大大加快深度学习的开发流程,目前发布版本工具的主要作用是图像数据处理和目标检测和分类中的标注. 标注训练 ...

  7. python 主语_前深度学习时代--FFM模型的原理与Python实现

    基于上一篇分析中协同过滤.逻辑回归及FM的比较,可以得出这样一个结论: 主流模型迭代的关键在于增强模型表达能力,而增强方式的主要脉络为: 引入其它可用特征信息(CF->LR). 将现有特征进行组 ...

  8. 系统学习深度学习(四) --CNN原理,推导及实现源码分析

    之前看机器学习中,多层感知器部分,提到可以在设计多层感知器时,对NN的结构设计优化,例如结构化设计和权重共享,当时还没了解深度学习,现在看到CNN,原来CNN就是这方面的一个代表.CNN由纽约大学的Y ...

  9. 深度学习-目标检测YOLOV3 YOLOv4原理详解

    2.YoloV3核心基础内容 2.1 网络结构可视化 Yolov3是目标检测Yolo系列非常非常经典的算法,不过很多同学拿到Yolov3或者Yolov4的cfg文件时,并不知道如何直观的可视化查看网络 ...

  10. 亚马逊Alexa的深度学习与语音识别的核心技术原理

    相关推荐 随着语音巨头抢占语音入口,蓝牙设备智能化已成为下一波竞争的关键,而身处其中的无线蓝牙耳机正披着"智能... 发表于 2018-04-04 10:34 • 37次阅读 训练数据来自于 ...

最新文章

  1. weblogic不用密码启动服务
  2. 读书感想--list/BSS等等
  3. 【Python-ML】神经网络-深度学习库Keras
  4. ic读卡器设置工具_IC设计工程师的职业前景真的有别人说的那么好吗?
  5. 总结2-深度学习网络搭建学习
  6. ionic Toggle(开关控件)
  7. python调用window dll和linux so例子
  8. 山东法律学校97级二班计算机班,关于表彰全国三好学生、全国优秀学生干部和全国先进班集体及其标兵的决定...
  9. 微信公众号、微信小程序、 微信扫码支付、微信委托代扣模式等相关的开发文档
  10. git merge fast-forward squash no-ff
  11. Hadoop 简介 及 安装
  12. FH162儿童电子液晶手写板方案芯片开发
  13. 实验三-RIPv2基本配置实验
  14. Linux--gcc的基本用法
  15. 控件测试之单行文本框测试
  16. “橄榄型”人口结构制约消费增长
  17. JAVA转go系列之我为什么要学习GO
  18. 如何一键拼图?不妨试试这三个一键拼图软件
  19. 【宇麦科技】群晖NAS套件之Drive的客户端安装与配置(二),新手必读!
  20. DNS污染——domain name的解析被劫持了返回无效的ip

热门文章

  1. 20190825 On Java8 第十三章 函数式编程
  2. 使用php函数防止SQL注入方法
  3. ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus
  4. PBXCp Error
  5. hexo+github
  6. as 与 is 的区别
  7. android中的Package替换流程
  8. JustForFly struts2标签s:generator
  9. BZOJ1301 字符加密Cipher (后缀数组)
  10. SpringMVC----视图层框架