TensorFlow【master】 _8.11

学习记录

7.10 安装、官网、StanfordTF Course
7.11 - StanfordTF Course
8.6 - 知乎资料学习

1.Tensorflow官网

https://www.tensorflow.org/get_started/get_started_for_beginners

安装Tensorflow

  • 直接在pytharm下安装

图和会话

  • https://www.tensorflow.org/programmers_guide/graphs
    • tf.Graph:包含图结构和图集合
    • tf.Graph 对象会为其包含的 tf.Operation 对象定义一个命名空间。TensorFlow 会自动为您的图中的每个指令选择一个唯一名称,但您也可以指定描述性名称,使您的程序阅读和调试起来更轻松。TensorFlow API 提供两种方法来覆盖操作名称:

2.Stanford TF 2017:

Lecture1

  • Tensorflow的主要特点是将定义计算(definition of computation)和执行计算(execution of computation)
    - 这里写图片描述
  • 操作、变量、常量组合在一起就是一个tensor
  • tensor只会定义一个计算图,需要通过session来fetch the value of a graph
  • 构建graph的步骤:
    • 创建新图
    • 设置为默认图(no more than one graph)
    • 添加节点
    • 这里写图片描述
  • 如果有两个graph(default_graph 和 user_graph)
    • 这里写图片描述
  • 使用graph的好处:

    • 可以只计算需要的某个子图(subgraph)
    • 是一种分布式预算的设计思路,利于分布式计算
    • 这里写图片描述
  • 调试代码时出现这个问题,明天去看看

  • Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 通过tensorflow编译安装获取底层的CPU加速接口。
  • TensorFlow如何充分使用所有CPU核数,提高TensorFlow的CPU使用率,以及Intel的MKL加速
  • 这里写图片描述

Lecture2 OPs

  • numpy broadcast特性
    • https://www.youtube.com/watch?v=9kC836XhICU
    • numpy.broadcast
    • 广播用以描述numpy中对两个形状不同的阵列进行数学计算的处理机制。较小的阵列“广播”到较大阵列相同的形状尺度上,使它们对等以进行数学计算。广播提供了一种向量化阵列的操作方式,因此Python不需要像C一样循环。广播操作不需要数据复制,通常执行效率非常高。然而,有时广播是个坏主意,可能会导致内存浪费以致计算减慢。
    • 向量被stretching to be the compatible shape
    • https://www.cnblogs.com/yangmang/p/7125458.html
    • 广播原则:如果两个数组的后缘维度(即:从末尾开始算起的维度)的轴长相符或其中一方的长度为1,则认为它们是广播兼容的,广播会在缺失和(或)长度为1的轴上进行。
    • 理解后缘轴长度
    • 感觉最好不要用,代码可读性差。

Constants

  • tf.constant(value, dtype=None, shape=None, name=’Const’, verify_shape=False)
  • Tensors filled with specific value

    • tf.zeros(shape, dtype=tf.float32, name=None)
    • tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)
    • tf.ones / tf.ones_like
    • tf.fill(dims, value, name=None)
      • tf.fill([2,3],8) => [[8,8,8],[8,8,8]]
  • Constants as sequences

    • tf.linspace()
    • tf.range(start, limit, delta, dtype, name)
  • Randomly generated constants

    • tf.random_normal()
    • tf.truncated_normal()
      • 截断正态分布是截断分布(Truncated Distribution)的一种,那么截断分布是什么?截断分布是指限制变量x取值范围(scope)的一种分布。
      • 正态分布,如果随机数偏离均值超过2个标准差,就重新随机
    • tf.random_uniform()
    • tf.random_shuffle()
      • 对张量的内容进行排序
    • tf.random_crop()
    • tf.multinomial()
    • tf.random_gamma()
    • tf.set_random_seed(seed)
  • Operations

    • 这里写图片描述
    • tf.add_n([a,b,b]) => a+b+b
    • tf.matmul(a,b) 矩阵相乘,如果维数不对应,通过 tf.reshape(a,[m,n]) 变换
  • Tensorflow Data type

    • takes Python natives types: boolean, numeric(int, float), strings
    • 这里写图片描述
      • ones_like返回与原tensor相同type的数据,如果输入是字符型,则error,因为1不是string.
    • Do not use Python native types for tensors because TF has to infer Python type.
    • Constants were stored in graph, which makes loading graphs expensive when constants are big.

