pytorch和python

#########################################################################################################################
a = torch.tensor([[1,2,3],[4,5,6]])
b = torch.tensor([[1,2,3],[4,5,6]])
print(a)
print(b)
m,x = a.max(1)  # Return the maximum along a given axis. value and its index
n,y = b.max(1)
print(m,x)
print(n,y)
#
print((x == y).sum())
print((x == y).sum().item())


tensor([[1, 2, 3],
        [4, 5, 6]])
tensor([[1, 2, 3],
        [4, 5, 6]])
tensor([3, 6]) tensor([2, 2])
tensor([3, 6]) tensor([2, 2])
tensor(2)
2

#########################################################################################################################
a = torch.tensor([[1,2,3],[4,5,6]])
b = torch.tensor([[1,2,3],[4,5,6]])
print(a)
print(b)
m,x = a.max(0)
n,y = b.max(0)
print(m,x)
print(n,y)
#
print((x == y).sum())
print((x == y).sum().item())
# 可以看到max函数返回的是value and its index(item),可以通过max(1)指定维度。比如指定0,获得的是最大的行矩阵,指定1,获得的是最大的单个值。
# 可以用torch.tersor()创建张量,

tensor([[1, 2, 3],
        [4, 5, 6]])
tensor([[1, 2, 3],
        [4, 5, 6]])
tensor([4, 5, 6]) tensor([1, 1, 1])
tensor([4, 5, 6]) tensor([1, 1, 1])
tensor(3)
3

#########################################################################################################################
a = [1,2,3]
b = iter(a)
print(b)
c = b.__next__() 
print(c) # 1
d = b.__next__() 
print(d) # 2

# 验证gup
import torch 
torch.cuda.is_available()
# 查看gpu硬件
lspci | grep -i vga

Guess you like

Origin blog.csdn.net/weixin_44716147/article/details/120100962