/usr/local/lib64/python3.6/site-packages/caffe2/python/layers
NameSizeModeActions
__pycache__/-0755rm
adaptive_weight.py56870644editdlrm
add_bias.py13960644editdlrm
arc_cosine_feature_map.py73450644editdlrm
batch_huber_loss.py35230644editdlrm
batch_lr_loss.py115770644editdlrm
batch_mse_loss.py23330644editdlrm
batch_normalization.py38230644editdlrm
batch_sigmoid_cross_entropy_loss.py14830644editdlrm
batch_softmax_loss.py45800644editdlrm
blob_weighted_sum.py22190644editdlrm
bpr_loss.py14990644editdlrm
bucket_weighted.py23550644editdlrm
build_index.py19370644editdlrm
concat.py48490644editdlrm
constant_weight.py12080644editdlrm
conv.py50500644editdlrm
dropout.py14100644editdlrm
fc.py92960644editdlrm
fc_without_bias.py19540644editdlrm
fc_with_bootstrap.py127880644editdlrm
feature_sparse_to_dense.py143610644editdlrm
functional.py48750644editdlrm
gather_record.py32600644editdlrm
homotopy_weight.py43060644editdlrm
label_smooth.py35070644editdlrm
last_n_window_collector.py23920644editdlrm
layers.py174120644editdlrm
layer_normalization.py42910644editdlrm
margin_rank_loss.py19510644editdlrm
merge_id_lists.py15000644editdlrm
pairwise_similarity.py35490644editdlrm
position_weighted.py20660644editdlrm
random_fourier_features.py31870644editdlrm
reservoir_sampling.py30130644editdlrm
sampling_train.py22100644editdlrm
sampling_trainable_mixin.py13660644editdlrm
select_record_by_context.py23810644editdlrm
semi_random_features.py58090644editdlrm
sparse_dropout_with_replacement.py39430644editdlrm
sparse_feature_hash.py46180644editdlrm
sparse_itemwise_dropout_with_replacement.py39440644editdlrm
sparse_lookup.py221700644editdlrm
split.py22570644editdlrm
tags.py41140644editdlrm
uniform_sampling.py27790644editdlrm
__init__.py9430644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/caffe2/python/layers/batch_softmax_loss.py (4580B)
## @package batch_softmax_loss # Module caffe2.python.layers.batch_softmax_loss from caffe2.python import core, schema from caffe2.python.layers.layers import ModelLayer import numpy as np class BatchSoftmaxLoss(ModelLayer): def __init__( self, model, input_record, name='batch_softmax_loss', label_smoothing_matrix=None, label_prob=False, scale=1.0, average_by_batch_size=False, **kwargs ): super(BatchSoftmaxLoss, self).__init__( model, name, input_record, **kwargs) assert schema.is_schema_subset( schema.Struct( ('label', schema.Scalar()), ('prediction', schema.Scalar()), ), input_record ) self.label_prob = label_prob self.scale = scale self.average_by_batch_size = average_by_batch_size # label smoothing matrix: a K * K matrix where K is the label # cardinality; (i, j) element is the value of for label i # treated/smoothed as label j self.label_smoothing_matrix = label_smoothing_matrix if self.label_smoothing_matrix is not None: self.initialize_label_smoothing_constants() self.output_schema = schema.Struct( ( 'softmax', schema.Scalar( input_record.prediction.field_type(), self.get_next_blob_reference('softmax') ) ), ( 'loss', schema.Scalar( np.float32, self.get_next_blob_reference('loss') ) ), ) def initialize_label_smoothing_constants(self): assert self.label_smoothing_matrix is not None self.label_smoothing_matrix = np.array( self.label_smoothing_matrix).astype(np.float32) assert len(self.label_smoothing_matrix.shape) == 2 label_dim = self.label_smoothing_matrix.shape[0] assert label_dim == self.label_smoothing_matrix.shape[1] self.label_smoothing_matrix = self.model.add_global_constant( '%s_label_smoothing_matrix' % self.name, array=self.label_smoothing_matrix, dtype=np.dtype(np.float32), ) self.label_dim = self.model.add_global_constant( '%s_label_dim' % self.name, array=label_dim, dtype=np.dtype(np.int64), ) # default case: label is given NOT as target distribution # but when used in label smoothing, the label must be in probabilities self.label_prob = True def compute_smoothed_label(self, net): assert self.label_smoothing_matrix is not None label = self.input_record.label() original_label_type = self.input_record.label.field_type() if original_label_type.base != np.int64: int64_label = net.NextScopedBlob('int64_label') net.Cast([label], [int64_label], to=core.DataType.INT64) else: int64_label = label one_hot_label = net.NextScopedBlob('one_hot_label') smoothed_label = net.NextScopedBlob('smoothed_label') net.OneHot([int64_label, self.label_dim], [one_hot_label]) net.MatMul([one_hot_label, self.label_smoothing_matrix], smoothed_label) return smoothed_label def add_ops(self, net): label = self.input_record.label.field_blobs() if self.label_smoothing_matrix is not None: label = [self.compute_smoothed_label(net)] elif not self.label_prob: if self.input_record.label.field_types()[0].base != np.int32: label = [ net.Cast(label, net.NextScopedBlob('int32_label'), to=core.DataType.INT32) ] softmax_input = self.input_record.prediction.field_blobs() + label if 'weight' in self.input_record: 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 ) softmax_input += [weight_blob] net.SoftmaxWithLoss( softmax_input, self.output_schema.field_blobs(), label_prob=self.label_prob, scale=self.scale, average_by_batch_size=self.average_by_batch_size, )