Variables

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)

init_ab = tf.variables_initializer([a,b],name="init_ab")
with tf.Session() as sess:
    sess.run(init_ab)

w = tf.Variable(xxx)
with tf.Session() as sess:
    sess.run(w.initializer)
  • Eval() a variable,获取variable的取值
with tf.Session() as sess:
    sess.run(w.initializer)
    print w.eval()
  • tf.Variable.assign()
    • 赋值语句
    • .assign()是一个op,需要在session里执行过才会生效
with tf.Session() as sess:
    sess.run(w.initializer)
    sess.run(w.assign(100))
    print w.eval()
  • Use a variable to initialize another variable

    • 如果一个变量依赖另一个变量的值,需要另一个变量initialize。可以直接用:
      • u=tf.Variable(2* w.intialized_value())
  • Session vs InteractiveSession

    • The only differences is an InteractiveSession makes itself the default.
  • Control Dependencies

    • defines which ops should be run first
    • tf.Graph.control_dependencies(control_inputs)

Placeholders

  • 作用:
    • Can assemble the graph first without knowing the values needed for computation.
    • placeholder是TensorFlow的占位符节点,由placeholder方法创建,其也是一种常量,但是由用户在调用run方法是传递的,也可以将placeholder理解为一种形参。即其不像constant那样直接可以使用,需要用户传递常数值。
    • feed the values to placeholders using a dictionary.
  • tf.placeholder(dtype, shape=None, name=None)
#当你要run的session里的OP包含placeholder时,需要对placeholder赋值
a=tf.placeholder(tf.float32, shape=[3])
b=tf.constant([5,5,5], tf.float32)
c=a+b
with tf.Session() as sess:
    print sess.run(c,feed_dict={a:[1,2,3]})
- test if a tensor is feedable
    - tf.Graph.is_feedable(tensor)
  • Lazy loading

    • Put in the OP during execution
    • 这里写图片描述
    • 坏处:没有显示地显示 tf.add这个节点,不易于tensorboard的graph阅读。
  • 一个case

    • 把weight、bias、incoming设定为variable,便于optimize更新参数
    • 这里写图片描述
    • 这里写图片描述

Lecture3 Basic models

  • linear model case problems

    • optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)
    • _, l = sess.run([optimizer, loss], feed_dict={X: x, Y: y})
      • sess.run中run的对象填写要求,取_是因为该函数return的值用不到
    • tf中的条件判断:没有if a-b>d then ,需要构建condition
      • 这里写图片描述
  • logistic regression(MINST)

    • target:
      • 这里写图片描述

Lecture4 Structure your models

Lecture5 Manage your experiment

  • more word2vec
    • tf.gradients:求导公式
    • 这里写图片描述
  • 构建tf graph时可以用这样一个框架

    • structure model
      • def load_data(self)
        • feel free to add instance variables to model object that store loaded data
      • def add_placeholders(self)
        • add placeholders variables to tensorflow computational graph
      • def create_feed_dict(self, input_batch, label_batch)
        • creates the feed_dict for training the given step
      • def add_model(self, input_data)
        • implements core of model that transform input_data into predictions
      • def add_loss_op(self, pred)
        • adds ops for loss to the computational graph
      • def run_epoch(self, sess, input_data, input_labels)
        • trains the model for one-epoch
      • def fit(self, sess, input_data, input_labels)
        • fit model on the provided data
      • def predict(self, sess, input_data, input_labels=None)
        • make predictions from the provided model
    • language model(model) –从model中继承
      • def add_embedding(self)
        • add embedding layer, that maps from vocabulary to vectors
    • 代码的规范
  • manage experiments

  • tf.summary: visualize our summary statistics during our training

    • tf.summary.scalar :loss function的取值
    • tf.summary.histogram:weights
    • tf.summary.image
    • Step1:create summaries
      • 这里写图片描述
    • Step2:run them
      • 这里写图片描述
    • Step3:write summary to file
      • 这里写图片描述
    • 在tensor board中查看结果

L6 Intuition Behind Backpropagation as a Computational Graph

学习 CS231n: Convolutional Neural Networks for Visual Recognition.

Backpropagation, Intuitions

3.知乎神贴

