Tensorflow数据的基本操作

发布时间 2023-07-29 18:53:18作者: NAVYSUMMER
# tensorflow里引入一个新的数据类型-张量(tensor),与numpy的ndarray类似,是一个多维数组。和numpy的区别在于:numpy的ndarray只支持CPU计算,而张量支持GPU,可以通过GPU加速,提高速度,同时张量还支持自动微分计算,更适合深度学习
## 创建数字序列的张量
import tensorflow as tf
x = tf.range(10)
x
<tf.Tensor: shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)>
# 查看张量形状
x.shape
TensorShape([10])
# 修改形状
x1 = tf.reshape(x,(2,5))
x1
<tf.Tensor: shape=(2, 5), dtype=int32, numpy=
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]], dtype=int32)>
# 全1的张量
tf.ones((3,3))
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>
# 全0的张量
tf.zeros((3,3))
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]], dtype=float32)>
# 均值为0,方差为1的正态分布张量
tf.random.normal((3,4),mean=0,stddev=1)
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[-0.78743833,  0.16588742,  0.05436951, -0.5504127 ],
       [ 0.6029112 ,  1.1076114 ,  0.0166753 , -1.5274208 ],
       [ 1.5660976 ,  0.7945377 , -1.6707777 , -0.29749963]],
      dtype=float32)>
# 创建常量,不可修改的张量
const1 = tf.constant(1)
const2 = tf.constant([1,2])
const3 = tf.constant([[1,2],[3,4]])
print(const1)
print(const2)
print(const3)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor([1 2], shape=(2,), dtype=int32)
tf.Tensor(
[[1 2]
 [3 4]], shape=(2, 2), dtype=int32)
# 张量的基础运算
x = tf.constant([1.0, 2, 4, 8])
y = tf.constant([2.0, 2, 2, 2])
x + y, x - y, x * y, x / y, x ** y, tf.exp(x)  
(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 3.,  4.,  6., 10.], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=array([-1.,  0.,  2.,  6.], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 2.,  4.,  8., 16.], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=array([0.5, 1. , 2. , 4. ], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 1.,  4., 16., 64.], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=
 array([2.7182817e+00, 7.3890562e+00, 5.4598148e+01, 2.9809580e+03],
       dtype=float32)>)
# 张量的拼接
X = tf.reshape(tf.range(12, dtype=tf.float32), (3, 4))
Y = tf.constant([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
tf.concat([X, Y], axis=0), tf.concat([X, Y], axis=1)
(<tf.Tensor: shape=(6, 4), dtype=float32, numpy=
 array([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.],
        [ 2.,  1.,  4.,  3.],
        [ 1.,  2.,  3.,  4.],
        [ 4.,  3.,  2.,  1.]], dtype=float32)>,
 <tf.Tensor: shape=(3, 8), dtype=float32, numpy=
 array([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],
        [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],
        [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]], dtype=float32)>)
# 张量的比较
X == Y
<tf.Tensor: shape=(3, 4), dtype=bool, numpy=
array([[False,  True, False,  True],
       [False, False, False, False],
       [False, False, False, False]])>
# 张量元素求和
## 所有元素求和
a = tf.reduce_sum(X)
## 每列求和
r = tf.reduce_sum(X,axis=0)
## 每行求和
c = tf.reduce_sum(X,axis=1)
print(X)
print(a)
print(r)
print(c)
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(3, 4), dtype=float32)
tf.Tensor(66.0, shape=(), dtype=float32)
tf.Tensor([12. 15. 18. 21.], shape=(4,), dtype=float32)
tf.Tensor([ 6. 22. 38.], shape=(3,), dtype=float32)
# 广播
a = tf.reshape(tf.range(3), (3, 1))
b = tf.reshape(tf.range(2), (1, 2))
print(a)
print(b)
A = tf.broadcast_to(a,(3,2))   # 广播
B = tf.broadcast_to(b,(3,2))   # 广播
print(a + b)  # 自动广播成相同的形状
print(A + B)  # 和上面的a+b结果相同
tf.Tensor(
[[0]
 [1]
 [2]], shape=(3, 1), dtype=int32)
tf.Tensor([[0 1]], shape=(1, 2), dtype=int32)
tf.Tensor(
[[0 1]
 [1 2]
 [2 3]], shape=(3, 2), dtype=int32)
tf.Tensor(
[[0 1]
 [1 2]
 [2 3]], shape=(3, 2), dtype=int32)
# 索引和切片
print(X)
print(X[-1], X[1:3])
print(X[:,1:2])
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(3, 4), dtype=float32)
tf.Tensor([ 8.  9. 10. 11.], shape=(4,), dtype=float32) tf.Tensor(
[[ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(2, 4), dtype=float32)
tf.Tensor(
[[1.]
 [5.]
 [9.]], shape=(3, 1), dtype=float32)
# 创建变量,可以修改
X_var = tf.Variable(X)
X_var[1, 2].assign(9)
print(X)
print(X_var)
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(3, 4), dtype=float32)
<tf.Variable 'Variable:0' shape=(3, 4) dtype=float32, numpy=
array([[ 0.,  1.,  2.,  3.],
       [ 4.,  5.,  9.,  7.],
       [ 8.,  9., 10., 11.]], dtype=float32)>
# Tensor和numpy互转
A = X.numpy()
B = tf.constant(A)
print(A)
print(B)
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]]
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(3, 4), dtype=float32)