| 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 | |
| 156 | class GradientChecker: |