import torch


x = torch.empty(5, 3) # 生成空的矩阵
print(x)

x = torch.rand(5, 4) # 生成随机矩阵
print(x)


x = torch.zeros(5, 4, dtype=torch.long) # 生成空矩阵
print(x)

x = torch.tensor([5, 3]) # 将列表转换维tensor类型
print(x)

x = x.new_ones([5, 3], dtype=torch.double) # 构造全1的数组
print(x)
x = torch.rand_like(x, dtype=torch.float) # 生成相同维度的随机矩阵
print(x.size())

y = torch.rand(5, 3) # 构造随机的矩阵
print(x + y)

torch.add(x, y) # 进行矩阵的相加操作

# 索引
print(x[:, 1])

# view操作可以改变矩阵维度

x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)

a = torch.ones(5)  # tensor
b = a.numpy()  # 将tensor转换为numpy格式

import numpy as np
a = np.ones([3, 2])
b = torch.from_numpy(a) # 将a转换为tensor格式
print(b)

 

posted on 2020-04-06 17:33  python我的最爱  阅读(2626)  评论(0编辑  收藏  举报