美文网首页
tensorflow常见函数

tensorflow常见函数

作者: 克里斯托弗的梦想 | 来源:发表于2019-03-13 19:47 被阅读0次
    tf.truncated_normal的用法

    tf.truncated_normal(shape, mean, stddev): shape表示生成张量的维度,mean是均值, stddev是标准差。这个函数产生正太分布、均值和标准差自己设定。

    import tensorflow as tf
    import numpy as np
    # import matplotlib.pyplot as plt
    
    c = tf.truncated_normal(shape=[10, 10], mean=0, stddev=1)
    with tf.Session() as sess:
        print(sess.run(c))
    
    tf.constant的用法

    创建一个常数张量,传入list或者数值来填充

    import tensorflow as tf
    import numpy as np
    tensor1 = tf.constant([1,2,3,4,5,6,7])        
    tensor2 = tf.constant(-1.0, shape=[2, 3])
    tensor3 = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) 
    tensor4 = tf.constant(np.arange(1, 13, dtype=np.int32), shape=[2, 2, 3])
    with tf.Session() as sess:
        print(sess.run(tensor1))
        print(sess.run(tensor2))
        print(sess.run(tensor3))
        print(sess.run(tensor4))
    
    tf.nn.conv2的用法

    tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
    参考网址:https://www.cnblogs.com/qggg/p/6832342.html
    第一个参数input: 需要做卷积的输入图像,它要求是一个tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一。
    第二个参数filter:相当于cnn中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维。
    第三个参数strides: 卷积时在图像每一维的步长,这是一个一维的向量,长度4。strides=[b,h,w,c],其中strides[0]和strides[3]默认为1。b表示在样本上的步长默认为1,也就是每一个样本都会进行运算。h表示在高度上的默认移动步长为1,这个可以自己设定,根据网络的结构合理调节。w表示在宽度上的默认移动步长为1,这个同上可以自己设定。c表示在通道上的默认移动步长为1,这个表示每一个通道都会进行运算。
    第四个参数padding: string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式。
    第五个参数: use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true

    结果返回一个Tensor,这个输出,就是我们常说的feature map,shape仍然是[batch, height, width, channels]这种形式。

    那么TensorFlow的卷积具体是怎样实现的呢,用一些例子去解释它:

    1、考虑一种最简单的情况,现在有一张3×3单通道的图像(对应的shape:[1,3,3,1]),用一个1×1的卷积核(对应的shape:[1,1,1,1])去做卷积,最后会得到一张3×3的feature map

    2、增加图片的通道数,使用一张3×3五通道的图像(对应的shape:[1,3,3,5]),用一个1×1的卷积核(对应的shape:[1,1,1,1])去做卷积,仍然是一张3×3的feature map,这就相当于每一个像素点,卷积核都与该像素点的每一个通道做卷积。

    import tensorflow as tf
    import numpy as np
    input = tf.Variable(tf.random_normal([1,3,3,5]))
    filter = tf.Variable(tf.random_normal([1,1,5,1]))
    
    op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        print(sess.run(input))
        print(sess.run(filter))
        print('_______')
        print(sess.run(op))
    

    3、把卷积核扩大,现在用3×3的卷积核做卷积,最后的输出是一个值,相当于情况2的feature map所有像素点的值求和。

    import tensorflow as tf
    import numpy as np
    input = tf.Variable(tf.random_normal([1,3,3,5]))
    filter = tf.Variable(tf.random_normal([3,3,5,1]))
    #
    op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        a = sess.run(input)
        print(sess.run(input))
        print('____________________')
        b= sess.run(filter)
        print(sess.run(filter))
        print('_______'*10)
        print(sess.run(op))
    

    4、使用更大的图片将情况2的图片扩大到5×5,仍然是3×3的卷积核,令步长为1,输出3×3的feature map

    input = tf.Variable(tf.random_normal([1,5,5,5]))
    filter = tf.Variable(tf.random_normal([3,3,5,1]))
    
    op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
    

    5、上面我们一直令参数padding的值为‘VALID’,当其为‘SAME’时,表示卷积核可以停留在图像边缘,如下,输出5×5的feature map

    input = tf.Variable(tf.random_normal([1,5,5,5]))
    filter = tf.Variable(tf.random_normal([3,3,5,1]))
    
    op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
    

    6、如果卷积核有多个

    input = tf.Variable(tf.random_normal([1,5,5,5]))
    filter = tf.Variable(tf.random_normal([3,3,5,7]))
    
    op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
    

    此时输出7张5×5的feature map
    7、步长不为1的情况,文档里说了对于图片,因为只有两维,通常strides取[1,stride,stride,1]

    input = tf.Variable(tf.random_normal([1,5,5,5]))
    filter = tf.Variable(tf.random_normal([3,3,5,7]))
    
    op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
    

    此时,输出7张3×3的feature map
    8、如果batch值不为1,同时输入10张图

    input = tf.Variable(tf.random_normal([10,5,5,5]))
    filter = tf.Variable(tf.random_normal([3,3,5,7]))
    
    op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
    

    每张图,都有7张3×3的feature map,输出的shape就是[10,3,3,7]

    tf.nn.bias_add的用法

    tf.nn.bias_add(value, bias, name=None),这个函数的作用是将偏差项bias加到value上面,是向量与矩阵的每一行进行相加,得到的结果和value矩阵大小相同。

    import tensorflow as tf
     
    a=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32)
    b=tf.constant([1,-1],dtype=tf.float32)
    c=tf.constant([1],dtype=tf.float32)
     
    with tf.Session() as sess:
        print('bias_add:')
        print(sess.run(tf.nn.bias_add(a, b)))
        #执行下面语句错误
        #print(sess.run(tf.nn.bias_add(a, c)))
    
    
    tf.add的用法

    tf.add(x, y, name=None), 矩阵x和y的数相加,就是y分别于x的每个数相加,得到结果和x大小相同,是tf.nn.bias_add的一般性。

    import tensorflow as tf    
      
    x=tf.constant([[1,2],[1,2]])    
    y=tf.constant([[1,1],[1,2]])  
    z=tf.add(x,y)  
      
    x1=tf.constant(1)  
    y1=tf.constant(2)  
    z1=tf.add(x1,y1)  
      
    x2=tf.constant(2)  
    y2=tf.constant([1,2])  
    z2=tf.add(x2,y2)  
      
    x3=tf.constant([[1,2],[3,4]])    
    y3=tf.constant([[1,1]])  
    z3=tf.add(x3,y3)  
      
    with tf.Session() as sess:  
        z_result,z1_result,z2_result,z3_result=sess.run([z,z1,z2,z3])  
        print('z =\n%s'%(z_result))  
        print('z1 =%s'%(z1_result))  
        print('z2 =%s'%(z2_result))  
        print('z3 =%s'%(z3_result))  
    
    tf.nn.relu的用法

    tf.nn.relu(a)函数是将大于0的数保持不变,小于0的数置为0。


    relu函数的图像
    import tensorflow as tf
     
    a = tf.constant([-2,-1,0,2,3])
    with tf.Session() as sess:
        print(sess.run(tf.nn.relu(a)))
    # 结果是 [0 0 0 2 3]
    
    b = tf.constant([[-2,-4],[4,-2]])
    with tf.Session() as sess:
        print(sess.run(tf.nn.relu(b)))
    # 结果是[[0, 0], [4, 0]]
    
    tf.nn.max_pool的用法

    tf.nn.max_pool(value, ksize, strides, padding, name=None)
    第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map ,依然是[batch, height, width, channels]这样的shape。
    第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为1。
    第三个参数strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride, stride, 1]
    第四个参数padding:和卷积类似,可以取VALID或者SAME。
    返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]形式。

    示例图
    import tensorflow as tf  
      
    a=tf.constant([  
            [[1.0,2.0,3.0,4.0],  
            [5.0,6.0,7.0,8.0],  
            [8.0,7.0,6.0,5.0],  
            [4.0,3.0,2.0,1.0]],  
            [[4.0,3.0,2.0,1.0],  
             [8.0,7.0,6.0,5.0],  
             [1.0,2.0,3.0,4.0],  
             [5.0,6.0,7.0,8.0]]  
        ])  
      
    a=tf.reshape(a,[1,4,4,2]) 
    pooling=tf.nn.max_pool(a, [1,2,2,1],  [1,1,1,1], padding='VALID')  
    with tf.Session() as sess:  
        print("image:")  
        image=sess.run(a)  
        print (image)  
        print("reslut:")  
        result=sess.run(pooling)  
        print (result)  
    ##### 结果
    image:  
    [[[[ 1.  2.]  
       [ 3.  4.]  
       [ 5.  6.]  
       [ 7.  8.]]  
      
      [[ 8.  7.]  
       [ 6.  5.]  
       [ 4.  3.]  
       [ 2.  1.]]  
      
      [[ 4.  3.]  
       [ 2.  1.]  
       [ 8.  7.]  
       [ 6.  5.]]  
      
      [[ 1.  2.]  
       [ 3.  4.]  
       [ 5.  6.]  
       [ 7.  8.]]]]  
    reslut:  
    [[[[ 8.  7.]  
       [ 6.  6.]  
       [ 7.  8.]]  
      
      [[ 8.  7.]  
       [ 8.  7.]  
       [ 8.  7.]]  
      
      [[ 4.  4.]  
       [ 8.  7.]  
       [ 8.  8.]]]]  
    
    池化后结果图

    当然,还可以改变步长

    pooling=tf.nn.max_pool(a, [1,2,2,1], [1,2,2,1],padding='VALID')  
    #### 结果
    reslut:  
    [[[[ 8.  7.]  
       [ 7.  8.]]  
      
      [[ 4.  4.]  
       [ 8.  8.]]]]  
    
    tf.concat的用法

    tf.concat([tensor1, tensor2, tensor3,...], axis),是用来拼接张量的函数

      t1 = [[1, 2, 3], [4, 5, 6]]
      t2 = [[7, 8, 9], [10, 11, 12]]
      tf.concat([t1, t2], 0)            # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
      tf.concat([t1, t2], 1)            # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
     
      # tensor t3 with shape [2, 3]
      # tensor t4 with shape [2, 3]
      tf.shape(tf.concat([t3, t4], 0))  # [4, 3]
      tf.shape(tf.concat([t3, t4], 1))  # [2, 6]
    
    

    这里解释当axis=0和axis=1的情况,怎么理解这个axis呢?
    axis=0 代表在第0个维度拼接
    axis=1 代表在第1个维度拼接
    对于[ [ ], [ ]]和[[ ], [ ]],低维拼接等于拿掉最外面括号,高维拼接是拿掉里面的括号(保证其他维度不变)。注意:tf.concat()拼接的张量只会改变一个维度,其他维度是保存不变的。比如两个shape为[2,3]的矩阵拼接,要么通过axis=0变成[4,3],要么通过axis=1变成[2,6]。改变的维度索引对应axis的值。

    tf.reshape的用法

    tf.reshape(tensor, shape, name=None)
    我们直接看例子

    import tensorflow as tf
    # 定义一个二维数组,总个数 num=40
    arrayList = [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                        [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
                        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ]
    input = tf.constant(arrayList)
    
    # reshape里面的参数的乘积必须等于上面数组所有参数的总和
    # 即 x*y*z = num ,比如4*2*5=40
    # 结果为一个三维数组
    output = tf.reshape(input, [4, 2, 5])
    # 当为-1时,要注意的是reshape里面最多有一个维度的值可以填写为-1,表示自动计算,比如下面就自动计算是4
    output2 = tf.reshape(input, [-1, 2, 5])
    arrays = []
    arrays.append(output)
    arrays.append(output2)
    with tf.Session() as sess:
        print('output:     reshape(4, 2, 5) ' )
        print(output)
        print(sess.run(arrays[0]))
        print('output2    reshape(-1, 2, 5) ')
        print(output2)
        print(sess.run(arrays[1]))
    
    
    结果截图

    相关文章

      网友评论

          本文标题:tensorflow常见函数

          本文链接:https://www.haomeiwen.com/subject/gtxxmqtx.html