TensorFlow学习笔记(一)基本操作

这篇博客介绍了TensorFlow的基本操作,包括element-wise的加减乘除,matrix-wise的矩阵乘法,dim-wise的平均、最大值、最小值和求和运算。此外,还探讨了Tensor的合并与分割,如concat用于拼接,stack用于堆叠以及unstack的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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])<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值