TensorFlow学习笔记(一)基本操作
Opation type
(1)element-wise ±*/
b=tf.fill([2,2],2.)
a=tf.ones([2,2])
a+b,a-b,a*b,a/b,b//a,b%a
(2)matrix-wise @,matmul
a@b
tf.matmul(a, b)
(3)dim-wise reduce_mean/max/min/sum
x=tf.ones([4,2])
W=tf.ones([2,1])
b=tf.constant(0.1)
x@W+b
#<tf.Tensor: id=27, shape=(4, 1), dtype=float32, numpy=
#array([[2.1],
# [2.1],
# [2.1],
# [2.1]], dtype=float32)>
合并与分割
(1)concat(拼接)
a = tf.ones([4, 35, 8])
b = tf.ones([2, 35, 8])
c=tf.concat([a, b], axis=0)
c.shape
#TensorShape([6, 35, 8])
注:concat必须保证除了要合并的维度外其他维度相同
(2)stack(堆叠)与unstack
a = tf.ones([4, 35, 8])
b = tf.ones([4, 35, 8])
tf.concat([a, b], axis=-1).shape
tf.stack([a, b], axis=0).shape
tf.stack([a, b], axis=3).shape
#(TensorShape([4, 35, 16]),
#TensorShape([2, 4, 35, 8]),
#TensorShape([4, 35, 8, 2]))
```
```python
c = tf.ones([2,4,35,8])
aa, bb = tf.unstack(c, axis=0)
```
注:stack必须保证所有维度都相同
### (3)split(分割)
```python
c = tf.ones([2,4,35,8])
res = tf.unstack(c, axis=3)
len(res) # 8
res = tf.split(c, axis=3, num_or_size_splits=2)
len(res) # 2
res[0].shape #TensorShape([2, 4, 35, 4])<