深度学习——nn.BCELoss(),nn.CrossEntropyLoss(),nn.BCEWithLogitsLoss()

nn.CrossEntropyLoss()

A=torch.randn((1,2,3,4))
torch.manual_seed(100)
B=torch.randint(0,2,(1,3,4))
# print(A)
# print(B)
entropy=torch.nn.CrossEntropyLoss()
print(entropy(A,B))
A=torch.randn((3,2,3,4))
torch.manual_seed(100)
B=torch.randint(0,2,(3,3,4))
# print(A)
# print(B)
entropy=torch.nn.CrossEntropyLoss()
print(entropy(A,B))
#类别数不可以超过输入通道数
C=torch.randn((1,3,3,4))
torch.manual_seed(100)
D=torch.randint(0,3,(1,3,4))
# print(A)
# print(B)
entropy=torch.nn.CrossEntropyLoss()
print(entropy(C,D))

nn.BCELOSS()

#使用前提,形状必须相同
C=torch.randn((1,2,3,4)).sigmoid()  
torch.manual_seed(100)
D=torch.randint(0,2,(1,2,3,4)).float()
print(C)
print(D)
bce=torch.nn.BCELoss()   
print(bce(C,D))
C=torch.randn((1,3,4)).sigmoid()
torch.manual_seed(100)
D=torch.randint(0,2,(1,3,4)).float()
print(C)
print(D)
bce=torch.nn.BCELoss()   #使用前提,形状必须相同
print(bce(C,D))

nn.BCEWithLogitsLoss()

bcew=torch.nn.BCEWithLogitsLoss()   #带有sigmoid()
E=torch.randn((1,1,3,4))
torch.manual_seed(100)
F=torch.randint(0,2,(1,3,4)).float()
print(bcew(E,F))

你可能感兴趣的:(深度学习,深度学习,人工智能)