# 一小时学会TensorFlow2之基本操作2实例代码

## 索引操作

### 简单索引

```a = tf.reshape(tf.range(12), [2, 2, 3])
print(a)

print(a[0])
print(a[0][0])
```

tf.Tensor(
[[[ 0 1 2]
[ 3 4 5]]

[[ 6 7 8]
[ 9 10 11]]], shape=(2, 2, 3), dtype=int32)
tf.Tensor(
[[0 1 2]
[3 4 5]], shape=(2, 3), dtype=int32)
tf.Tensor([0 1 2], shape=(3,), dtype=int32)

### Numpy 式索引

```a = tf.reshape(tf.range(12), [2, 2, 3])
print(a)

print(a[0])
print(a[0, 0])
```

tf.Tensor(
[[[ 0 1 2]
[ 3 4 5]]

[[ 6 7 8]
[ 9 10 11]]], shape=(2, 2, 3), dtype=int32)
tf.Tensor(
[[0 1 2]
[3 4 5]], shape=(2, 3), dtype=int32)
tf.Tensor([0 1 2], shape=(3,), dtype=int32)

### 使用 : 进行索引

```c = tf.ones([4, 14, 14, 4])
print(c[0, :, :, :].shape)
print(c[0, 1, :, :].shape)
```

(14, 14, 4)
(14, 4)

### tf.gather

```data = tf.zeros([3, 8, 128])

g1 = tf.gather(data, axis=0, indices=[0, 2])
print(g1.shape)

g2 = tf.gather(data, axis=1, indices=[0, 1, 2, 3])
print(g2.shape)
```

(2, 8, 128)
(3, 4, 128)

### tf.gather_nd

```g1 = tf.gather_nd(data, [0])
print(g1.shape)

g2 = tf.gather_nd(data, [0, 1])
print(g2.shape)

g3 = tf.gather_nd(data, [0, 1, 2])
print(g3.shape)
```

(8, 128)
(128,)
()

```tf.boolean_mask(
)
```

```data = tf.zeros([3, 8, 128])

print(b1.shape)

b2 = tf.boolean_mask(data, mask=[True, False, True, False, True, False, True, False], axis=1)
print(b2.shape)
```

(2, 8, 128)
(3, 4, 128)

## 切片操作

### 简单切片

```tensor[start : end]
```

```tf.Tensor([0 1 2], shape=(3,), dtype=int32)
tf.Tensor([9], shape=(1,), dtype=int32)
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)
```

### step 切片

```tensor[start : end: step]
```

```d = tf.range(6)
print(d[::-1])  # 实现倒序
print(d[::2])  # 步长为2
```

tf.Tensor([5 4 3 2 1 0], shape=(6,), dtype=int32)
tf.Tensor([0 2 4], shape=(3,), dtype=int32)

## 维度变换

### tf.reshape

tf.reshape 可以帮助我们进行维度转换.

```tf.reshape(
tensor, shape, name=None
)
```

• tensor: 传入的张量
• shape: 张量的形状
• name: 数据名称

```a = tf.random.normal([3, 8, 128])
print(a.shape)

b = tf.reshape(a, [3, 1024])
print(b.shape)

c = tf.reshape(a, [3, -1])
print(c.shape)
```

(3, 8, 128)
(3, 1024)
(3, 1024)

### tf.transpose

```tf.transpose(
a, perm=None, conjugate=False, name='transpose'
)
```

```a = tf.random.normal([4, 3, 2, 1])
print(a.shape)

b = tf.transpose(a)
print(b.shape)

c = tf.transpose(a, perm=[0, 1, 3, 2])
print(c.shape)
```

(4, 3, 2, 1)
(1, 2, 3, 4)
(4, 3, 1, 2)

### tf.expand_dims

```tf.expand_dims(
input, axis, name=None
)
```

• input: 输入
• axis: 操作的维度
• name: 数据名称

