美文网首页
tensorflow基础02--常用函数

tensorflow基础02--常用函数

作者: tu7jako | 来源:发表于2020-05-24 13:51 被阅读0次

    1. 强制tensor转换为该数据类型

    tf.cast(张量名,dtype=数据类型)
    

    2. 张量最值计算

    计算张量维度上的最小值

    tf.reduce_min(张量名)
    

    计算张量维度上的最大值

    tf.reduce_max(张量名)
    
    import tensorflow as tf
    
    a = tf.constant([3, 4, 5, 6, 7], dtype=tf.float32)
    print(a)
    b = tf.cast(a, dtype=tf.float64)
    print(b)
    print(tf.reduce_min(b))
    print(tf.reduce_max(b))
    

    运行结果:

    tf.Tensor([3. 4. 5. 6. 7.], shape=(5,), dtype=float32)
    tf.Tensor([3. 4. 5. 6. 7.], shape=(5,), dtype=float64)
    tf.Tensor(3.0, shape=(), dtype=float64)
    tf.Tensor(7.0, shape=(), dtype=float64)
    

    3. 张量的轴

    axis:在二维张量或者数组中,用axis=0或axis=1控制执行维度。axis=0表示跨行,axis=1表示跨列(0对第一个维度进行操作,1对第二个维度进行操作)

    计算张量沿着指定维度的平均值

    tf.reduce_mean(张量名,axis=操作轴)
    

    计算沿着指定维度的和

    tf.reduce_sum(张量名,axis=操作轴)
    
    import tensorflow as tf
    
    a = tf.constant([[1, 2, 3, 4],
                     [5, 6, 7, 8],
                     [9, 10, 11, 12]])
    print(tf.reduce_mean(a))
    print(tf.reduce_mean(a, axis=0))
    print(tf.reduce_mean(a, axis=1))
    print(tf.reduce_sum(a, axis=0))
    print(tf.reduce_sum(a, axis=1))
    print(tf.reduce_sum(a))
    

    运行结果:

    tf.Tensor(6, shape=(), dtype=int32)
    tf.Tensor([5 6 7 8], shape=(4,), dtype=int32)
    tf.Tensor([ 2  6 10], shape=(3,), dtype=int32)
    tf.Tensor([15 18 21 24], shape=(4,), dtype=int32)
    tf.Tensor([10 26 42], shape=(3,), dtype=int32)
    tf.Tensor(78, shape=(), dtype=int32)
    

    4. Variable

    tf.Variable()将变量标记为“可训练”,被标记的变量会在反向传播中记录梯度信息。神经网络训练中,常用该函数标记训练参数。

    w = tf.Variable(tf.random.normal([3, 5], mean=0, stddev=1))
    # 运行结果
    <tf.Variable 'Variable:0' shape=(3, 5) dtype=float32, numpy=
    array([[-0.7211323 , -0.42148843, -1.0543721 , -1.1280293 , -1.1020839 ],
           [-0.35192478,  1.0499264 , -0.32882023,  0.8757452 , -0.0855011 ],
           [-1.2367997 , -1.0482794 ,  0.5232124 ,  0.09402131,  0.25787485]],
          dtype=float32)>
    

    5. 常用数学函数

    • 四则运算:tf.add, tf.subtract, tf.multiply, tf.divide
    • 平方、次方、开方: tf.square, tf.pow, tf.sqrt
    • 矩阵乘: tf.matmul

    只有维度相同的张量才可以进行四则运算

    import tensorflow as tf
    
    a = tf.ones([2, 3])
    b = tf.fill([2, 3], 5.)
    
    print(a)
    print(b)
    print(tf.add(a, b))
    print(tf.subtract(a, b))
    print(tf.multiply(a, b))
    print(tf.divide(a, b))
    

    运行结果:

    # a
    tf.Tensor(
    [[1. 1. 1.]
     [1. 1. 1.]], shape=(2, 3), dtype=float32)
    
    # b
    tf.Tensor(
    [[5. 5. 5.]
     [5. 5. 5.]], shape=(2, 3), dtype=float32)
    
    # a + b
    tf.Tensor(
    [[6. 6. 6.]
     [6. 6. 6.]], shape=(2, 3), dtype=float32)
    
    # a - b
    tf.Tensor(
    [[-4. -4. -4.]
     [-4. -4. -4.]], shape=(2, 3), dtype=float32)
    
    # a * b
    tf.Tensor(
    [[5. 5. 5.]
     [5. 5. 5.]], shape=(2, 3), dtype=float32)
    
    # a / b
    tf.Tensor(
    [[0.2 0.2 0.2]
     [0.2 0.2 0.2]], shape=(2, 3), dtype=float32)
    
    import tensorflow as tf
    
    a = tf.fill((3, 4), 5.)
    
    print(a)
    print(tf.square(a))
    print(tf.pow(a, 3))
    print(tf.sqrt(a))
    

    运行结果:

    tf.Tensor(
    [[25. 25. 25. 25.]
     [25. 25. 25. 25.]
     [25. 25. 25. 25.]], shape=(3, 4), dtype=float32)
    tf.Tensor(
    [[125. 125. 125. 125.]
     [125. 125. 125. 125.]
     [125. 125. 125. 125.]], shape=(3, 4), dtype=float32)
    tf.Tensor(
    [[2.236068 2.236068 2.236068 2.236068]
     [2.236068 2.236068 2.236068 2.236068]
     [2.236068 2.236068 2.236068 2.236068]], shape=(3, 4), dtype=float32)
    
    import tensorflow as tf
    
    a = tf.constant([[1, 1, 1, 1],
                     [1, 3, 4, 2],
                     [1, 2, 5, 1]], dtype=tf.float32)
    b = tf.fill([4, 3], 3.)
    
    print(a)
    print(b)
    print(tf.matmul(a, b))
    

    运算结果:

    tf.Tensor(
    [[1. 1. 1. 1.]
     [1. 3. 4. 2.]
     [1. 2. 5. 1.]], shape=(3, 4), dtype=float32)
    tf.Tensor(
    [[3. 3. 3.]
     [3. 3. 3.]
     [3. 3. 3.]
     [3. 3. 3.]], shape=(4, 3), dtype=float32)
    tf.Tensor(
    [[12. 12. 12.]
     [30. 30. 30.]
     [27. 27. 27.]], shape=(3, 3), dtype=float32)
    

    6. tf.data.Dataset.from_tensor_slices

    切分传入张量的第一维度,生成输入特征/标签对,构建数据集(兼容numpy格式)

    import tensorflow as tf
    
    features = tf.constant([23, 34, 24, 12])
    labels = tf.constant([1, 0, 1, 0])
    
    dataset = tf.data.Dataset.from_tensor_slices((features, labels))
    print(dataset)
    
    for ele in dataset:
        print(ele)
    

    运行结果:

    <TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)>
    
    (<tf.Tensor: shape=(), dtype=int32, numpy=23>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
    (<tf.Tensor: shape=(), dtype=int32, numpy=34>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
    (<tf.Tensor: shape=(), dtype=int32, numpy=24>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
    (<tf.Tensor: shape=(), dtype=int32, numpy=12>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
    

    7. tf.GradientTape

    with结构记录计算过程,gradient求出张量的梯度

    with tf.GradientTape() as tape:
          若干计算过程
    grad = tape.gradient(函数, 对谁求导)
    
    import tensorflow as tf
    
    with tf.GradientTape() as tape:
        w = tf.Variable(tf.constant(3.0))
        loss = tf.pow(w, 2)
    grad = tape.gradient(loss, w)
    print(grad)
    

    运行结果:

    tf.Tensor(6.0, shape=(), dtype=float32)
    

    8. tf.one_hot

    tf.one_hot()函数将待转换数据,转换为one-hot形式的数据输出。

    import tensorflow as tf
    
    labels = tf.constant([1, 0, 2])
    output = tf.one_hot(labels, depth=3)
    print(output)
    

    运行结果:

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

    9. tf.assign_sub

    • 赋值操作,更新参数的值并返回
    • 调用assign_sub前,先用tf.Variable定义变量为可训练(可自更新)
    import tensorflow as tf
    
    w = tf.Variable(4)
    w.assign_sub(1)
    print(w)
    

    运行结果:

    <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>
    

    10. tf.argmax

    返回张量沿着指定维度最大值的索引

    tf.argmax(张量名, axis=操作轴)
    
    import numpy as np
    
    a = np.array([[1, 3, 6],
                  [2, 5, 8],
                  [2, 4, 8],
                  [6, 3, 2]])
    print(a)
    print(tf.argmax(a, axis=0))
    print(tf.argmax(a, axis=1))
    

    运行结果:

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

    tf.argmin同理可得

    相关文章

      网友评论

          本文标题:tensorflow基础02--常用函数

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