zoukankan      html  css  js  c++  java
  • TensorFlow 张量变换

    原文链接:https://www.w3cschool.cn/tensorflow_python/tensorflow_python-85v22c69.html

    import tensorflow as tf
    tf.enable_eager_execution()
    
    
    # tf.string_to_number
    str_tensor = tf.constant(["2.2", "1.8"])
    flt32_tesnor = tf.string_to_number(str_tensor, out_type=tf.float32)
    print(flt32_tesnor)  # tf.Tensor([2.2 1.8], shape=(2,), dtype=float32)
    
    # tf.to_double
    db_tensor = tf.to_double(flt32_tesnor)
    print(db_tensor)  # tf.Tensor([2.20000005 1.79999995], shape=(2,), dtype=float64)
    
    # tf.cast
    int32_tensor = tf.cast(db_tensor, tf.int32)
    print(int32_tensor)  # tf.Tensor([2 1], shape=(2,), dtype=int32)
    
    # TensorFlow 张量形状的确定与改变
    # tf.broadcast_dynamic_shape
    tensor_x = tf.ones(shape=[1, 2, 3], dtype=tf.int32)
    tensor_y = tf.ones(shape=[5, 1, 3], dtype=tf.int32)
    shape_z = tf.broadcast_dynamic_shape(tensor_x.shape, tensor_y.shape)
    print(shape_z)  # tf.Tensor([5 2 3], shape=(3,), dtype=int32)
    
    # tf.broadcast_dynamic_shape
    shape_w = tf.broadcast_static_shape(tensor_x.shape, tensor_y.shape)
    print(shape_w)  # (5, 2, 3)
    
    # tf.shape
    t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
    print(tf.shape(t))  # tf.Tensor([2 2 3], shape=(3,), dtype=int32)
    
    # tf.shape_n
    print(tf.shape_n(t))  # (1)[ <tf.Tensor: id=31, shape=(2,), dtype=int32, numpy=array([2, 3], dtype=int32)>,
                          # (2)  <tf.Tensor: id=32, shape=(2,), dtype=int32, numpy=array([2, 3], dtype=int32)>]
                          # 形状是:(2) [2, 3]
    
    # tf.size
    print(tf.size(t))  # tf.Tensor(12, shape=(), dtype=int32)
    
    # tf.rank, 相当于np.ndim
    # 张量的秩, 比如shape是[2,2,3],则秩(维度)为3
    print(tf.rank(t))  # tf.Tensor(3, shape=(), dtype=int32)
    
    # t的shape是[2, 2, 3]
    print(tf.reshape(t, [4, 3]))
    """
    tf.Tensor(
    [[1 1 1]
     [2 2 2]
     [3 3 3]
     [4 4 4]], shape=(4, 3), dtype=int32)
    """
    
    print(tf.reshape(t, [-1]))  # [-1] can be used to flatten 't'
    """
    tf.Tensor([1 1 1 2 2 2 3 3 3 4 4 4], shape=(12,), dtype=int32)
    """
    
    print(tf.reshape(t, [3, -1]))  # -1 can be used to infer the shape
    """
    tf.Tensor(
    [[1 1 1 2]
     [2 2 3 3]
     [3 4 4 4]], shape=(3, 4), dtype=int32)
    """
    
    print(tf.reshape(tf.constant([[[7]]]), []))  # shape `[]` reshapes to a scalar
    """
    tf.Tensor(7, shape=(), dtype=int32)
    """
    
    
    # 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]
    
    # 't' is a tensor of shape [2]
    tf.shape(tf.expand_dims(t, 0))  # ==> [1, 2]
    tf.shape(tf.expand_dims(t, 1))  # ==> [2, 1]
    tf.shape(tf.expand_dims(t, -1))  # ==> [2, 1]
    
    # 't' is a tensor of shape [2, 3, 5]
    tf.shape(tf.expand_dims(t, 0))  # ==> [1, 2, 3, 5]
    tf.shape(tf.expand_dims(t, 2))  # ==> [2, 3, 1, 5]
    tf.shape(tf.expand_dims(t, 3))  # ==> [2, 3, 5, 1]
    
    """
    tf.tile(
        input,
        multiples,
        name=None
    )
    """
    
  • 相关阅读:
    docker 部署aps.net MVC到windows容器
    docker 搭建私有仓库 harbor
    解决关于:Oracle数据库 插入数据中文乱码 显示问号???
    ionic cordova build android error: commamd failed with exit code eacces
    cordova build android Command failed with exit code EACCES
    Xcode 10 iOS12 "A valid provisioning profile for this executable was not found
    使用remix发布部署 发币 智能合约
    区块链: 编译发布智能合约
    mac 下常用命令备忘录
    JQuery fullCalendar 时间差 排序获取距当前最近的时间。
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/13460804.html
Copyright © 2011-2022 走看看