Tensorflow数据类型
导入包
1 import tensorflow as tf 2 print(tf.__version__)
输出:2.0.0
数值类型
scalar, vector, matrix, tensor, variable
指定执行CPU
1 # 指定执行CPU 2 import os 3 os.environ["CUDA_VISIBLE_DEVICES"]="-1"
scalar
1 print(tf.constant(2)) 2 print(tf.constant(2, dtype=tf.float64)) 3 print(tf.constant(2, dtype=tf.double)) 4 print(tf.constant("Python")) 5 6 out: 7 tf.Tensor(2, shape=(), dtype=int32) 8 tf.Tensor(2.0, shape=(), dtype=float64) 9 tf.Tensor(2.0, shape=(), dtype=float64) 10 tf.Tensor(b'Python', shape=(), dtype=string)
vector
1 print(tf.constant([1,2.0])) 2 print(tf.constant([True, False])) 3 out: 4 tf.Tensor([1. 2.], shape=(2,), dtype=float32) 5 tf.Tensor([ True False], shape=(2,), dtype=bool) 6 7 # 从tf的tensor转换到numpy数据 8 vec_a = tf.constant([1]) 9 vec_a.numpy() 10 out: 11 array([1], dtype=int32)
matrix
1 matrix_b = tf.constant([[1, 2, 3], [2,8,6]]) 2 matrix_b 3 out: 4 <tf.Tensor: id=7, shape=(2, 3), dtype=int32, numpy= 5 array([[1, 2, 3], 6 [2, 8, 6]], dtype=int32)>
tensor: rank>2
1 tensor_c = tf.constant([[[1, 2, 3], [2,8,6]],[[1, 2, 3], [2,8,6]]]) 2 tensor_c 3 out: 4 <tf.Tensor: id=8, shape=(2, 2, 3), dtype=int32, numpy= 5 array([[[1, 2, 3], 6 [2, 8, 6]], 7 8 [[1, 2, 3], 9 [2, 8, 6]]], dtype=int32)>
小结:
1 # 输出数据维度、shape、判断是否为tensor 2 int_a = tf.constant(2, dtype=tf.int64) 3 print(int_a.ndim, int_a.shape, tf.is_tensor(int_a)) 4 out: 5 0 () True 6 7 tensor_c = tf.constant([[[1, 2, 3], [2,8,6]],[[1, 2, 3], [2,8,6]]]) 8 print(tensor_c.ndim, tensor_c.shape, tf.is_tensor(tensor_c)) 9 isinstance(tensor_c, tf.Tensor) 10 out: 11 3 (2, 2, 3) True 12 True 13 14 # 判断数据类型 15 tensor_c.dtype 16 out: 17 tf.int32 18 19 # numpy数据转换为张量数据,注意它的类型 20 import numpy as np 21 a = np.arange(5) 22 print(a.dtype) 23 convert_a = tf.convert_to_tensor(a) 24 print(convert_a.dtype) 25 convert_a 26 out: 27 int64 28 <dtype: 'int64'> 29 <tf.Tensor: id=11, shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4])> 30 31 # 这里在训练时有时会出问题,当我们把numpy数据直接转换过 32 # 来时,dtype可以会出现报错,所以可以在转换是指定数据类型 33 convert_b = tf.convert_to_tensor(a, dtype=tf.int32) 34 convert_b 35 out: 36 <tf.Tensor: id=12, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)> 37 38 # 上面的问题我们也可以使用cast操作,不改变原数据的类型 39 convert_c = tf.cast(convert_a, tf.int32) 40 print(convert_a) 41 print(convert_c) 42 convert_d = tf.cast(tf.constant([0,1]), tf.bool) 43 print(convert_d) 44 out: 45 tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64) 46 tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int32) 47 tf.Tensor([False True], shape=(2,), dtype=bool)
variable
1 a = tf.range(3) 2 b = tf.Variable(a) 3 print(b) 4 print(b.trainable) 5 print(b.name) # 没有指定名字时,变量自动赋一个序号作为名字 6 c = tf.Variable(a, name="stage1") 7 print(c.name) 8 out: 9 <tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([0, 1, 2], dtype=int32)> 10 True 11 Variable:0 12 stage1:0
1 print(isinstance(a, tf.Tensor)) 2 print(isinstance(b, tf.Tensor)) 3 print(tf.is_tensor(b)) # 注意这里没有区分张量和变量,变量也看做一个张量;推荐使用这个函数。 4 out: 5 True 6 False 7 True
1 b.numpy() 2 out: array([0, 1, 2], dtype=int32)
清澈的爱,只为中国