• tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask


    1.  tf.split(3, group, input)  # 拆分函数
        3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值

    import tensorflow as tf
    import numpy as np
    
    x = [[1, 2], [3, 4]]
    Y = tf.split(axis=1, num_or_size_splits=2, value=x)
    
    sess = tf.Session()
    for y in Y:
        print(sess.run(y))

    2.  tf.concat(3, input) # 串接函数
        3 表示的是在第三个维度上, input表示的是输入,输入一般都是列表

    import tensorflow as tf
    
    
    x = [[1, 2], [3, 4]]
    y = tf.concat(x, axis=0)
    
    sess = tf.Session()
    print(sess.run(y))

    3. tf.squeeze(input, squeeze_dims=[1, 2]) # 表示的是去除列数为1的维度, squeeze_dim 指定维度

    import tensorflow as tf
    import numpy as np
    
    x = [[1, 2]]
    print(np.array(x).shape)
    y = tf.squeeze(x, axis=[0])
    
    sess = tf.Session()
    
    print(sess.run(y))

    4. tf.less_equal(a, b)  a 可以是一个列表, b表示需要比较的数,如果比b大返回false,否者返回True

    import tensorflow as tf
    import numpy as np
    
    raw_gt = [1, 2, 3, 4]
    
    y = tf.where(tf.less_equal(raw_gt, 2))
    
    
    sess = tf.Session()
    
    print(sess.run(y))

    5.tf.where(input)   # 返回是真的序号,通过tf.where找出小于等于2的数的序号

    import tensorflow as tf
    import numpy as np
    
    raw_gt = [1, 2, 3, 4]
    
    
    y = tf.where(tf.less_equal(raw_gt, 2))
    
    sess = tf.Session()
    print(sess.run(y))

    6. tf.gather   # 根据序列号对数据进行取值,输入的是input, index

    import tensorflow as tf
    import numpy as np
    
    raw_gt = [3, 4, 5, 6]
    
    
    y = tf.gather(raw_gt, [[0], [1]])
    
    sess = tf.Session()
    print(sess.run(y))

     7. tf.cast(input, tf.float32)  # 主要目的是进行数值类型转换

    import tensorflow as tf
    import numpy as np
    
    raw_gt = [3.0, 4.0, 5.0, 6.0]
    
    
    y = tf.cast(raw_gt, tf.int32)
    
    sess = tf.Session()
    print(sess.run(y))

    8. tf.expand_dims(input, axis=1) # 进行矩阵维度的扩增

    import tensorflow as tf
    import numpy as np
    
    raw_gt = [3.0, 4.0, 5.0, 6.0]
    
    
    y = tf.expand_dims(raw_gt, axis=1)
    
    sess = tf.Session()
    print(sess.run(tf.shape(y)))

    9. tf.argmax(input, axis=1)  根据维度找出这个维度下的最大值的序号, axis=0 表示找出每一行中的最大值, axis=1,表示找出每一列的最大值

    import tensorflow as tf
    import numpy as np
    
    raw_gt = [[3.0, 4.0], [5.0, 3]]
    
    y = tf.argmax(raw_gt, axis=1)
    
    sess = tf.Session()
    print(sess.run(y))

     1o. tf.reshape(input, shape)  # tf.reshape主要用于数据的shape重新组合

    import tensorflow as tf
    import numpy as np
    
    
    a = [[1, 2], [3, 4]]
    y = tf.reshape(a, [-1, 4])
    
    sess = tf.Session()
    print(sess.run(y))

     11. tf.stack(input, axis)   # 进行数据的拼接,为了去除一个维度

    import tensorflow as tf
    distort_left_right_random = tf.random_uniform([1], 0, 1.0, dtype=tf.float32)[0]
    mirror = tf.less(tf.stack([1.0, 0.8, 1.0]), 0.5)
    sess = tf.Session()
    print(sess.run(mirror))
    mirror = tf.boolean_mask([0, 1, 2], mirror)
    
    
    sess = tf.Session()
    print(sess.run(mirror))

    12.tf.less(a, b)  # 如果a小于b返回True

    13.tf.boolean_mask # 找出数据中是True的位置

    mirror = tf.boolean_mask([0, 1, 2], [True, False, False])
    
    
    sess = tf.Session()
    print(sess.run(mirror))

     13 tf.slice # 根据给出的起始位置进行数据的抽取

    # tf.slice
    # import tensorflow as tf
    # import numpy as np
    # x=[[1,2,3],[4,5,6]]
    # y=np.arange(24).reshape([2,3,4])
    # z=tf.constant([[[1,2,3],[4,5,6]], [[7,8,9],[10,11,12]],  [[13,14,15],[16,17,18]]])
    # sess=tf.Session()
    # begin_x=[1,0]        #第一个1,决定了从x的第二行[4,5,6]开始,第二个0,决定了从[4,5,6] 中的4开始抽取
    # size_x=[1,2]           # 第一个1决定了,从第二行以起始位置抽取1行,也就是只抽取[4,5,6] 这一行,在这一行中从4开始抽取2个元素
    # out=tf.slice(x,begin_x,size_x)
    # print(sess.run(out))

    14 .tf.reduce_sum(input) 表示将所有的进行相加

    15 .tf.gfile.MakeDirs(train_dir)  # 根据train_dir创建文件夹

    import tensorflow as tf
    
    train_dir = 'make/'
    tf.gfile.MakeDirs(train_dir)

    16. tf.gfile.IsDirectory(train_dir)   # 判断是否是文件夹

    if tf.gfile.IsDirectory(train_dir):
        print('1')
    else:
        print('0')

    17.tf.transpose(input, [1, 0]) # 表示将第一维的大小与第二维度进行调换

    import tensorflow as tf
    import numpy as np
    
    x = [[1, 2, 3, 4], [1, 2, 4, 3]]
    print(np.shape(x))
    sess = tf.Session()
    # 进行维度的变化,perm表示将第一二维度转换为第一个维度
    print(sess.run(tf.transpose(x, perm=[1, 0])))
  • 相关阅读:
    什么是枚举?有什么作用?有什么好处?
    java获取某个范围内的一个随机数
    java中普通代码块,构造代码块,静态代码块的区别及代码示例
    Oracle数据库迁移
    linux下修改文件权限
    <%@ include file="">和<jsp:include file="">区别
    JAVA常见异常解析
    jdk环境变量配置
    jstl中fmt标签详解
    jsp脚本元素
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/10150028.html
Copyright © 2020-2023  润新知