飞道的博客

pytorch的基本方法

206人阅读  评论(0)

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.]])

转载:https://blog.csdn.net/weixin_49710179/article/details/115566309
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场