pytorch的基本方法

1.如何判断一个对象是否为Tensor?

## 1.如何判断一个对象是否为Tensor?
obj = np.arange(1,10)  # arange() 主要是用于生成数组
print(torch.is_tensor(obj))
obj1 = torch.Tensor(10)
print(obj1)
print(torch.is_tensor(obj1))
-----------------------------------------------------------
result:
False
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
True
tensor([[0., 0.],
        [0., 0.]])

2.如何全局设置Tensor的数据类型?

torch.set_default_tensor_type(torch.DoubleTensor)  # DoubleTensor 为 64-bit floating point
print(torch.Tensor(2).dtype)                       # torch.Tensor = torch.FloatTensor 为 32-bit floating point
------------------------------------------------------------
result:
torch.float64
tensor([[0., 0.],
        [0., 0.]])
        最终全局的生成Tensor数据类型皆为 torch.float64

3.如何判断一个对象是否为 pytorch Storage 对象?

obj = np.arange(1,10)
print(torch.is_storage(obj))
storage = torch.DoubleStorage(10)
print(torch.is_storage(storage))
---------------------------------------------------------------
result:
False
True
tensor([[0., 0.],
        [0., 0.]])

4.如何获取Tensor中元素的个数?

a = torch.Tensor(3,4)           # (3,4)三行四列的0.      [3,4] 转化为FloatTensor
print(a)
print(torch.numel(a))         # numel:元素个数
-----------------------------------------------------------------
result:
12
tensor([[0., 0.],
        [0., 0.]])

5.如何创建指定形状的单位矩阵?

print(torch.eye(3,3))
------------------------------------------------------------------
result:
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

6.如何从NumPy多为数组中创建Tensor?

a = np.arange(1,20,2)
print(torch.from_numpy(a))
b = np.ndarray((2,3))      # ndarray: 多维数组
print(b)
print(torch.from_numpy(b))
--------------------------------------------------------------------
result:
tensor([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19], dtype=torch.int32)
[[-1.32114451e-077  1.07583145e-311  1.07583145e-311]
 [ 1.91987942e-263  1.07583145e-311  1.07583145e-311]]
tensor([[-1.3211e-77, 1.0758e-311, 1.0758e-311],
        [1.9199e-263, 1.0758e-311, 1.0758e-311]], dtype=torch.float64)
tensor([[0., 0.],
        [0., 0.]])

7.如何创建等差数列

# strat / end / step
print(torch.linspace(1,9,5,requires_grad=True))
------------------------------------------------------------------
result:
tensor([1., 3., 5., 7., 9.], requires_grad=True)

8.如何创建全为1矩阵

print(torch.ones(2,3))
---------------------------------------------------------------------
result:
tensor([[1., 1., 1.],
        [1., 1., 1.]])

9.创建均匀分布的随机矩阵

print(torch.rand(2,3))
-----------------------------------------------------------------------
result:
tensor([[0.9824, 0.0894, 0.1111],
        [0.1677, 0.2522, 0.9551]])

10.创建服从正态分布的随机矩阵

print(torch.randn(2,3))
-----------------------------------------------------------------------
result:
tensor([[-0.6391,  1.0262,  0.3605],
        [ 2.3658, -1.2214, -0.3639]])

11.创建一个随机整数序列

print(numpy.random.permutation(10))
print(torch.randperm(10))
----------------------------------------------------------------------
result:
[4 2 5 9 1 3 0 8 6 7]
tensor([5, 1, 8, 7, 4, 2, 6, 3, 9, 0])

12.创建一个列表如同numpy中的arange()

print(np.arange(1,10,3))
print(torch.arange(1,10,3))
--------------------------------------------------------------------
result:
[1 4 7]
tensor([1, 4, 7])

13. 如何创建一个元素全为0的矩阵

print(torch.zeros(2,2))
---------------------------------------------------------------------
result:
tensor([[0., 0.],
        [0., 0.]])
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值