白话TensorFlow+实战系列

  • https://zhuanlan.zhihu.com/p/26454768
  • 常用损失函数
    • 分类问题的交叉熵:
      • pred = tf.matmal(x,w)+b
      • loss = tf.nn.softmax_cross_entropy_with_logits(pred,y)
    • 回归问题的MSE:
      • loss = tf.reduce_mean(tf.square(pred-y))
  • 学习率
    • 指数衰减型:
      • learning_rate = tf.train.exponential_decay(0.1, global_step, 100, 0.98, staircase = True)
    • 过拟合
      • L1:tf.contrib.layers.l1_regularizer(lambda)(w)
      • L2:tf.contrib.layers.l2_regularizer(lambda)(w)
  • example
    • 这里写图片描述
    • 构建了一个[20,10,10,8]的全连接神经网络
    • 其中get_weight函数用于获取每一层的权重w并进行L2正则化后放入collection中进行管理。 collection 是个啥玩意儿
    • cur_net表示的是当前神经网络层。
    • in_net与out_net表示的是相邻的两层,用于构建权重。注意两层之间参数的传递和迭代
    • for循环就是构建网络过程。
    • mse_loss就是均方差损失函数。
    • 最后一并加入collection中,全部相加就得到最后的loss。
  • 滑动平均模型
    • tf.train.ExponentialMovingAverage
    • 该函数会为每一个变量生成一个影子变量(shadow_variable),影子变量的初始值即为变量的初始值,随后影子变量由该方程进行改变:
    • shadow_variable = decay * shadow_variable + (1 - decay) * variable
  • 变量管理
    • 基于字典的方法创建变量
      • 用字典的方式,key就是你取的网络层名字,value就是神经网络各层的变量。
      • 这里写图片描述
      • 使用变量时直接提取dict里的对象
      • 这里写图片描述
    • 变量共享
      • 该方法主要是通过tf.get_variable()与tf.variable_scope()函数来实现。
      • tf.get_variable() 获取变量或创建变量
      • tf.variable_scope() 创建变量的命名空间,当reuse = True时,指明在该管理器中,tf.get_variable()用于获取已经创建的变量;当reuse = False时,指明在该管理器中,tf.get_variable()用于创建变量。
      • 这里写图片描述
        • 创建时reuse=False,调用参数时设置resue=True
    • 创建变量的方法对比
      • tf.Variable() 与 tf.get_variable()
        • 共同点:都是用于在一个name_scope下获取或创建一个变量的方式
        • 区别:
          • 前者用于创建一个新变量,在同一个name_scope下可以创建相同名字的变量,底层实现会自动引入别名机制,两次调用产生两个不同的变量。
          • 后者用于获取一个变量,且不受name_scope约束,变量的name只包含variable_scope,若这个变量已存在,则自动获取;若不存在,则自动创建一个变量。
          • 如图所示,get_variable创建的变量不包含name_scope属性
          • 这里写图片描述
      • tf.variable_scope() 与 tf.name_scope()
        • name_scope用于管理一个图里的各种op,返回的是一个以scope_name命名的context manager,一个name_scope下可定义各种op活name_scope,避免各个op之间冲突。
        • variable_scope一般与get_variable()配合使用,用于管理一个graph中变量的名字,避免变量之间的命名冲突,它允许在一个variable_scope下共享变量。
    • 变量共享:
      • 在 TensorFlow 中,我们定义一个变量,相当于往 Graph 中添加了一个节点。和普通的 python 函数不一样,在一般的函数中,我们对输入进行处理,然后返回一个结果,而函数里边定义的一些局部变量我们就不管了。但是在 TensorFlow 中,我们在函数里边创建了一个变量,就是往 Graph 中添加了一个节点。出了这个函数后,这个节点还是存在于 Graph 中的。
      • https://blog.youkuaiyun.com/selous/article/details/77095155?locationNum=9&fps=1
      • 同一个项目下共享(在定义后紧接着使用),需添加 scopre.reuse_variables()
    • tf.variable, tf.placeholder 的区别
      • https://blog.youkuaiyun.com/shenxiaoming77/article/details/79141078
      • tf.placeholder() 占位符。* trainable==False *
      • tf.Variable() 一般变量用这种方式定义。 * 可以选择 trainable 类型 *
      • tf.get_variable() 一般都是和 tf.variable_scope() 配合使用,从而实现变量共享的功能。 * 可以选择 trainable 类型 *
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值