(2)tensorflow创建张量

创建张量

综述:

功能代码
利用数组、列表对象创建张量tf.constant与tf.convert_to_tensor
创建纯0 或纯 1 张量tf.zeros()和tf.ones()
按照现有张量创建纯0 或纯 1 张量tf.zeros_like, tf.ones_like
创建自定义纯数字张量tf.fill(shape,number)
创建已知分布张量tf.random.normal(shape, mean=0.0, stddev=1.0) 和tf.random.uniform(shape, minval=0, maxval=None, dtype=tf.float32)
创建序列张量tf.range(limit, delta=1)

利用数组、列表对象创建张量

  • tf.constant与tf.convert_to_tensor都可以转换,
利用数组、列表对象创建张量实例:
import tensorflow as tf
import numpy as np
a = [1,2,3,4,6.7]
a1 = tf.constant(a)
a2 = tf.convert_to_tensor(a)
print(a,'\n',a1,'\n',a2)

b = np.array([[1,2],[3,4]])  #默认精度float64,转换后也是64位精度
b1 = tf.constant(b)
b2 = tf.convert_to_tensor(b)
print(b,'\n',b1,'\n',b2)

out:
[1, 2, 3, 4, 6.7] 
 tf.Tensor([1.  2.  3.  4.  6.7], shape=(5,), dtype=float32) 
 tf.Tensor([1.  2.  3.  4.  6.7], shape=(5,), dtype=float32)
 
[[1 2]
 [3 4]] 
 tf.Tensor(
[[1 2]
 [3 4]], shape=(2, 2), dtype=int32)
 tf.Tensor(
[[1 2]
 [3 4]], shape=(2, 2), dtype=int32)
 

创建纯0 或纯 1 张量

  • tf.zeros()和tf.ones() 括号内填入shape信息
  • tf.zeros_like, tf.ones_like 可以方便地新建与某个张量 shape 一致, 且内容为全 0 、全 1 的张量,等价于tf.zeros(a.shape)
演示tf.zeros()和tf.ones()实例:
import tensorflow as tf
a = tf.zeros([2,3])
b = tf.ones([2,3,8])
print(a,'\n',b)

out:

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


 tf.Tensor(
[[[1. 1. 1. 1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1. 1. 1. 1.]]

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


演示tf.zeros_like, tf.ones_like 实例:
import tensorflow as tf
b = tf.ones([2,3,8])
c = tf.zeros_like(b)
print(b,'\n',c)

out:
tf.Tensor(
[[[1. 1. 1. 1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1. 1. 1. 1.]]

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

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

创建自定义纯数字张量

  • tf.fill(shape,number)
import tensorflow as tf
b = tf.fill([2,3,8],12)
print(b)

out:
tf.Tensor(
[[[12 12 12 12 12 12 12 12]
  [12 12 12 12 12 12 12 12]
  [12 12 12 12 12 12 12 12]]

 [[12 12 12 12 12 12 12 12]
  [12 12 12 12 12 12 12 12]
  [12 12 12 12 12 12 12 12]]], shape=(2, 3, 8), dtype=int32)

创建已知分布张量

  • tf.random.normal(shape, mean=0.0, stddev=1.0) 正太分布,默认为标准正太分布
  • tf.random.uniform(shape, minval=0, maxval=None, dtype=tf.float32) 均匀分布,默认0-1,指定dtype=tf.int32可以整数取样
import tensorflow as tf
a = tf.random.normal([2,4,3], mean=0.0, stddev=2.0)
b = tf.random.uniform([2,3,6],minval=0,maxval=10,dtype=tf.float64)
c = tf.random.uniform([2,3,6],minval=0,maxval=10,dtype=tf.int32)
print(a,'\n',b,'\n',c)

out:
tf.Tensor(
[[[-0.487546   -0.9160655  -2.5163028 ]
  [-0.53561485 -1.3563609  -0.07336067]
  [-1.1784577   0.43411794  0.39190876]
  [-0.50878274  1.0863602   1.497958  ]]

 [[-1.7565571  -1.8858548   0.9200668 ]
  [ 0.09523225 -1.1106855   0.9887945 ]
  [ 1.2684689  -1.207795   -0.2490597 ]
  [-0.6422965  -1.1830204  -0.71071637]]], shape=(2, 4, 3), dtype=float32) 
 tf.Tensor(
[[[5.16625062 8.67236031 3.49752012 9.9600533  4.03615569 8.57707904]
  [2.28123347 0.2695141  9.55248423 6.32091438 9.74293732 3.23273489]
  [7.81335254 2.7062304  5.47168213 2.02222255 9.67112532 1.41002762]]

 [[8.67107021 3.63745347 9.05892649 1.31212917 9.81716484 5.46850381]
  [8.55753798 3.02706301 1.36651951 3.50193782 3.09584558 6.8218033 ]
  [9.47387673 0.62421657 3.58870369 0.15481355 3.26599608 1.2453541 ]]], shape=(2, 3, 6), dtype=float64) 
 tf.Tensor(
[[[6 5 6 9 2 0]
  [9 9 1 2 8 9]
  [7 4 3 4 1 4]]

 [[0 0 9 8 9 7]
  [0 3 5 9 9 9]
  [5 7 4 4 7 2]]], shape=(2, 3, 6), dtype=int32)

创建序列张量

  • tf.range(limit, delta=1)默认创建[0, limit)之间,步长为 delta 的整型序列,不包含 limit 本身,使用与python中range()相仿
import tensorflow as tf
a = tf.range(3,27,1)
b = tf.range(3,27,2)
c = tf.range(27)
print(a)
print(b)
print(c)

out:
tf.Tensor([ 3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26], shape=(24,), dtype=int32)
tf.Tensor([ 3  5  7  9 11 13 15 17 19 21 23 25], shape=(12,), dtype=int32)
tf.Tensor(
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26], shape=(27,), dtype=int32)
posted @ 2020-08-21 15:13  kuanleung  阅读(54)  评论(0)    收藏  举报  来源