tensor数据类型基本运算(一)
import tensorflow as tf tf_arr = tf.constant([[1, 2, 3],[4, 5, 6], [7, 8, 9]]) print(tf_arr) print("张量的内容:", tf_arr.numpy) print("张量的形状:", tf_arr.shape) print("张量的存储位置:", tf_arr.device) print("张量的维度:", tf_arr.ndim) print("张量的秩:", tf.rank(tf_arr)) print("判断是否为张量:", tf.is_tensor(tf_arr)) 123456789
输出为:
tf.Tensor( [[1 2 3] [4 5 6] [7 8 9]], shape=(3, 3), dtype=int32) 张量的内容: <bound method _EagerTensorBase.numpy of <tf.Tensor: shape=(3, 3), dtype=int32, numpy= array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])>> 张量的形状: (3, 3) 张量的存储位置: /job:localhost/replica:0/task:0/device:CPU:0 张量的维度: 2 张量的秩: tf.Tensor(2, shape=(), dtype=int32) 判断是否为张量: True 12345678910111213 2. tensor的创建
import tensorflow as tf import numpy as np import pandas as pd # 1. 其他数据类型转换为tensor a = tf.convert_to_tensor(np.ones([1, 3])) # 通过numpy ndaray类创建 b = tf.convert_to_tensor([1, 2, 3]) # 通过list类创建 c = tf.convert_to_tensor(pd.Series([1, 2, 3])) # 通过pandas 各种类创建 # 2. 通过zeros创建 d = tf.zeros([1, 3]) # 3. 通过ones创建 e = tf.ones([1, 3]) # 4. 通过fill创建 f = tf.fill([1, 3], 0) # 5. 统计方法创建 g = tf.random.normal([1, 3], mean=1, stddev=1) #使用正态分布创建 h = tf.random.truncated_normal([1, 3], mean=1, stddev=1) # 截取正太分布 i = tf.random.uniform([1, 3], minval=0, maxval=1) # 均匀分布采样 # 6. range创建 j = tf.range(3) # range创建 print(a) print(b) print(c) print(d) print(e) print(f) print(g) print(h) print(i) print(j)
12345678910111213141516171819202122232425262728293031输出为:
tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float64) tf.Tensor([1 2 3], shape=(3,), dtype=int32) tf.Tensor([1 2 3], shape=(3,), dtype=int64) tf.Tensor([[0. 0. 0.]], shape=(1, 3), dtype=float32) tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float32) tf.Tensor([[0 0 0]], shape=(1, 3), dtype=int32) tf.Tensor([[1.0774127 2.1872802 2.6653028]], shape=(1, 3), dtype=float32) tf.Tensor([[1.305073 1.3633053 0.40162748]], shape=(1, 3), dtype=float32) tf.Tensor([[0.6752325 0.9771416 0.9276693]], shape=(1, 3), dtype=float32) tf.Tensor([0 1 2], shape=(3,), dtype=int32) 12345678910 3. tensor索引与切片
import tensorflow as tf a = tf.range(9) a = tf.reshape(a, [3, 3]) # 1. 通过[idx][idx]索引 print("通过[idx][idx]索引:", a[1][1]) # 2. 通过[idx, idx]索引 print("通过[idx, idx]索引:", a[1, 1]) # 3. 通过start:end切片索引 print("通过start:end切片索引(1):", a[1:2, 1:2]) print("通过start:end切片索引(2):", a[-2:-1, -2:-1]) # 4. 通过start:end:step切片索引 print("通过start:end:step切片索引(1):", a[1:2:1, 1:2:1]) print("通过start:end:step切片索引(2):", a[-2:-1:1, -2:-1:1]) ''' 注:使用切片方法时,要学会使用 1. 省略号...:代表选取剩余维度下的内容; 2. -1:已知所有数据,某些维度的数量,-1为自动计算最后一个维度,另外-1也代表最后一个数 ''' # 5. 通过tf.gather_nd(params, indices, name=None) : 通过索引来提取 print("通过tf.gather_nd(params, indices, name=None) :", tf.gather_nd(a, [[0], [1]])) # 提取第0行和第1行 # 6. 通过tf.boolean_mask(params, axis= , mask=[]) print("通过tf.boolean_mask(params, axis= , mask=[]):", tf.boolean_mask(a, axis=0, mask=[True, True, False]))
123456789101112131415161718192021222324252627282930输出为:
通过[idx][idx]索引: tf.Tensor(4, shape=(), dtype=int32) 通过[idx, idx]索引: tf.Tensor(4, shape=(), dtype=int32) 通过start:end切片索引(1): tf.Tensor([[4]], shape=(1, 1), dtype=int32) 通过start:end切片索引(2): tf.Tensor([[4]], shape=(1, 1), dtype=int32) 通过start:end:step切片索引(1): tf.Tensor([[4]], shape=(1, 1), dtype=int32) 通过start:end:step切片索引(2): tf.Tensor([[4]], shape=(1, 1), dtype=int32) 通过tf.gather_nd(params, indices, name=None) : tf.Tensor( [[0 1 2] [3 4 5]], shape=(2, 3), dtype=int32) 通过tf.boolean_mask(params, axis= , mask=[]): tf.Tensor( [[0 1 2] [3 4 5]], shape=(2, 3), dtype=int32) 123456789101112 4. 维度变换
import tensorflow as tf a = tf.range(12) a = tf.reshape(a, [3, 4]) # 1. 通过reshape实现 print("通过reshape实现:", tf.reshape(a, [4, 3])) # 2. 通过转置tf.transpose print("通过转置tf.transpose:", tf.transpose(a, perm=[1, 0])) # 3. 通过增加维度 b = tf.expand_dims(a, axis=0) print("通过增加维度:", b) # 4. 通过减少维度b print("通过减少维度:", tf.squeeze(b, axis=0)) # 5. 通过broadcasting """ - 把不同shape复制相同的shape - 没有复制数据,数据却扩充了 - 小维度先对齐,shape对其即可 """ print(tf.broadcast_to(tf.random.normal([3, 1]), [3, 3]))
123456789101112131415161718192021222324输出为:
通过reshape实现: tf.Tensor( [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]], shape=(4, 3), dtype=int32) 通过转置tf.transpose: tf.Tensor( [[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]], shape=(4, 3), dtype=int32) 通过增加维度: tf.Tensor( [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]], shape=(1, 3, 4), dtype=int32) 通过减少维度: tf.Tensor( [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]], shape=(3, 4), dtype=int32) tf.Tensor( [[ 0.08429808 0.08429808 0.08429808] [ 0.2560939 0.2560939 0.2560939 ] [-1.3891599 -1.3891599 -1.3891599 ]], shape=(3, 3), dtype=float32)
12345678910111213141516171819202122 5. 数学运算 5.1 对元素的操作import tensorflow as tf a = tf.constant([[2, 2], [2, 2]]) b = tf.constant([[2, 2], [2, 2]]) # 1. 元素的加减乘除 print("元素的加法:", a + b) print("元素的减法:", a - b) print("元素的乘法:", a * b) print("元素的除法:", a / b) print("元素的整除:", a // b) print("元素的余除:", a % b) # 2. 元素的幂次方开方 print("元素的平方(1):", tf.square(a)) print("元素的幂次方(2):", a ** 3) print("元素的幂次方(3):", tf.pow(a, 3)) print("元素的开方:", tf.sqrt(tf.cast(a, dtype=tf.float32))) # 3. 元素的指数和对数 print("元素的指数:", tf.exp(tf.cast(a, dtype=tf.float32))) print("元素的对数:", tf.math.log(tf.exp(tf.cast(a, dtype=tf.float32)))) # 以e为底
123456789101112131415161718192021输出为:
元素的加法: tf.Tensor( [[4 4] [4 4]], shape=(2, 2), dtype=int32) 元素的减法: tf.Tensor( [[0 0] [0 0]], shape=(2, 2), dtype=int32) 元素的乘法: tf.Tensor( [[4 4] [4 4]], shape=(2, 2), dtype=int32) 元素的除法: tf.Tensor( [[1. 1.] [1. 1.]], shape=(2, 2), dtype=float64) 元素的整除: tf.Tensor( [[1 1] [1 1]], shape=(2, 2), dtype=int32) 元素的余除: tf.Tensor( [[0 0] [0 0]], shape=(2, 2), dtype=int32) 元素的平方(1): tf.Tensor( [[4 4] [4 4]], shape=(2, 2), dtype=int32) 元素的幂次方(2): tf.Tensor( [[8 8] [8 8]], shape=(2, 2), dtype=int32) 元素的幂次方(3): tf.Tensor( [[8 8] [8 8]], shape=(2, 2), dtype=int32) 元素的开方: tf.Tensor( [[1.4142135 1.4142135] [1.4142135 1.4142135]], shape=(2, 2), dtype=float32) 元素的指数: tf.Tensor( [[7.389056 7.389056] [7.389056 7.389056]], shape=(2, 2), dtype=float32) 元素的对数: tf.Tensor( [[2. 2.] [2. 2.]], shape=(2, 2), dtype=float32)
123456789101112131415161718192021222324252627282930313233343536 5.2 对矩阵的操作import tensorflow as tf a = tf.constant([[2, 2], [2, 2]]) b = tf.constant([[2, 2], [2, 2]]) # 矩阵的乘法 print("矩阵的乘法(1):", a @ b) print("矩阵的乘法(2):", tf.matmul(a, b)) 1234567
输出为:
矩阵的乘法(1): tf.Tensor( [[8 8] [8 8]], shape=(2, 2), dtype=int32) 矩阵的乘法(2): tf.Tensor( [[8 8] [8 8]], shape=(2, 2), dtype=int32) 123456
相关知识
==的作用
第三章 MySQL支持的数据类型
7月26号=》276页
Java 中 == 和 equals() 区别
类型转换之byte
皮亚杰儿童认知发展阶段理论的基本含义
task1
超详细YOLOv8姿态检测全程概述:环境、训练、验证与预测详解
Python中的自增运算
请教 java中 a+=b+=a+=b 的详细运算过程
网址: tensor数据类型基本运算(一) https://www.mcbbbk.com/newsview752522.html
上一篇: 2024年A股创投、北交所概念、 |
下一篇: python实现一个数如果恰好等 |
推荐分享

- 1我的狗老公李淑敏33——如何 5096
- 2南京宠物粮食薄荷饼宠物食品包 4363
- 3家养水獭多少钱一只正常 3825
- 4豆柴犬为什么不建议养?可爱的 3668
- 5自制狗狗辅食:棉花面纱犬的美 3615
- 6狗交配为什么会锁住?从狗狗生 3601
- 7广州哪里卖宠物猫狗的选择性多 3535
- 8湖南隆飞尔动物药业有限公司宠 3477
- 9黄金蟒的价格 3396
- 10益和 MATCHWELL 狗 3352