如果你没安装TensorFlow2-GPU版本可以参考我的博客
链接: 手把手配置Win10-TensorFlow2-GPU版本&Anaconda&CUDA10.0&Pycharm.
定义Tensor变量
import tensorflow as tf
import numpy as np
// 第一种
eg1 = tf.convert_to_tensor(np.ones([2, 3]))
eg2 = tf.convert_to_tensor(np.zeros([2, 3]))
eg3 = tf.convert_to_tensor([1, 2])
// 第二种
eg4 = tf.ones_like(eg1)
eg5 = tf.zeros_like(eg1)
eg5_2 = tf.zeros(eg1.shape)
// 第三种
eg6 = tf.fill([2, 2], 0.)
// 随机初始化
eg7 = tf.random.normal([2, 2], mean=1, stddev=1)
// 不使用参数使用标准正态分布 均值0,方差
eg8 = tf.random.truncated_normal([2, 2], mean=0, stddev=1)
// 使用裁剪过的正态分布
eg9 = tf.random.uniform([2, 2], minval=0, maxval=1)
// 使用均匀分布
// constant
eg10 = tf.constant([1, 2])
// 随机打散
idx = tf.range(10)
idx = tf.random.shuffle(idx)
a = tf.random.normal([10, 784])
a = tf.gather(a, idx)
Tensor的维度变换
import tensorflow as tf
a = tf.random.normal([4, 35, 8])
b = tf.expand_dims(a, axis=0)
print(b.shape)//(1, 4, 35, 8)
c = tf.random.normal([1, 4, 35, 8])
d = tf.squeeze(a)
print(d.shape)//(4, 35, 8)
Tensor的合并与切片
import tensorflow as tf
a = tf.ones([4, 35, 8])
b = tf.ones([2, 35, 8])
e = tf.ones([4, 35, 8])
c = tf.concat([a, b], axis=0)
d = tf.stack([a, e], axis=0)
print(c.shape, d.shape)//(6, 35, 8) (2, 4, 35, 8)
f, g = tf.unstack(d, axis=0)
print(f.shape)//(4, 35, 8)
res = tf.split(d, axis=3, num_or_size_splits=[2, 2, 4])
print(res[0].shape, res[2].shape)//(2, 4, 35, 2) (2, 4, 35, 4)
Tensor的排序
import tensorflow as tf
a = tf.random.shuffle(tf.range(5))
print(a)//tf.Tensor([3 0 2 4 1], shape=(5,), dtype=int32)
print(tf.sort(a, direction='DESCENDING'))//tf.Tensor([4 3 2 1 0], shape=(5,), dtype=int32)
a = ([[4, 6, 8], [9, 4, 7], [4, 5, 1]])
res = tf.math.top_k(a, 2)
print(res.indices, res.values)
// tf.Tensor(
[[2 1]
[0 2]
[1 0]], shape=(3, 2), dtype=int32)
// tf.Tensor(
[[8 6]
[9 7]
[5 4]], shape=(3, 2), dtype=int32)
// 这里解释一下top_k
// res.indices为矩阵a中每行元素中前K个(此处k=2)最大的值的下标
// res.values 为矩阵a中每行元素中前K个(此处k=2)最大的值
Tensor的复制和填充
import tensorflow as tf
// 填充
a = tf.reshape(tf.range(9), [3, 3])
b = tf.pad(a, [[1, 1], [0, 1]])
print(b)
//tf.Tensor(
[[0 0 0 0]
[0 1 2 0]
[3 4 5 0]
[6 7 8 0]
[0 0 0 0]], shape=(5, 4), dtype=int32)
// 复制
a = tf.reshape(tf.range(9), [3, 3])
c = tf.tile(a, [1, 2])
//行为原来的一倍即不复制
//列为原来的两倍即复制一遍
print(c)
//tf.Tensor(
[[0 1 2 0 1 2]
[3 4 5 3 4 5]
[6 7 8 6 7 8]], shape=(3, 6), dtype=int32)
Tensor的限幅
import tensorflow as tf
a = tf.range(10)
print(a)
//tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,),dtype=int32)
a_maximum = tf.maximum(a, 2)
print(a_maximum)
//tf.Tensor([2 2 2 3 4 5 6 7 8 9], shape=(10,),dtype=int32)
a_minimum = tf.minimum(a, 8)
print(a_minimum)
//tf.Tensor([0 1 2 3 4 5 6 7 8 8], shape=(10,),dtype=int32)
a_clip_by_value = tf.clip_by_value(a, 2, 8)
print(a_clip_by_value)
//tf.Tensor([2 2 2 3 4 5 6 7 8 8], shape=(10,),dtype=int32)
a = tf.random.normal([3, 3])
mask = a > 0
a_mask = tf.boolean_mask(a, mask)
indices = tf.where(mask)
a_after_mask = tf.gather_nd(a, indices)
print(a)
// tf.Tensor(
[[ 0.38650084 1.620614 -1.0425532 ]
[-0.24474359 0.56643033 0.73257655]
[ 0.25838175 -0.9129598 -1.2527405 ]]
, shape=(3,3),dtype=float32)
print(a_mask)
print(a_after_mask)
// tf.Tensor([0.38650084 1.620614 0.56643033 0.73257655 0.25838175], shape=(5,), dtype=float32)
// tf.Tensor([0.38650084 1.620614 0.56643033 0.73257655 0.25838175], shape=(5,), dtype=float32)
// 进阶操作
mask = tf.convert_to_tensor(np.array([[True, True, False],
[True, False, False],
[True, True, False]]))
a = tf.ones([3, 3])
b = tf.zeros([3, 3])
c = tf.where(mask, a, b)
print(c)
// tf.Tensor(
[[1. 1. 0.]
[1. 0. 0.]
[1. 1. 0.]], shape=(3, 3), dtype=float32)
Tensor的Meshgrid
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
y = tf.linspace(-2., 2, 5)// (5, 5) 在(-2,2)上均匀采集5个点
x = tf.linspace(-2., 2, 5)// (5, 5)
point_x, point_y = tf.meshgrid(x, y)// 组合坐标 形成25个点
point = tf.stack([point_x, point_y], axis=2)//(5, 5, 2)
//tf.Tensor(
[[[-2. -2.]
[-1. -2.]
[ 0. -2.]
[ 1. -2.]
[ 2. -2.]]
[[-2. -1.]
[-1. -1.]
[ 0. -1.]
[ 1. -1.]
[ 2. -1.]]
[[-2. 0.]
[-1. 0.]
[ 0. 0.]
[ 1. 0.]
[ 2. 0.]]
[[-2. 1.]
[-1. 1.]
[ 0. 1.]
[ 1. 1.]
[ 2. 1.]]
[[-2. 2.]
[-1. 2.]
[ 0. 2.]
[ 1. 2.]
[ 2. 2.]]], shape=(5, 5, 2), dtype=float32)
一个有趣的例子
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def func(x):
z = tf.math.sin(x[..., 0]) + tf.math.sin(x[..., 1])
return z
x = tf.linspace(0., 2*3.14, 500)
y = tf.linspace(0., 2*3.14, 500)
point_x, point_y = tf.meshgrid(x, y)
points = tf.stack([point_x, point_y], axis=2)
print('point:', points.shape)
z = func(points)
print(z.shape)
print('z:', z.shape)
plt.figure('plot 2d func value')
plt.imshow(z, origin='lower', interpolation='none')
plt.colorbar()
plt.show()