用 numpy 创建 tensor:
import numpy as np
import torch
a_np = np.array([2,3.3])
a_tensor = torch.from_numpy(a_np)
print("a_tensor: ", a_tensor)
用 list 创建 tensor
import torch
a_list = [2,3.3]
a_torch = torch.tensor(a_list)
print("a_torch: ", a_torch)
print("a_torch.dtype: ", a_torch.dtype)
Tensor与tensor
import torch
data = [1,2.1]
print("Tensor(data):\n", torch.Tensor(data))
print("Tensor(2,3):\n", torch.Tensor(2,3))
print("tensor(data):\n", torch.tensor(data))
try:
print("tensor(2,3):\n", torch.tensor(2,3))
except TypeError as e:
print("tensor(2,3)代码运行报错")
print(e)
- tensor只能转换数值数据,不能输入shape数据
- Tensor既能转换数值数据,也能输入shape数据
未初始化
import torch
print("torch.empty([2,3]):\n", torch.empty([2,3]))
print("torch.empty(2,3):\n", torch.empty(2,3))
print("torch.FloatTensor([2,3]):\n", torch.FloatTensor([2,3]))
print("torch.FloatTensor(2,3):\n", torch.FloatTensor(2,3))
- 在创建张量后,要记得初始化,不然可能会对之后运算造成严重影响。
设置默认类型
import torch
print(torch.tensor([1,2.3]).type())
torch.set_default_tensor_type(torch.DoubleTensor)
print(torch.tensor([1,2.3]).type())
数据初始化1,均匀分布
import torch
def showMsg(t, tname):
print("{}:\n{}".format(tname, t))
print("{}平均值:{}\n".format(tname, t.sum() / t.numel()))
a = torch.rand(3, 3)
showMsg(a, "rand")
a_like = torch.rand_like(a)
showMsg(a_like, "a_like")
a_int = torch.randint(1, 10, [3, 3])
showMsg(a_int, "a_int")
a_int_like = torch.randint_like(a_int, 1, 10)
showMsg(a_int_like, "a_int_like")
数据初始化2,正态分布
import torch
def showMsg(t, tname):
print("{}:\n{}".format(tname, t))
mean = t.sum() / t.numel()
print("{}平均值:{}".format(tname, mean))
print("{}方差:{}\n".format(tname, ((t - mean) ** 2).sum() / t.numel() ))
showMsg(torch.randn(300,300), "标准正态分布")
'''
生成符合一定条件的正态分布矩阵
torch.normal(mean, standard_Deviation, shape)
mean: 均值
standard_Deviation: 标准差
shape: 生成矩阵的阶数
'''
a = torch.normal(1, 10 ,(300, 300))
showMsg(a, "正态分布")
统一初始化
import torch
def showMsg(t, tname):
print("{}:\n{}\n".format(tname, t))
showMsg(torch.full([2, 3], 0), "full0")
showMsg(torch.full([3, 3], 1), "full1")
showMsg(torch.full([], 2), "full2")
showMsg(torch.full([1], 3), "full3")
创建序列1,按步长创建
import torch
def showMsg(t, tname):
print("{}:\n{}\n".format(tname, t))
showMsg(torch.arange(0, 10), "arange0-10")
showMsg(torch.arange(0, 10,2), "arange0-10,step=2")
showMsg(torch.range(0, 10), "range0-10")
showMsg(torch.range(0, 10, 2), "range0-10,step=2")
- 注:torch.range在使用时会报警告UserWarning,不建议使用,尽量用arange。
创建序列2,按数量创建
import torch
def showMsg(t, tname):
print("{}:\n{}\n".format(tname, t))
showMsg(torch.linspace(0, 10), "linspace默认数量")
showMsg(torch.linspace(0, 10, steps=4), "linspace, steps=4")
showMsg(torch.logspace(0, 1), "logspace")
showMsg(torch.logspace(0, 1, base=2, steps=4), "logspace,base=2, steps=4")
- 注意:使用时不指定数量会报警告UserWarnig,尽量指定生成数量steps。
创建全1矩阵、全0矩阵
import torch
def showMsg(t, tname):
print("{}:\n{}\n".format(tname, t))
a = torch.ones(3, 3)
showMsg(a, "全1矩阵")
showMsg(torch.ones_like(a), "全1矩阵_like")
showMsg(torch.zeros(3, 3), "全0矩阵")
showMsg(torch.zeros_like(a), "全0矩阵_like")
创建单位矩阵
import torch
def showMsg(t, tname):
print("{}:\n{}\n".format(tname, t))
showMsg(torch.eye(3,3), "单位矩阵1")
showMsg(torch.eye(3,4), "单位矩阵2")
showMsg(torch.eye(3), "单位矩阵3")
随机打乱
import torch
def showMsg(t, tname):
print("{}:\n{}\n".format(tname, t))
showMsg(torch.randperm(10), "randperm")
math_scores = [
["张三", 90],
["李四", 85]
]
english_scores = [
["张三", 80],
["李四", 90]
]
idx = torch.randperm(2)
print("姓名\t数学\t英语")
for i in idx:
print("{}\t{}\t{}".format(math_scores[i][0], math_scores[i][1], english_scores[i][1]))
|