24 lines
421 B
Python
24 lines
421 B
Python
import numpy as np
|
|
import torch
|
|
|
|
a = np.array([1, 2, 3, 10, 9, 8, 7, 6, 5, 4])
|
|
b = torch.from_numpy(a)
|
|
print(b)
|
|
|
|
c = torch.arange(10)
|
|
print(c)
|
|
print(c.dtype, c.layout, c.device)
|
|
|
|
d = int(c.matmul(b))
|
|
#不写int是tensor(274)
|
|
e = c * b
|
|
print(d, e)
|
|
|
|
#in-place与广播
|
|
a1 = torch.tensor([1,1,3])
|
|
a2 = torch.tensor([1])#右对齐
|
|
a3 = torch.add(a1, a2)
|
|
print(a3, a3.layout)
|
|
|
|
#比较
|
|
print(torch.ge(a1, a2), torch.ne(a1, a2)) |