TensorFlow电子文档
大小:317 KB已被114人关注
TensorFlow2.0深度强化学习指南是一个不错的学习资源,大小为317 KB,由籍宏博 提供,TensorFlow类资源中评分为7.4。
Tags:深度学习 TensorFlow
TensorFlow虽是深度学习领域最广泛使用的框架,但是对比PyTorch这一动态图框架,采用静态图(Graph模式)的TensorFlow确实是难用。好在最近TensorFlow支持了eager模式,对标PyTorch的动态执行机制。更进一步地,Google在最近推出了全新的版本TensorFlow 2.0,2.0版本相比1.0版本不是简单地更新,而是一次重大升级(虽然目前只发布了preview版本)。简单地来说,TensorFlow 2.0默认采用eager执行模式,而且重整了很多混乱的模块。毫无疑问,2.0版本将会逐渐替换1.0版本,所以很有必要趁早入手TensorFlow 2.0。
《TensorFlow2.0深度强化学习指南》将通过实施AdvantageActor-Critic(演员-评论家,A2C)代理来解决经典的CartPole-v0环境,通过深度强化学习(DRL)展示即将推出的TensorFlow2.0特性。虽然我们的目标是展示TensorFlow2.0,但我将尽最大努力让DRL的讲解更加平易近人,包括对该领域的简要概述。本文主要介绍了如何用深度强化学习来展示TensorFlow2.0的强大特性,希望对您的学习有所帮助。
给大家精选了网上关于《TensorFlow2.0深度强化学习指南》的学习笔记心得及相关实例内容,值得大家学习参考。
TensorFlow变量管理详解
一、TensorFlow变量管理
1. TensorFLow还提供了tf.get_variable函数来创建或者获取变量,tf.variable用于创建变量时,其功能和tf.Variable基本是等价的。tf.get_variable中的初始化方法(initializer)的参数和tf.Variable的初始化过程也类似,initializer函数和tf.Variable的初始化方法是一一对应的,详见下表。
tf.get_variable和tf.Variable最大的区别就在于指定变量名称的参数。对于tf.Variable函数,变量名称是一个可选的参数,通过name=”v”的形式给出,对于tf.get_variable函数,变量名称是一个必填的参数,tf.get_variable会根据这个名称去创建或者获取变量。
2. 通过tf.variable_scope函数可以控制tf.get_variable函数的语义。当tf.variable_scope函数的参数reuse=True生成上下文管理器时,该上下文管理器内的所有的tf.get_variable函数会直接获取已经创建的变量,如果变量不存在则报错;当tf.variable_scope函数的参数reuse=False或者None时创建的上下文管理器中,tf.get_variable函数则直接创建新的变量,若同名的变量已经存在则报错。
3. 另tf.variable_scope函数是可以嵌套使用的。嵌套的时候,若某层上下文管理器未声明reuse参数,则该层上下文管理器的reuse参数与其外层保持一致。
4.tf.variable_scope函数提供了一个管理变量命名空间的方式。在tf.variable_scope中创建的变量,名称.name中名称前面会加入命名空间的名称,并通过“/”来分隔命名空间的名称和变量的名称。tf.get_variable("foou/baru/u", [1]),可以通过带命名空间名称的变量名来获取其命名空间下的变量。
二、TensorFlow编程演示
import tensorflow as tf # 在名字为foo的命名空间内创建名字为v的变量 with tf.variable_scope("foo"): v = tf.get_variable("v", [1], initializer=tf.constant_initializer(1.0)) ''''' # 因为命名空间foo内已经存在变量v,再次创建则报错 with tf.variable_scope("foo"): v = tf.get_variable("v", [1]) # ValueError: Variable foo/v already exists, disallowed. # Did you mean to set reuse=True in VarScope? ''' # 将参数reuse参数设置为True,则tf.get_variable可直接获取已声明的变量 with tf.variable_scope("foo", reuse=True): v1 = tf.get_variable("v", [1]) print(v == v1) # True ''''' # 当reuse=True时,tf.get_variable只能获取指定命名空间内的已创建的变量 with tf.variable_scope("bar", reuse=True): v2 = tf.get_variable("v", [1]) # ValueError: Variable bar/v does not exist, or was not created with # tf.get_variable(). Did you mean to set reuse=None in VarScope? ''' with tf.variable_scope("root"): # 通过tf.get_variable_scope().reuse函数获取当前上下文管理器内的reuse参数取值 print(tf.get_variable_scope().reuse) # False with tf.variable_scope("foo1", reuse=True): print(tf.get_variable_scope().reuse) # True with tf.variable_scope("bar1"): # 嵌套在上下文管理器foo1内的bar1内未指定reuse参数,则保持与外层一致 print(tf.get_variable_scope().reuse) # True print(tf.get_variable_scope().reuse) # False # tf.variable_scope函数提供了一个管理变量命名空间的方式 u1 = tf.get_variable("u", [1]) print(u1.name) # u:0 with tf.variable_scope("foou"): u2 = tf.get_variable("u", [1]) print(u2.name) # foou/u:0 with tf.variable_scope("foou"): with tf.variable_scope("baru"): u3 = tf.get_variable("u", [1]) print(u3.name) # foou/baru/u:0 u4 = tf.get_variable("u1", [1]) print(u4.name) # foou/u1:0 # 可直接通过带命名空间名称的变量名来获取其命名空间下的变量 with tf.variable_scope("", reuse=True): u5 = tf.get_variable("foou/baru/u", [1]) print(u5.name) # foou/baru/u:0 print(u5 == u3) # True u6 = tf.get_variable("foou/u1", [1]) print(u6.name) # foou/u1:0 print(u6 == u4) # True
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持码农之家。
展开 +
收起 -
本文介绍了tensorflow中next_batch的具体使用,分享给大家,具体如下: 此处给出了几种不同的next_batch方法,该文章只是做出代码片段的解释,以备以后查看: def next_batch(self, batch_size, fake_data=False): """Return the next `batch_size` examples from this data set.""" if fake_data: fake_image = [1] * 784 if self.one_hot: fake_label = [1] + [0] * 9 else: fake_label = 0 return [fake_image for _ in xrange(batch_size)], [ fake_label for _ in xrange(batch_size) ] start = self._index_in_epoch self._index_in_epoch += batch_size if self._index_in_epoch self._num_examples: # epoch中的句子下标是否大于所有语料的个数,如果为True,开始新一轮的遍历 # Finished epoch self._epochs_completed += 1 # Shuffle the data perm = numpy.……
蒯新月
Copyright 2018-2021 www.xz577.com 码农之家
版权投诉 / 书籍推广 / 赞助:520161757@qq.com
Tensorflow简单验证码识别应用
简单的Tensorflow验证码识别应用,供大家参考,具体内容如下 1.Tensorflow的安装方式 简单,在此就不赘述了. 2.训练集训练集 以及测试及如下(纯手工打造,所以数量不多): 3.实现代码部分 (参考了网上的一些实现来完成的) main.py(主要的神经网络代码) from gen_check_code import gen_captcha_text_and_image_new,gen_captcha_text_and_imagefrom gen_check_code import numberfrom test_check_code import get_test_captcha_text_and_imageimport numpy as npimport tensorflow as tftext, image = gen_captcha_text_and_image_new()print("验证码图像channel:", image.shape) # (60, 160, 3) # 图像大小 IMAGE_HEIGHT = image.shape[0]IMAGE_WIDTH = image.shape[1]image_shape = image.shapeMAX_CAPTCHA = len(text)print("验证码文本最长字符数……