mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Summary:
To achive this, I modified the blob name scheme defined in a layer.
Before it was scope/fc_w and scope/fc_w_auto_0 (if there is another fc
within the same scope).
Now I change it to scope/fc/w and scope/fc_auto_0/w.
That is, we rely on the uniqueness of the scoped layer name to define
names for blobs.
I also overwrote the create_param method in LayerModelHelper to let it
use the resolved name for blobs given the sharingparameter context.
There are some details such as making the initializer more structured
that I need to finalize.
Reviewed By: kennyhorror
Differential Revision: D5435132
fbshipit-source-id: a0525f5ea0977e255dd5ea765b38913f5951d455
66 lines
1.8 KiB
Python
66 lines
1.8 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 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 = net.Squeeze(
|
|
self.input_record.prediction(),
|
|
net.NextScopedBlob('squeezed_prediction'),
|
|
dims=[1]
|
|
)
|
|
|
|
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.StopGradient(
|
|
label,
|
|
net.NextScopedBlob('stopped_label')
|
|
)
|
|
|
|
l2dist = net.SquaredL2Distance(
|
|
[label, prediction],
|
|
net.NextScopedBlob('l2')
|
|
)
|
|
|
|
net.AveragedLoss(l2dist, self.output_schema.field_blobs())
|