文章目录
- 数据类型转换
- 函数使用
- tf.size()
- tf.shape(input, name=None)
- tf.rank(input, name=None)
- tf.reshape(tensor, shape, name=None)
- tf.expand_dims(input, dim, name=None)
- tf.slice(input_, begin, size, name=None)
- tf.split()
- tf.concat(values,axis,name='concat')
- tf.pack(values, axis=0, name=’pack’)
- tf.reverse(tensor, dims, name=None)
- tf.transpose(a, perm=None, name=’transpose’)
- tf.gather(params, indices, validate_indices=None, name=None)
- tf.one_hot()
- tf.unique(x, out_idx=tf.dtypes.int32,name=None)
- 矩阵操作
- 生成随机张量
TensorFlow2.0官方文档
数据类型转换
import tensorflow as tf
import tensorflow.compat.v1 as tf1
tf1.disable_v2_behavior
import numpy as np
string = '12.3'
n1 = tf1.string_to_number(string, out_type=None, name=None)
print(n1)
x = 12.3
d1 = tf1.to_double(x, name='ToDouble')
print(d1)
f1 = tf1.to_float(x, name='ToFloat')
print(f1)
i1 = tf1.to_int32(x, name='ToInt32')
print(i1)
i2 = tf1.to_int64(x, name='ToInt64')
print(i2)
a = [1.8, 2.2]
i3 = tf.cast(a, tf.int32)
print(i3)
tf.Tensor(12.3, shape=(), dtype=float32)
tf.Tensor(12.300000190734863, shape=(), dtype=float64)
tf.Tensor(12.3, shape=(), dtype=float32)
tf.Tensor(12, shape=(), dtype=int32)
tf.Tensor(12, shape=(), dtype=int64)
tf.Tensor([1 2], shape=(2,), dtype=int32)
函数使用
tf.size()
返回数据元素个数
a=[[1,2],[3,4]]
tf.size(a)
<tf.Tensor: id=12, shape=(), dtype=int32, numpy=4>
tf.shape(input, name=None)
返回数据的shape
t=[
[[1,1,1],[2,2,2]],
[[3,3,3],[4,4,4]]
]
print(tf.shape(t))
tf.shape(t)
tf.Tensor([2 2 3], shape=(3,), dtype=int32)
<tf.Tensor: id=39, shape=(3,), dtype=int32, numpy=array([2, 2, 3], dtype=int32)>
tt=tf.Variable(t,name='tt')
print(tf.shape(tt))
tf.shape(tt)
tf.Tensor([2 2 3], shape=(3,), dtype=int32)
<tf.Tensor: id=33, shape=(3,), dtype=int32, numpy=array([2, 2, 3], dtype=int32)>
tf.rank(input, name=None)
返回tensor的rank 注意:tensor的rank表示一个tensor需要的索引数目来唯一表示,任何一个元素 也就是通常所说的 “order”, “degree”或
tf.rank(t)
<tf.Tensor: id=35, shape=(), dtype=int32, numpy=3>
tf.reshape(tensor, shape, name=None)
改变tensor的形状
t=[1,2,3,4,5,6,7,8,9]
print(tf.shape(t))
t2=tf.reshape(t,[3,3])
print(tf.shape(t2))
t3=tf.reshape(t,[3,-1])
print(tf.shape(t3))
tf.Tensor([9], shape=(1,), dtype=int32)
tf.Tensor([3 3], shape=(2,), dtype=int32)
tf.Tensor([3 3], shape=(2,), dtype=int32)
tf.expand_dims(input, dim, name=None)
插入维度1进入一个tensor中
t=[2,3]
t_123=[print('\n',tf.shape(tf.expand_dims(t,i))) for i in [0, 1, -1]]
tf.Tensor([1 2], shape=(2,), dtype=int32)
tf.Tensor([2 1], shape=(2,), dtype=int32)
tf.Tensor([2 1], shape=(2,), dtype=int32)
t4=tf.ones([2,3,5])
print(t4)
t_567=[print('\n',tf.shape(tf.expand_dims(t4,i))) for i in [0, 2, 3]]
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.]]], shape=(2, 3, 5), dtype=float32)
tf.Tensor([1 2 3 5], shape=(4,), dtype=int32)
tf.Tensor([2 3 1 5], shape=(4,), dtype=int32)
tf.Tensor([2 3 5 1], shape=(4,), dtype=int32)
tf.slice(input_, begin, size, name=None)
对tensor进行切片操作,其中size[i] = input.dim_size(i) - begin[i]该操作要求 0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n]
t = [
[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]],
[[5, 5, 5], [6, 6, 6]]
]
t1 = tf.slice(t, [1, 0, 0], [1, 1, 3])
t2 = tf.slice(t, [1, 0, 0], [1, 2, 3])
t3 = tf.slice(t, [1, 0, 0], [2, 1, 3])
print(t1 , t2, t3,sep='\n===========\n')
tf.Tensor([[[3 3 3]]], shape=(1, 1, 3), dtype=int32)
===========
tf.Tensor(
[[[3 3 3]
[4 4 4]]], shape=(1, 2, 3), dtype=int32)
===========
tf.Tensor(
[[[3 3 3]]
[[5 5 5]]], shape=(2, 1, 3), dtype=int32)
tf.split()
tf.split(
value,
num_or_size_splits,
axis=0,
num=None,
name=‘split’
)
t=tf.ones([5,30])
t_123=tf.split(t,num_or_size_splits=3,axis=1)
t_123_shape=[print(tf.shape(tt)) for tt in t_123]
tf.Tensor([ 5 10], shape=(2,), dtype=int32)
tf.Tensor([ 5 10], shape=(2,), dtype=int32)
tf.Tensor([ 5 10], shape=(2,), dtype=int32)
tf.concat(values,axis,name=‘concat’)
沿着某一维度连结tens
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
t3 = tf.concat([t1, t2], 0)
t4 = tf.concat([t1, t2], 1)
print(t3,t4,sep='\n===\n')
tf.Tensor(
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]], shape=(4, 3), dtype=int32)
===
tf.Tensor(
[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]], shape=(2, 6), dtype=int32)
t1 = [
[[1, 2], [2, 3]],
[[4, 4], [5, 3]]
]
t2 = [
[[7, 4], [8, 4]],
[[2, 10], [15, 11]]
]
tf.concat([t1, t2], -1)
<tf.Tensor: id=407, shape=(2, 2, 4), dtype=int32, numpy=
array([[[ 1, 2, 7, 4],
[ 2, 3, 8, 4]],
[[ 4, 4, 2, 10],
[ 5, 3, 15, 11]]], dtype=int32)>
tf.pack(values, axis=0, name=’pack’)
将一系列rank-R的tensor打包为一个rank-(R+1)的tensor
x=[1,4]; y=[2,5]; z=[3,6]
# print(tf.pack([x,y,z],axis=0))
# print(tf.pack([x,y,z],axis=1))
# AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'pack'
np.asarray([x, y, z])
array([[1, 4],
[2, 5],
[3, 6]])
tf.reverse(tensor, dims, name=None)
沿着某维度进行序列反转其中dim为列表,元素为bool型,size等于rank(tensor)
t = [
[ [[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]],
[[12, 13, 14, 15], [16, 17, 18, 19],[20, 21, 22, 23]]
]
]
# tensor 't' shape is [1, 2, 3, 4]
dims=[
[[3],[-1]],
[[1],[-3]],
[[2],[-2]]
]
t_rev=[print(tf.reverse(t,dim[0]),end='\n=======\n') for dim in dims]
tf.Tensor(
[[[[ 3 2 1 0]
[ 7 6 5 4]
[11 10 9 8]]
[[15 14 13 12]
[19 18 17 16]
[23 22 21 20]]]], shape=(1, 2, 3, 4), dtype=int32)
=======
tf.Tensor(
[[[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]]], shape=(1, 2, 3, 4), dtype=int32)
=======
tf.Tensor(
[[[[ 8 9 10 11]
[ 4 5 6 7]
[ 0 1 2 3]]
[[20 21 22 23]
[16 17 18 19]
[12 13 14 15]]]], shape=(1, 2, 3, 4), dtype=int32)
=======
tf.transpose(a, perm=None, name=’transpose’)
调换tensor的维度顺序 按照列表perm的维度排列调换tensor顺序,如未定义,则perm为(n-1…0)
t = [[1, 2, 3],[4, 5, 6]]
t1 = tf.transpose(t)
t2 = tf.transpose(t, perm=[1, 0])
print(t1)
print(t2)
tf.Tensor(
[[1 4]
[2 5]
[3 6]], shape=(3, 2), dtype=int32)
tf.Tensor(
[[1 4]
[2 5]
[3 6]], shape=(3, 2), dtype=int32)
tf.gather(params, indices, validate_indices=None, name=None)
合并索引indices所指示params中的切片
t = [[1, 2,],[3,4],[ 5, 6]]
t_gather=tf.gather(t,[0,2])
print(t_gather)
tf.Tensor(
[[1 2]
[5 6]], shape=(2, 2), dtype=int32)
tf.one_hot()
tf.one_hot(indices,depth,on_value=None,off_value=None,axis=None,dtype=None,name=None)
indices = [0, 1, 2]
depth = 3
t1 = tf.one_hot(indices, depth)
indices = [0, 2, -1, 1]
depth = 3
t2 = tf.one_hot(indices, depth,on_value=5.0, off_value=0.0,axis=-1)
indices = [[0, 2], [1, -1]]
depth = 3
t3 = tf.one_hot(indices, depth,on_value=1.0, off_value=0.0,axis=-1)
print(t1,t2,t3,sep='\n\n')
tf.Tensor(
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[5. 0. 0.]
[0. 0. 5.]
[0. 0. 0.]
[0. 5. 0.]], shape=(4, 3), dtype=float32)
tf.Tensor(
[[[1. 0. 0.]
[0. 0. 1.]]
[[0. 1. 0.]
[0. 0. 0.]]], shape=(2, 2, 3), dtype=float32)
tf.unique(x, out_idx=tf.dtypes.int32,name=None)
t = [1, 1, 2, 4, 4, 4, 7, 8, 8]
z= tf.unique(t)
y, idx = tf.unique(t)
print(z)
print(y)
print(idx)
Unique(y=<tf.Tensor: id=478, shape=(5,), dtype=int32, numpy=array([1, 2, 4, 7, 8], dtype=int32)>, idx=<tf.Tensor: id=479, shape=(9,), dtype=int32, numpy=array([0, 0, 1, 2, 2, 2, 3, 4, 4], dtype=int32)>)
tf.Tensor([1 2 4 7 8], shape=(5,), dtype=int32)
tf.Tensor([0 0 1 2 2 2 3 4 4], shape=(9,), dtype=int32)
- tf.zeros(shape,dtype=tf.dtypes.float32,name=None)
- tf.diag(diagonal, name=None)tf.trace(x, name=None)
- tf.matrix_determinant(input, name=None)
- tf.matrix_inverse(input, adjoint=None, name=None)
- tf.transpose(a, perm=None, name=‘transpose’)
zeros=tf.zeros([2,3])
zeros
<tf.Tensor: id=62, shape=(2, 3), dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>
aa=tf.Variable([[1.,2.],[3.,4.]])
diag=tf1.diag(aa) #将矩阵中的元素转换为新矩阵对角线上的元素
md=tf1.matrix_determinant(aa) # 矩阵行列式的值
trace=tf1.trace(aa) #对角线上元素之和
mi=tf1.matrix_inverse(aa) #矩阵求逆
print(diag,md,trace,mi,sep='\n===========\n')
tf.Tensor(
[[[[1. 0.]
[0. 0.]]
[[0. 2.]
[0. 0.]]]
[[[0. 0.]
[3. 0.]]
[[0. 0.]
[0. 4.]]]], shape=(2, 2, 2, 2), dtype=float32)
===========
tf.Tensor(-2.0, shape=(), dtype=float32)
===========
tf.Tensor(5.0, shape=(), dtype=float32)
===========
tf.Tensor(
[[-2.0000002 1.0000001 ]
[ 1.5000001 -0.50000006]], shape=(2, 2), dtype=float32)
t = [[1, 2, 3],[4, 5, 6]]
t1 = tf.transpose(t)
t2 = tf.transpose(t, perm=[1, 0])
print(t1,'\n\n')
print(t2)
tf.Tensor(
[[1 4]
[2 5]
[3 6]], shape=(3, 2), dtype=int32)
tf.Tensor(
[[1 4]
[2 5]
[3 6]], shape=(3, 2), dtype=int32)
矩阵操作
- tf.matmul(a,b,transpose_a=False,transpose_b=False,a_is_sparse=False,b_is_sparse=False,name=None)
矩阵相乘 - tf.complex(real, imag, name=None) 将两实数转换为复数形式
- tf.complex_abs(x, name=None) 计算复数的绝对值,即长度。
- tf.conj(input, name=None) 计算共轭复数
- tf.imag(input, name=None)
- tf.real(input, name=None)
- tf.fft(input, name=None)
- tf.eye(num_rows,num_columns=None,batch_shape=None,dtype=tf.dtypes.float32,name=None)
- tf.fill(dims,value,name=None)
- tf.ones(shape,dtype=tf.dtypes.float32,name=None)
a= [
[1,2],
[3,4]
]
b=[
[5,6],
[7,8]
]
ab_matmul=tf.matmul(a,b)
ab_matmul
<tf.Tensor: id=140, shape=(2, 2), dtype=int32, numpy=
array([[19, 22],
[43, 50]], dtype=int32)>
#复数操作
a=1.; b=2.
ab_cp=tf.complex(a,b)
ab_cp_abs=tf.abs(ab_cp)
ab_cp_conj=tf.math.conj(ab_cp)
ab_cp_imag=tf.math.imag(ab_cp)
ab_cp_real=tf.math.real(ab_cp)
print(ab_cp,ab_cp_abs,ab_cp_conj,ab_cp_imag,ab_cp_real,sep='\n========\n')
tf.Tensor((1+2j), shape=(), dtype=complex64)
========
tf.Tensor(2.236068, shape=(), dtype=float32)
========
tf.Tensor((1-2j), shape=(), dtype=complex64)
========
tf.Tensor(2.0, shape=(), dtype=float32)
========
tf.Tensor(1.0, shape=(), dtype=float32)
a= [
[1,2],
[3,4]
]
a_fft=tf1.fft(a) #快速傅里叶变换
a_fft
<tf.Tensor: id=207, shape=(2, 2), dtype=complex64, numpy=
array([[ 3.+0.j, -1.+0.j],
[ 7.+0.j, -1.+0.j]], dtype=complex64)>
eye_d=tf.eye(3,2,dtype=tf.dtypes.float32) #单位矩阵
fill_d=tf.fill(dims=[2,1],value=21,name=None)
ones_d=tf.ones(shape=[2,2],dtype=tf.dtypes.float32,name=None) #全一矩阵
print(eye_d,fill_d,ones_d,sep='\n====\n')
tf.Tensor(
[[1. 0.]
[0. 1.]
[0. 0.]], shape=(3, 2), dtype=float32)
====
tf.Tensor(
[[21]
[21]], shape=(2, 1), dtype=int32)
====
tf.Tensor(
[[1. 1.]
[1. 1.]], shape=(2, 2), dtype=float32)
生成随机张量
tf.random_normal()
tf.truncated_normal()
产生截断正态分布随机数,取值范围为 [ mean - 2 * stddev, mean + 2 * stddev ]
tf.random_uniform()
sp=[2,2]
a=tf1.random_normal(sp) #正态分布随机数
b=tf1.truncated_normal(sp) #产生截断正态分布随机数,取值范围为 [ mean - 2 * stddev, mean + 2 * stddev ]
c=tf1.random_uniform(sp) #均匀分布随机数
d=tf1.random_shuffle([0,1,2]) #随机地将张量沿其第一维度打乱
print(a,b,c,d,sep='\n'+'*'*119+'\n')
tf.Tensor(
[[ 0.15850271 -0.00646496]
[-0.54101175 -0.36107492]], shape=(2, 2), dtype=float32)
***********************************************************************************************************************
tf.Tensor(
[[-0.51454467 -0.3176144 ]
[ 1.3686976 -1.2122307 ]], shape=(2, 2), dtype=float32)
***********************************************************************************************************************
tf.Tensor(
[[0.951952 0.9437604 ]
[0.30550683 0.91686046]], shape=(2, 2), dtype=float32)
***********************************************************************************************************************
tf.Tensor([1 0 2], shape=(3,), dtype=int32)