zoukankan      html  css  js  c++  java
  • Tensorflow 方法记录

    1.tf.convert_to_tensor:传入的list必须是一个有固定长度的list,如果为2维的list,第二维的list的长度必须是固定。

    2.tf.layers.conv1d(),默认宽卷积,里面的参数filter_size,为卷积核的height,而卷积核的width为输入的width

    传入的是一个[batch_size,width,height],输出的是一个[batch_size,height,filter_num]

    3.tf.layers.conv2d(),里面的参数filter_size为一个tuple,为卷积核的大小

    传入的是一个[batch_size,width,height,channel_num],输入的是一个[batch_size,after_conv_width,afer_conv_height,filter_num]

    4.tf.stack():拼接矩阵,可以指定维数,如果指定维数的话,那么拼接的就是指定维数的数据,如果不指定维数,那么就是拼接全部。

    5.tf.unstack():拆解一个矩阵,可以指定维度。

    tensorflow tf.split 和 tf.unstack 实例

    import tensorflow as tf
    
    A = [[1, 2, 3], [4, 5, 6]]
    a0 = tf.split(A, num_or_size_splits=3, axis=1)
    a1 = tf.unstack(A, num=3,axis=1)
    a2 = tf.split(A, num_or_size_splits=2, axis=0)
    a3 = tf.unstack(A, num=2,axis=0)
    with tf.Session() as sess:
        print(sess.run(a0))
        print(sess.run(a1))
        print(sess.run(a2))
        print(sess.run(a3))

    [array([[1],[4]]), array([[2],[5]]), array([[3],[6]])] 
    [array([1, 4]), array([2, 5]), array([3, 6])] 
    [array([[1, 2, 3]]), array([[4, 5, 6]])] 
    [array([1, 2, 3]), array([4, 5, 6])]

    tf.squeeze()

    去掉维数为1的维度。 
    举个栗子:

    # 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
    tf.shape(tf.squeeze(t))  # [2, 3]

    也可以指定去掉哪个维度:

    # 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
    tf.shape(tf.squeeze(t, [2, 4]))  # [1, 2, 3, 1]

    6.tf.assign()

    https://www.jianshu.com/p/fc5f0f971b14

    7.当出现变量已经存在时,要添加tf.name_scope()

    8.tf.multiply() 点乘, 区别于tf.matul

      a.如果是一个矩阵乘以一个数,矩阵里面每一个数都乘以这个数。

      b.可以一个数乘以一个数

    9.tf.contrib.rnn.static_bidirectional_rnn:组合正序和逆序,输入: [sequence_length, batch_size, hidden_size],输出: [sequence, batch_size, 2 * output_size], forward_state: [batch_size, 2 * output_size], backward_size: [batch_size, 2 * output_size]

    encoder_outputs, forward_state, backward_state = rnn.static_bidirectional_rnn(forward_cell,
                                                            backward_cell, input_content_emb, dtype=tf.float32,
                                                            sequence_length=self.config.sequence_length)

    10.tf.greater():

      tf.greater 的输入是两个张量,此函数会比较这两个输入张量中每一个元素的大小,并返回比较结果。当tf.greater 的输入张量维度不一样时, Ten sorFlow 会进行类似NumPy 广播操作( broadcasting )的处理。

    11.tf.where()

      tf.where函数有三个参数。第一个为选择条件根据, 当选择条件为True 时, tf.where 函数会选择第二个参数中的值, 否则使用第三个参数中的值。

    12.tf.train.exponential decay()

      

      1.learning_rate:初始化的学习率

      2.global_step:当前的步数

      3.decay_steps:多少步衰减一次。

      4.decay_rate: 衰减率

      5.staricase:是否为阶梯衰减,设计为True的话, decay_steps=len(dataset)/len(batch)

    13.tf.contrib.layers.l2_regularizer(weight_decay)(weight)/tf.contrib.layers.l1_regularizer(weight_decay)(weight)

      正则化,下图为实例:

    14. tf.variable_scope() tf.get variable()

    15. tf.abs() 求绝对值

    16. tf.tile() 

    tf.tile(  
        input,     #输入  
        multiples,  #某一维度上复制的次数  
        name=None  
    )
    import tensorflow as tf
    a = tf.tile([1,2,3],[2])
    b = tf.tile([[1,2],
                 [3,4],
                 [5,6]],[2,3])
    with tf.Session() as sess:
        print(sess.run(a))
        print(sess.run(b))

    17. tf.einsum()

    # Matrix multiplication
    >>> einsum('ij,jk->ik', m0, m1)  # output[i,k] = sum_j m0[i,j] * m1[j, k]
    
    # Dot product
    >>> einsum('i,i->', u, v)  # output = sum_i u[i]*v[i]
    
    # Outer product
    >>> einsum('i,j->ij', u, v)  # output[i,j] = u[i]*v[j]
    
    # Transpose
    >>> einsum('ij->ji', m)  # output[j,i] = m[i,j]
    
    # Batch matrix multiplication
    >>> einsum('aij,ajk->aik', s, t)  # out[a,i,k] = sum_j s[a,i,j] * t[a, j, k]
  • 相关阅读:
    现代软件工程 第一章 概论 第3题——韩婧
    现代软件工程 第一章 概论 第2题——韩婧
    小组成员邓琨、白文俊、张星星、韩婧
    UVa 10892 LCM的个数 (GCD和LCM 质因数分解)
    UVa 10780 幂和阶乘 求n!中某个因子的个数
    UVa 11859 除法游戏(Nim游戏,质因子)
    Codeforces 703C Chris and Road 二分、思考
    Codeforces 703D Mishka and Interesting sum 树状数组
    hdu 5795 A Simple Nim SG函数(多校)
    hdu 5793 A Boring Question 推公式(多校)
  • 原文地址:https://www.cnblogs.com/callyblog/p/9075882.html
Copyright © 2011-2022 走看看