MCPcopy Index your code
hub / github.com/pytorch/pytorch / Check

Method Check

caffe2/python/gradient_checker.py:121–153  ·  view source on GitHub ↗
(net, outputs_with_grad, input_values,
              input_to_check, step_size=0.0001,
              threshold=0.05, print_net=True)

Source from the content-addressed store, hash-verified

119
120 @staticmethod
121 def Check(net, outputs_with_grad, input_values,
122 input_to_check, step_size=0.0001,
123 threshold=0.05, print_net=True):
124
125 net_results, net_grads, full_net = _get_grad(
126 net, [], outputs_with_grad, input_values, [input_to_check])
127 analytic_grad = net_grads[input_to_check]
128
129 def GetLoss(new_value):
130 workspace.blobs[input_to_check] = new_value
131 workspace.RunNetOnce(full_net)
132 return sum([
133 workspace.blobs[output]
134 for output in outputs_with_grad
135 ]).sum()
136
137 def GetValue(dim, delta):
138 input_value = input_values[input_to_check].copy()
139 input_value.flat[dim] += delta
140 return input_value
141
142 grad_estimate = np.zeros_like(input_values[input_to_check])
143 for dim in range(input_values[input_to_check].size):
144 pos_loss = GetLoss(GetValue(dim, step_size))
145 neg_loss = GetLoss(GetValue(dim, -step_size))
146 grad_estimate.flat[dim] = (pos_loss - neg_loss) / step_size / 2
147
148 err_msg = "Error in gradient check for net_copy {}".format(
149 net.Name())
150 if print_net:
151 err_msg += ": {}".format(net.Proto())
152
153 return _assert_close(analytic_grad, grad_estimate, threshold, err_msg)
154
155
156class GradientChecker:

Callers 2

test_multi_lstmMethod · 0.80

Calls 6

_get_gradFunction · 0.85
_assert_closeFunction · 0.85
rangeFunction · 0.50
formatMethod · 0.45
NameMethod · 0.45
ProtoMethod · 0.45

Tested by 2

test_multi_lstmMethod · 0.64