```a = tf.random.normal([4, 3, 2, 1])
print(a.shape)

b = tf.expand_dims(a, axis=0)
print(b.shape)

c = tf.expand_dims(a, axis=1)
print(c.shape)

d = tf.expand_dims(a, axis=-1)
print(d.shape)
```

(4, 3, 2, 1)
(1, 4, 3, 2, 1)
(4, 1, 3, 2, 1)
(4, 3, 2, 1, 1)

### tf.squeeze

tf.squeeze 可以帮助我们删去所有维度为1 的维度.

```tf.squeeze(
input, axis=None, name=None
)
```

• input: 输入
• axis: 操作的维度
• name: 数据名称

```a = tf.zeros([2, 1, 1, 3, 5])

s1 = tf.squeeze(a)
print(s1.shape)

s2 = tf.squeeze(a, axis=1)
print(s2.shape)

s3 = tf.squeeze(a, axis=2)
print(s3.shape)
```

(2, 3, 5)
(2, 1, 3, 5)
(2, 1, 3, 5)

## Boardcasting

### tf.boardcast_to

boardcast_to:

```tf.broadcast_to(
input, shape, name=None
)
```

• input: 输入
• shape: 数据形状
• name: 数据名称

```a = tf.broadcast_to(tf.random.normal([4, 1, 1, 1]), [4, 32, 32, 3])
print(a.shape)

b = tf.broadcast_to(tf.zeros([128, 1, 1, 1]), [128, 32, 32, 3])
print(b.shape)
```

(4, 32, 32, 3)
(128, 32, 32, 3)

### tf.tile

```tf.tile(
input, multiples, name=None
)
```

• input: 输入
• multiples: 同一纬度上复制的次数
• name: 数据名称

```a = tf.zeros([4, 1, 1, 1])
print(a.shape)

b = tf.tile(a, [1, 32, 32, 3])
print(b.shape)
```

(4, 1, 1, 1)
(4, 32, 32, 3)

## 数学运算

### 加减乘除

```# 定义张量
t1 = tf.ones([3, 3])
t2 = tf.fill([3, 3], 3.0)

# 加

# 减
minus = t1 - t2
print(minus)

# 乘
multiply = t1 * t2
print(multiply)

# 除
divide = t1 / t2
print(divide)
```

tf.Tensor(
[[4. 4. 4.]
[4. 4. 4.]
[4. 4. 4.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[-2. -2. -2.]
[-2. -2. -2.]
[-2. -2. -2.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[3. 3. 3.]
[3. 3. 3.]
[3. 3. 3.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[0.33333334 0.33333334 0.33333334]
[0.33333334 0.33333334 0.33333334]
[0.33333334 0.33333334 0.33333334]], shape=(3, 3), dtype=float32)

### log & exp

```# log
a = tf.fill([2], 100.0)
print(a)

b = tf.math.log(a)  # 以e为底
print(b)

# exp
c = tf.ones([2])
print(c)

d = tf.exp(c)
print(d)
```

tf.Tensor([100. 100.], shape=(2,), dtype=float32)
tf.Tensor([4.6051702 4.6051702], shape=(2,), dtype=float32)
tf.Tensor([1. 1.], shape=(2,), dtype=float32)
tf.Tensor([2.7182817 2.7182817], shape=(2,), dtype=float32)

### pow & sqrt

```# 定义张量
a = tf.fill([2], 4.0)
print(a)

# pow
b = tf.pow(a, 2)
print(b)

# sqrt
c = tf.sqrt(a, 2)
print(c)
```

tf.Tensor([4. 4.], shape=(2,), dtype=float32)
tf.Tensor([16. 16.], shape=(2,), dtype=float32)
tf.Tensor([2. 2.], shape=(2,), dtype=float32)

### 矩阵相乘 @

```# 定义张量
a = tf.fill([2, 2], 2)
b = tf.fill([2, 2], 3)

# matmul
c = tf.matmul(a, b)
print(c)

# @
d = a@b
print(d)
```

tf.Tensor(
[[12 12]
[12 12]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[12 12]
[12 12]], shape=(2, 2), dtype=int32)