mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-15 21:00:47 +00:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/28265 Fix the difference in dper3 and dper2 when regressionLoss is used. Test Plan: test using dper2 model id f134632386 Comparison tool output before change: ``` FOUND OP DIFFERENT WITH DPER2!!! OP is of type ExpandDims OP inputs ['supervision:label'] OP outputs ['sparse_nn/regression_loss/mean_squared_error_loss/ExpandDims:0'] =============================== Finished all dper3 ops, number of good ops 11, bad ops 1, skipped 26 run_comparison for dper2 / dper3 nets running time: 0.0020143985748291016 result type: <class 'NoneType'> result: None ``` After change: ``` FOUND OP DIFFERENT WITH DPER2!!! OP is of type ExpandDims OP inputs ['sparse_nn_2/regression_loss_2/mean_squared_error_loss_8/Squeeze:0_grad'] OP outputs ['sparse_nn_2/over_arch_2/linear_2/FC_grad'] =============================== Finished all dper3 ops, number of good ops 19, bad ops 1, skipped 16 run_comparison for dper2 / dper3 nets running time: 0.0017991065979003906 result type: <class 'NoneType'> result: None ``` dper2 label part of net P111794577 dper3 label part of net after change P116817194 Reviewed By: kennyhorror Differential Revision: D17795740 fbshipit-source-id: 9faf96f5140f5a1efdf2985820bda3ca400f61fa
79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
## @package batch_mse_loss
|
|
# Module caffe2.python.layers.batch_mse_loss
|
|
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
|
|
from caffe2.python import core, schema
|
|
from caffe2.python.layers.layers import (
|
|
ModelLayer,
|
|
)
|
|
from caffe2.python.layers.tags import (
|
|
Tags
|
|
)
|
|
import numpy as np
|
|
|
|
|
|
class BatchMSELoss(ModelLayer):
|
|
|
|
def __init__(self, model, input_record, name='batch_mse_loss', **kwargs):
|
|
super(BatchMSELoss, self).__init__(model, name, input_record, **kwargs)
|
|
|
|
assert schema.is_schema_subset(
|
|
schema.Struct(
|
|
('label', schema.Scalar()),
|
|
('prediction', schema.Scalar())
|
|
),
|
|
input_record
|
|
)
|
|
self.tags.update([Tags.EXCLUDE_FROM_PREDICTION])
|
|
|
|
self.output_schema = schema.Scalar(
|
|
np.float32,
|
|
self.get_next_blob_reference('output'))
|
|
|
|
def add_ops(self, net):
|
|
prediction = self.input_record.prediction()
|
|
label = self.input_record.label.field_blobs()
|
|
if self.input_record.label.field_type().base != (
|
|
self.input_record.prediction.field_type().base):
|
|
|
|
label = net.Cast(
|
|
label,
|
|
net.NextScopedBlob('cast_label'),
|
|
to=schema.data_type_for_dtype(
|
|
self.input_record.prediction.field_type()
|
|
)
|
|
)
|
|
|
|
label = net.ExpandDims(label, 1, dims=[1])
|
|
|
|
label = net.StopGradient(
|
|
label,
|
|
net.NextScopedBlob('stopped_label')
|
|
)
|
|
|
|
l2dist = net.SquaredL2Distance(
|
|
[label, prediction],
|
|
net.NextScopedBlob('l2')
|
|
)
|
|
|
|
if 'weight' in self.input_record.fields:
|
|
weight_blob = self.input_record.weight()
|
|
if self.input_record.weight.field_type().base != np.float32:
|
|
weight_blob = net.Cast(
|
|
weight_blob,
|
|
weight_blob + '_float32',
|
|
to=core.DataType.FLOAT
|
|
)
|
|
weight_blob = net.StopGradient(
|
|
[weight_blob],
|
|
[net.NextScopedBlob('weight_stop_gradient')],
|
|
)
|
|
l2dist = net.Mul(
|
|
[l2dist, weight_blob],
|
|
net.NextScopedBlob('weighted_l2_distance'),
|
|
)
|
|
|
|
net.AveragedLoss(l2dist, self.output_schema.field_blobs())
|