• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    TensorFlow2基本操作之合并分割与统计
    POST TIME:2021-10-18 12:55

    合并与分割

    tf.concat

    tf.concat可以帮助我们实现拼接操作.

    格式:

    tf.concat(
        values, axis, name='concat'
    )
    

    参数:

    例子:

    part_1 = tf.zeros([5, 3])
    print(part_1)
    
    part_2 = tf.ones([5, 3])
    print(part_2)
    
    # 竖向拼接
    result_1 = tf.concat([part_1, part_2], axis=0)
    print(result_1)
    
    # 横向拼接
    result_2 = tf.concat([part_1, part_2], axis=1)
    print(result_2)
    

    输出结果:

    tf.Tensor(
    [[0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]], shape=(5, 3), dtype=float32)
    tf.Tensor(
    [[1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]], shape=(5, 3), dtype=float32)
    tf.Tensor(
    [[0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]], shape=(10, 3), dtype=float32)
    tf.Tensor(
    [[0. 0. 0. 1. 1. 1.]
    [0. 0. 0. 1. 1. 1.]
    [0. 0. 0. 1. 1. 1.]
    [0. 0. 0. 1. 1. 1.]
    [0. 0. 0. 1. 1. 1.]], shape=(5, 6), dtype=float32)

    tf.stack

    rf.stack可以创建一个新的维度来合并两个张量.

    格式:

    tf.stack(
        values, axis=0, name='stack'
    )
    

    参数:

    例子:

    part_1 = tf.zeros([5, 3])
    print(part_1)
    
    part_2 = tf.ones([5, 3])
    print(part_2)
    
    # 头拼接
    result_1 = tf.stack([part_1, part_2], axis=0)
    print(result_1)
    
    # 尾拼接
    result_2 = tf.stack([part_1, part_2], axis=2)
    print(result_2)
    

    输出结果:

    tf.Tensor(
    [[0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]], shape=(5, 3), dtype=float32)
    tf.Tensor(
    [[1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]], shape=(5, 3), dtype=float32)
    tf.Tensor(
    [[[0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]]

    [[1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
    tf.Tensor(
    [[[0. 1.]
    [0. 1.]
    [0. 1.]]

    [[0. 1.]
    [0. 1.]
    [0. 1.]]

    [[0. 1.]
    [0. 1.]
    [0. 1.]]

    [[0. 1.]
    [0. 1.]
    [0. 1.]]

    [[0. 1.]
    [0. 1.]
    [0. 1.]]], shape=(5, 3, 2), dtype=float32)

    tf.unstack

    tf.unstack是一个矩阵分解函数.

    格式:

    # unstack
    tf.unstack(
    value, num=None, axis=0, name='unstack'
    )

    参数:

    例子:

    a = tf.stack([tf.zeros([5, 3]), tf.ones([5, 3])], axis=0)
    print(a)
    
    b = tf.unstack(a, axis=0)
    print(b)
    

    输出结果:

    tf.Tensor(
    [[[0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]]

    [[1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
    [tf.Tensor: shape=(5, 3), dtype=float32, numpy=
    array([[0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.]], dtype=float32)>, tf.Tensor: shape=(5, 3), dtype=float32, numpy=
    array([[1., 1., 1.],
    [1., 1., 1.],
    [1., 1., 1.],
    [1., 1., 1.],
    [1., 1., 1.]], dtype=float32)>]

    tf.split

    tf.split()可以把一个张量划分为几个子张量.

    格式:

    tf.split(
        value, num_or_size_splits, axis=0, num=None, name='split'
    )
    

    参数:

    例子:

    # split
    a = tf.stack([tf.zeros([5, 3]), tf.ones([5, 3])], axis=0)
    print(a)
    
    b = tf.split(a, 2)
    print(b)
    

    输出结果:

    tf.Tensor(
    [[[0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]
    [0. 0. 0.]]

    [[1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]
    [1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
    [tf.Tensor: shape=(1, 5, 3), dtype=float32, numpy=
    array([[[0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.]]], dtype=float32)>, tf.Tensor: shape=(1, 5, 3), dtype=float32, numpy=
    array([[[1., 1., 1.],
    [1., 1., 1.],
    [1., 1., 1.],
    [1., 1., 1.],
    [1., 1., 1.]]], dtype=float32)>]

    数据统计

    tf.norm

    tf.norm可以帮助我们计算向量, 矩阵, 张量的范数.

    格式:

    tf.norm(
        tensor, ord='euclidean', axis=None, keepdims=None, name=None
    )
    

    参数:

    例子:

    a = tf.fill([2, 2], 2.0)
    print(a)
    
    # sqrt(2^2 * 4) = sqrt(16) = 4
    b = tf.norm(a)
    print(b)
    
    # [2 + 2, 2 + 2] = [4, 4]
    c = tf.norm(a, ord=1, axis= 0)
    print(c)
    
    # [sqrt(2^2 + 2^2), sqrt(2^2 + 2^2)] = [sqrt(8), sqrt(8)]
    d = tf.norm(a, ord=2, axis= 0)
    print(d)
    

    输出结果:

    tf.Tensor(
    [[2. 2.]
    [2. 2.]], shape=(2, 2), dtype=float32)
    tf.Tensor(4.0, shape=(), dtype=float32)
    tf.Tensor([4. 4.], shape=(2,), dtype=float32)
    tf.Tensor([2.828427 2.828427], shape=(2,), dtype=float32)

    reduce_min/max/mean

    计算一个张量各个维度上元素的最小值 / 最大值 / 平均值.

    格式:

    tf.math.reduce_min / reduce_max / reduce_mean(
        input_tensor, axis=None, keepdims=False, name=None
    )
    

    参数:

    例子:

    a = tf.reshape(tf.range(9), [3, 3])
    print(a)
    
    min = tf.reduce_min(a)
    print(min)
    
    max = tf.reduce_max(a)
    print(max)
    

    输出结果:

    tf.Tensor(
    [[0 1 2]
    [3 4 5]
    [6 7 8]], shape=(3, 3), dtype=int32)
    tf.Tensor(0, shape=(), dtype=int32)
    tf.Tensor(8, shape=(), dtype=int32)

    argmax / argmin

    tf.argmax/tf.argmin可以帮我们找到最大 / 最小值所在的索引 (index).

    格式:

    tf.math.argmax(
        input, axis=None, output_type=tf.dtypes.int64, name=None
    )
    

    参数:

    例子:

    # argmax / argmin
    a = tf.reshape(tf.range(9), [3, 3])
    print(a)
    
    max = tf.argmax(a)
    print(max)
    
    min = tf.argmin(a)
    print(min)
    

    输出结果:

    tf.Tensor(
    [[0 1 2]
    [3 4 5]
    [6 7 8]], shape=(3, 3), dtype=int32)
    tf.Tensor([2 2 2], shape=(3,), dtype=int64)
    tf.Tensor([0 0 0], shape=(3,), dtype=int64)

    tf.equal

    tf.equal可以帮助我们判断两个张量是否相等. 返回 True / False.

    格式:

    tf.math.equal(
        x, y, name=None
    )
    

    例子:

    a = tf.zeros(5, dtype=tf.float32)
    print(a)
    
    b = tf.range(5, dtype=tf.float32)
    print(b)
    
    print(tf.equal(a, b))
    

    输出结果:

    tf.Tensor([0. 0. 0. 0. 0.], shape=(5,), dtype=float32)
    tf.Tensor([0. 1. 2. 3. 4.], shape=(5,), dtype=float32)
    tf.Tensor([ True False False False False], shape=(5,), dtype=bool)

    tf.unique

    tf.unique可以帮我们找出张量中不重复的值

    格式:

    tf.unique(
        x, out_idx=tf.dtypes.int32, name=None
    )
    

    参数:

    例子:

    a = tf.range(5)
    print(tf.unique(a))
    
    b = tf.constant([4, 2, 2, 4, 3])
    print(tf.unique(b))
    

    输出结果:

    Unique(y=tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>, idx=tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>)
    Unique(y=tf.Tensor: shape=(3,), dtype=int32, numpy=array([4, 2, 3])>, idx=tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2])>)

    到此这篇关于一小时学会TensorFlow2基本操作之合并分割与统计的文章就介绍到这了,更多相关TensorFlow2合并分割与统计内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • 手把手教你使用TensorFlow2实现RNN
    • tensorflow2.0实现复杂神经网络(多输入多输出nn,Resnet)
    • windows系统Tensorflow2.x简单安装记录(图文)
    • 详解TensorFlow2实现前向传播
    • Python强化练习之Tensorflow2 opp算法实现月球登陆器
    上一篇:Python基础常用内建函数图文示例解析
    下一篇:TensorFlow2基本操作之 张量排序 填充与复制 查找与替换
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信