美文网首页
创建Tensor

创建Tensor

作者: 酵母小木 | 来源:发表于2020-01-29 21:39 被阅读0次

    最近在看韩剧《LIVE》,韩剧有一个致命的问题,为了传达某一主题,它总是那么浪漫,浪漫地让人忘记了生活中的一地鸡毛和一摊狗血。希望以后能够找一个自己愿意为之奋斗终身的职业而不是某种赖以生存的手段,这个职业能够让我找到人生的价值和人生的使命感。
    “我需要某种使命感,亦或是某种信仰”——2020

    1.通过numpy和List数据类型转换为Tensor

    //numpy转换为Tensor
    In [19]: tf.convert_to_tensor(np.ones([2, 3]))
    Out[19]: <tf.Tensor: id=14, shape=(2, 3), dtype=float64, numpy=
    array([[1., 1., 1.],
           [1., 1., 1.]])>
    
    In [20]: tf.convert_to_tensor(np.zeros([2, 3]))
    Out[20]: <tf.Tensor: id=15, shape=(2, 3), dtype=float64, numpy=
    array([[0., 0., 0.],
           [0., 0., 0.]])>
    
    //List转换为Tensor
    In [21]: tf.convert_to_tensor([1, 2])
    Out[21]: <tf.Tensor: id=16, shape=(2,), dtype=int32, numpy=array([1, 2])>
    
    //数据类型强制转换为浮点型
    In [22]: tf.convert_to_tensor([1, 2.])
    Out[22]: <tf.Tensor: id=17, shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>
    
    In [23]: tf.convert_to_tensor([[1], [2.]])
    Out[23]: <tf.Tensor: id=18, shape=(2, 1), dtype=float32, numpy=
    array([[1.],
           [2.]], dtype=float32)>
    

    2.tf.zeros()和tf.ones()

    //生成全为零的Tensor
    tf.zeros(
        shape,
        dtype=tf.dtypes.float32,
        name=None
    )
    //生成全为1的Tensor
    tf.ones(
        shape,
        dtype=tf.dtypes.float32,
        name=None
    )
    

    ipythontf.zeros()的练习

    In [26]: tf.zeros([])
    Out[26]: <tf.Tensor: id=19, shape=(), dtype=float32, numpy=0.0>
    
    In [27]: tf.zeros([1])
    Out[27]: <tf.Tensor: id=22, shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>
    
    In [28]: tf.zeros([2, 2])
    Out[28]:
    <tf.Tensor: id=25, shape=(2, 2), dtype=float32, numpy=
    array([[0., 0.],
           [0., 0.]], dtype=float32)>
    
    In [29]: tf.zeros([2, 3, 3])
    Out[29]:
    <tf.Tensor: id=28, shape=(2, 3, 3), dtype=float32, numpy=
    array([[[0., 0., 0.],
            [0., 0., 0.],
            [0., 0., 0.]],
    
           [[0., 0., 0.],
            [0., 0., 0.],
            [0., 0., 0.]]], dtype=float32)>
    

    ipythontf.ones()的练习

    In [35]: tf.ones([])
    Out[35]: <tf.Tensor: id=42, shape=(), dtype=float32, numpy=1.0>  #标量
    
    In [39]: tf.ones([0])
    Out[39]: <tf.Tensor: id=54, shape=(0,), dtype=float32, numpy=array([], dtype=float32)>  #空
    
    In [34]: tf.ones([1])
    Out[34]: <tf.Tensor: id=41, shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>
    
    In [38]: tf.ones([2])
    Out[38]: <tf.Tensor: id=51, shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>  
    
    In [41]: tf.ones([2, 3])
    Out[41]: <tf.Tensor: id=57, shape=(2, 3), dtype=float32, numpy=
    array([[1., 1., 1.],
           [1., 1., 1.]], dtype=float32)>
    

    3.tf.zeros_like()和tf.ones_like()

    常用于参数初始化

    // tf.zeros_like(a)等同于tf.zeros(a.shape)
    // 即创建一个形状和a相似的全为零的Tensor
    
    // tf.ones_like(a)等同于tf.ones(a.shape)
    // 即创建一个形状和a相似的全为1的Tensor
    
    In [30]: a=tf.zeros([2,3,3])
    
    In [31]: tf.zeros_like(a)
    Out[31]: <tf.Tensor: id=32, shape=(2, 3, 3), dtype=float32, numpy=
    array([[[0., 0., 0.],
            [0., 0., 0.],
            [0., 0., 0.]],
    
           [[0., 0., 0.],
            [0., 0., 0.],
            [0., 0., 0.]]], dtype=float32)>
    
    In [43]: tf.ones_like(a)
    Out[43]: <tf.Tensor: id=63, shape=(2, 3, 3), dtype=float32, numpy=
    array([[[1., 1., 1.],
            [1., 1., 1.],
            [1., 1., 1.]],
    
           [[1., 1., 1.],
            [1., 1., 1.],
            [1., 1., 1.]]], dtype=float32)>
    

    4.tf.fill()

    tf.fill(
        dims,
        value,
        name=None
    )
    

    ipython中的练习笔记

    In [44]: tf.fill([2,3],5)
    Out[44]: <tf.Tensor: id=66, shape=(2, 3), dtype=int32, numpy=
    array([[5, 5, 5],
           [5, 5, 5]])>
    

    5.随机化初始化:tf.random包

    • tf.random.normal():正态随机分布
    tf.random.normal(
        shape,      //尺寸
        mean=0.0,    //均值
        stddev=1.0,  //方差
        dtype=tf.dtypes.float32,  //数据类型
        seed=None,
        name=None
    )
    
    • tf.random.truncated_normal():正态截断随机分布

    从截断的正态分布中输出随机值。生成的值服从具有指定平均值和标准偏差的正态分布,如果生成的值与平均值之差绝对值大于2个标准偏差的值则丢弃重新选择。
    即在tf.truncated_normal中,如果x的取值在区间(μ-2σ,μ+2σ)之外则重新进行选择。

    tf.random.truncated_normal(
        shape,
        mean=0.0,
        stddev=1.0,
        dtype=tf.dtypes.float32,
        seed=None,
        name=None
    )
    
    • tf.random.uniform():均匀分布
    tf.random.uniform(
        shape,
        minval=0,      //起始值
        maxval=None,  //终止值
        dtype=tf.dtypes.float32,  //数据类型
        seed=None,
        name=None
    )
    
    • tf.random.shuffle():随机打散
    tf.random.shuffle(
        value,
        seed=None,
        name=None
    )
    

    ipythontf.ones()的练习

    In [45]: idx = tf.range(10)
    
    In [46]: idx = tf.random.shuffle(idx)
    
    In [47]: idx
    Out[47]: <tf.Tensor: id=71, shape=(10,), dtype=int32, numpy=array([1, 6, 4, 7, 9, 3, 5, 2, 8, 0])>
    
    In [48]: a = tf.random.normal([10, 784])
    
    In [49]: b = tf.random.uniform([10],maxval=10, dtype=tf.int32)
    
    In [50]: a
    Out[50]: <tf.Tensor: id=77, shape=(10, 784), dtype=float32, numpy=
    array([[-0.09842622, -0.09330572,  0.3747942 , ...,  0.12874106,
             0.5441303 , -0.00928718],
           [-0.88250256, -0.8332032 , -1.2387674 , ..., -0.7861157 ,
            -0.17430513, -0.01252976],
           [ 0.16364974,  0.9982837 , -0.92416286, ..., -0.5699464 ,
            -0.7448051 ,  0.9708245 ],
           ...,
           [-1.3529503 ,  0.13369474,  0.15024382, ...,  0.83006376,
            -0.80389315, -0.1496974 ],
           [ 1.8251408 , -1.1838558 ,  1.1211517 , ...,  1.4375882 ,
             0.31284317, -1.3510509 ],
           [ 2.702064  , -0.26362687, -1.5527179 , ...,  0.13134663,
             1.4883399 ,  1.5345211 ]], dtype=float32)>
    
    In [51]: b
    Out[51]: <tf.Tensor: id=81, shape=(10,), dtype=int32, numpy=array([6, 2, 0, 3, 9, 6, 3, 6, 7, 8])>
    
    In [52]: a = tf.gather(a, idx)
    
    In [53]: a
    Out[53]: <tf.Tensor: id=83, shape=(10, 784), dtype=float32, numpy=
    array([[-0.88250256, -0.8332032 , -1.2387674 , ..., -0.7861157 ,
            -0.17430513, -0.01252976],
           [-0.00973835, -0.49523634, -0.7052403 , ...,  0.30609828,
            -0.03025832,  0.64454794],
           [ 2.3122382 , -0.5850862 , -0.89548963, ...,  0.16975878,
            -0.37662947,  0.14918844],
           ...,
           [ 0.16364974,  0.9982837 , -0.92416286, ..., -0.5699464 ,
            -0.7448051 ,  0.9708245 ],
           [ 1.8251408 , -1.1838558 ,  1.1211517 , ...,  1.4375882 ,
             0.31284317, -1.3510509 ],
           [-0.09842622, -0.09330572,  0.3747942 , ...,  0.12874106,
             0.5441303 , -0.00928718]], dtype=float32)>
    
    In [54]: b = tf.gather(b, idx)
    
    In [55]: b
    Out[55]: <tf.Tensor: id=85, shape=(10,), dtype=int32, numpy=array([2, 3, 9, 6, 8, 3, 6, 0, 7, 6])>
    

    6.tf.constant()

    In [57]: tf.constant(1)
    Out[57]: <tf.Tensor: id=86, shape=(), dtype=int32, numpy=1> //标量
    
    In [58]: tf.constant([])
    Out[58]: <tf.Tensor: id=87, shape=(0,), dtype=float32, numpy=array([], dtype=float32)>      //空
    
    In [59]: tf.constant([1])
    Out[59]: <tf.Tensor: id=88, shape=(1,), dtype=int32, numpy=array([1])>  //数组
    
    In [64]: tf.constant([1, 2.])
    Out[64]: <tf.Tensor: id=93, shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>
    

    参考资料

    相关文章

      网友评论

          本文标题:创建Tensor

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