@@ -25,8 +25,13 @@ def test_case_1():
2525 import torch
2626 import torch.nn as nn
2727 loss = torch.nn.BCEWithLogitsLoss(reduction='none')
28- input = torch.tensor([1.,0.7,0.2], requires_grad=True)
29- target = torch.tensor([1.,0., 0.])
28+ input = torch.zeros(3)
29+ input[0] = 1.0
30+ input[1] = 0.7
31+ input[2] = 0.2
32+ input.requires_grad = True
33+ target = torch.zeros(3)
34+ target[0] = 1.0
3035 result = loss(input, target)
3136 """
3237 )
@@ -38,9 +43,17 @@ def test_case_2():
3843 """
3944 import torch
4045 import torch.nn as nn
41- loss = nn.BCEWithLogitsLoss(weight=torch.tensor([1.0,0.2, 0.2]), reduction='none')
42- input = torch.tensor([1.,0.7,0.2], requires_grad=True)
43- target = torch.tensor([1.,0., 0.])
46+ weight = torch.ones(3)
47+ weight[1] = 0.2
48+ weight[2] = 0.2
49+ loss = nn.BCEWithLogitsLoss(weight=weight, reduction='none')
50+ input = torch.zeros(3)
51+ input[0] = 1.0
52+ input[1] = 0.7
53+ input[2] = 0.2
54+ input.requires_grad = True
55+ target = torch.zeros(3)
56+ target[0] = 1.0
4457 result = loss(input, target)
4558 """
4659 )
@@ -52,9 +65,14 @@ def test_case_3():
5265 """
5366 import torch
5467 import torch.nn as nn
55- loss= nn.BCEWithLogitsLoss(pos_weight = torch.ones([3]))
56- input = torch.tensor([1.,0.7,0.2], requires_grad=True)
57- target = torch.tensor([1.,0., 0.])
68+ loss= nn.BCEWithLogitsLoss(pos_weight=torch.ones(3))
69+ input = torch.zeros(3)
70+ input[0] = 1.0
71+ input[1] = 0.7
72+ input[2] = 0.2
73+ input.requires_grad = True
74+ target = torch.zeros(3)
75+ target[0] = 1.0
5876 result = loss(input, target)
5977 """
6078 )
@@ -67,8 +85,13 @@ def test_case_4():
6785 import torch
6886 import torch.nn as nn
6987 loss = nn.BCEWithLogitsLoss(size_average=True)
70- input = torch.tensor([1.,0.7,0.2], requires_grad=True)
71- target = torch.tensor([1.,0., 0.])
88+ input = torch.zeros(3)
89+ input[0] = 1.0
90+ input[1] = 0.7
91+ input[2] = 0.2
92+ input.requires_grad = True
93+ target = torch.zeros(3)
94+ target[0] = 1.0
7295 result = loss(input, target)
7396 """
7497 )
@@ -81,8 +104,13 @@ def test_case_5():
81104 import torch
82105 import torch.nn as nn
83106 loss = nn.BCEWithLogitsLoss()
84- input = torch.tensor([1.,0.7,0.2], requires_grad=True)
85- target = torch.tensor([1.,0., 0.])
107+ input = torch.zeros(3)
108+ input[0] = 1.0
109+ input[1] = 0.7
110+ input[2] = 0.2
111+ input.requires_grad = True
112+ target = torch.zeros(3)
113+ target[0] = 1.0
86114 result = loss(input, target)
87115 """
88116 )
@@ -94,9 +122,17 @@ def test_case_6():
94122 """
95123 import torch
96124 import torch.nn as nn
97- loss = nn.BCEWithLogitsLoss(weight=torch.tensor([1.0,0.2, 0.2]), size_average=None, reduce=None, reduction='none', pos_weight=None)
98- input = torch.tensor([1.,0.7,0.2], requires_grad=True)
99- target = torch.tensor([1.,0., 0.])
125+ weight = torch.ones(3)
126+ weight[1] = 0.2
127+ weight[2] = 0.2
128+ loss = nn.BCEWithLogitsLoss(weight=weight, size_average=None, reduce=None, reduction='none', pos_weight=None)
129+ input = torch.zeros(3)
130+ input[0] = 1.0
131+ input[1] = 0.7
132+ input[2] = 0.2
133+ input.requires_grad = True
134+ target = torch.zeros(3)
135+ target[0] = 1.0
100136 result = loss(input, target)
101137 """
102138 )
@@ -109,9 +145,17 @@ def test_case_7():
109145 """
110146 import torch
111147 import torch.nn as nn
112- loss = nn.BCEWithLogitsLoss(torch.tensor([1.0,0.2, 0.2]), None, None, 'none', None)
113- input = torch.tensor([1.,0.7,0.2], requires_grad=True)
114- target = torch.tensor([1.,0., 0.])
148+ weight = torch.ones(3)
149+ weight[1] = 0.2
150+ weight[2] = 0.2
151+ loss = nn.BCEWithLogitsLoss(weight, None, None, 'none', None)
152+ input = torch.zeros(3)
153+ input[0] = 1.0
154+ input[1] = 0.7
155+ input[2] = 0.2
156+ input.requires_grad = True
157+ target = torch.zeros(3)
158+ target[0] = 1.0
115159 result = loss(input, target)
116160 """
117161 )
@@ -124,9 +168,17 @@ def test_case_8():
124168 """
125169 import torch
126170 import torch.nn as nn
127- loss = nn.BCEWithLogitsLoss(pos_weight=None, reduction='none', reduce=None, size_average=None, weight=torch.tensor([1.0,0.2, 0.2]))
128- input = torch.tensor([1.,0.7,0.2], requires_grad=True)
129- target = torch.tensor([1.,0., 0.])
171+ weight = torch.ones(3)
172+ weight[1] = 0.2
173+ weight[2] = 0.2
174+ loss = nn.BCEWithLogitsLoss(pos_weight=None, reduction='none', reduce=None, size_average=None, weight=weight)
175+ input = torch.zeros(3)
176+ input[0] = 1.0
177+ input[1] = 0.7
178+ input[2] = 0.2
179+ input.requires_grad = True
180+ target = torch.zeros(3)
181+ target[0] = 1.0
130182 result = loss(input, target)
131183 """
132184 )
0 commit comments