Skip to content

Commit

Permalink
add chapter4
Browse files Browse the repository at this point in the history
  • Loading branch information
lyhue1991 committed Feb 27, 2020
1 parent 0194dfe commit ecd894d
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions 4-1,张量的结构操作.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,26 +464,56 @@ d = tf.where(c<0,tf.fill(c.shape,np.nan),c) #tf.where和np.where作用类似,
d
```

```
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[nan, 1., nan],
[ 2., 2., nan],
[ 3., nan, 3.]], dtype=float32)>
```

```python

```

```python
#如果where只有一个参数,将返回所有满足条件的位置坐标
indices = tf.where(c<0)
indices
```

```
<tf.Tensor: shape=(4, 2), dtype=int64, numpy=
array([[0, 0],
[0, 2],
[1, 2],
[2, 1]])>
```

```python
#将张量的第[0,0]和[2,1]两个位置元素替换为0得到新的张量
d = c - tf.scatter_nd([[0,0],[2,1]],[c[0,0],c[2,1]],c.shape)
d
```

```
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[ 0., 1., -1.],
[ 2., 2., -2.],
[ 3., 0., 3.]], dtype=float32)>
```

```python
#scatter_nd的作用和gather_nd有些相反,可以将某些值插入到一个给定shape的全0的张量的指定位置处。
indices = tf.where(c<0)
tf.scatter_nd(indices,tf.gather_nd(c,indices),c.shape)
```

```python

```
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[-1., 0., -1.],
[ 0., 0., -2.],
[ 0., -3., 0.]], dtype=float32)>
```

```python
Expand Down

0 comments on commit ecd894d

Please sign in to comment.