Reference list 1 2 [1 , 1.2 , 'Hello' , (1 , 2 ), layers] [64 , 32 , 32 , 3 ]
np.array 解决同类型数据运算。
深度计算之前已经设计好的科学计算库,不支持 GPU,不支持自动求导。TensorFlow 解决了这一问题。
tf.Tensor int, float, double, bool, string 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 a = tf.constant(1 )print (a) a = tf.constant(1. )print (a) a = tf.constant(2.2 , dtype=tf.int32)print (a) a = tf.constant(2.2 , dtype=tf.double)print (a) a = tf.constant([True , False ])print (a) a = tf.constant('Hello, world!' )print (a)
scalar vector matrix 1 [[1.1 , 2.2 ], [3.3 , 4.4 ], [5.5 , 6.6 ]]
tensor rank > 2
Tensor Property 切换 Tensor 到 CPU 或 GPU 1 2 3 4 5 6 7 8 9 10 11 with tf.device("cpu" ): a = tf.constant([1 ])with tf.device("gpu" ): b = tf.range (4 )print (a.device) print (b.device) aa = a.gpu() bb = b.cpu()print (aa.device) print (bb.device)
Tensor 转 numpy 1 2 3 4 b = tf.range (4 ) bb = b.numpy()print (b) print (bb)
numpy 转 Tensor 1 2 3 4 5 6 7 a = np.arange(5 ) a1 = a.dtype a2 = tf.convert_to_tensor(a) a3 = tf.convert_to_tensor(a, dtype=tf.int64)print (a1) print (a2) print (a3)
int32 转 float32/double/int64 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 b = tf.range (4 ) b1 = tf.cast(b, dtype=tf.float32) b2 = tf.cast(b, dtype=tf.double) b3 = tf.cast(b, dtype=tf.int64)print (b) print (b1) print (b2) print (b3) a = tf.ones([]) a1 = int (a) a2 = float (a)print (a) print (a1) print (a2)
int32 和 bool 的相互转换 1 2 3 4 5 b = tf.constant([0 , 1 ]) b1 = tf.cast(b, dtype=tf.bool ) b2 = tf.cast(b1, dtype=tf.int32)print (b1) print (b2)
Tensor 转 Variable 1 2 3 4 5 6 7 8 9 10 a = tf.range (5 ) b = tf.Variable(a)print (b.trainable) b1 = isinstance (b, tf.Tensor) b2 = isinstance (b, tf.Variable) b3 = tf.is_tensor(b)print (b1) print (b2) print (b3)
获取 Tensor 的 rank 1 2 3 4 5 6 a = tf.constant(1.1 ) b = tf.constant([1.1 ])print (a.ndim) print (b.ndim) print (tf.rank(a)) print (tf.rank(b))
判断是否为 Tensor 1 2 3 4 5 6 7 8 9 10 a = tf.constant([1. ]) d = np.arange(4 ) a1 = isinstance (a, tf.Tensor) a2 = tf.is_tensor(a) d1 = isinstance (d, tf.Tensor) d2 = tf.is_tensor(d)print (a1) print (a2) print (d1) print (d2)
获取 Tensor 的数据类型 1 2 3 4 a = tf.constant([1. ]) d = np.arange(4 )print (a.dtype) print (d.dtype)
判断 Tensor 是否为指定的数据类型 1 2 3 4 a = tf.constant([1. ]) d = np.arange(4 )print (a.dtype == tf.float32) print (d.dtype == np.int32)
Tensor 初始化 from numpy, list 1 2 3 4 5 6 t1 = tf.convert_to_tensor(np.ones([2 , 3 ])) t2 = tf.convert_to_tensor([2 , 3 ])print (t1) print (t2)
zeros 1 2 3 4 t1 = tf.zeros([2 , 3 ])print (t1)
ones 1 2 3 4 t1 = tf.ones([2 , 3 ])print (t1)
fill 1 2 3 4 t1 = tf.fill([2 , 3 ], 3.14 )print (t1)
random normal, truncated_normal 1 2 3 4 5 6 7 8 9 10 11 12 t1 = tf.random.normal([2 , 3 ], mean=1 , stddev=1 ) t2 = tf.random.normal([2 , 3 ], mean=1 , stddev=1 ) t3 = tf.random.truncated_normal([2 , 3 ], mean=1 , stddev=1 )print (t1) print (t2) print (t3)
1 2 3 4 t1 = tf.random.uniform([2 , 3 ], minval=0 , maxval=100 )print (t1)
shuffle 1 2 3 4 id = tf.range (10 ) id1 = tf.random.shuffle(id )print (id ) print (id1)
constant 1 2 t1 = tf.constant(1 )print (t1)
range 1 2 3 4 5 6 7 8 t1 = tf.range (4 ) t2 = tf.one_hot(t1, 6 )print (t1) print (t2)