/
usr
/
local
/
lib64
/
python3.6
/
site-packages
/
caffe2
/
python
/
layers
/
/usr/local/lib64/python3.6/site-packages/caffe2/python/layers
mkdir
upload
Name
Size
Mode
Actions
__pycache__/
-
0755
rm
adaptive_weight.py
5687
0644
edit
dl
rm
add_bias.py
1396
0644
edit
dl
rm
arc_cosine_feature_map.py
7345
0644
edit
dl
rm
batch_huber_loss.py
3523
0644
edit
dl
rm
batch_lr_loss.py
11577
0644
edit
dl
rm
batch_mse_loss.py
2333
0644
edit
dl
rm
batch_normalization.py
3823
0644
edit
dl
rm
batch_sigmoid_cross_entropy_loss.py
1483
0644
edit
dl
rm
batch_softmax_loss.py
4580
0644
edit
dl
rm
blob_weighted_sum.py
2219
0644
edit
dl
rm
bpr_loss.py
1499
0644
edit
dl
rm
bucket_weighted.py
2355
0644
edit
dl
rm
build_index.py
1937
0644
edit
dl
rm
concat.py
4849
0644
edit
dl
rm
constant_weight.py
1208
0644
edit
dl
rm
conv.py
5050
0644
edit
dl
rm
dropout.py
1410
0644
edit
dl
rm
fc.py
9296
0644
edit
dl
rm
fc_without_bias.py
1954
0644
edit
dl
rm
fc_with_bootstrap.py
12788
0644
edit
dl
rm
feature_sparse_to_dense.py
14361
0644
edit
dl
rm
functional.py
4875
0644
edit
dl
rm
gather_record.py
3260
0644
edit
dl
rm
homotopy_weight.py
4306
0644
edit
dl
rm
label_smooth.py
3507
0644
edit
dl
rm
last_n_window_collector.py
2392
0644
edit
dl
rm
layers.py
17412
0644
edit
dl
rm
layer_normalization.py
4291
0644
edit
dl
rm
margin_rank_loss.py
1951
0644
edit
dl
rm
merge_id_lists.py
1500
0644
edit
dl
rm
pairwise_similarity.py
3549
0644
edit
dl
rm
position_weighted.py
2066
0644
edit
dl
rm
random_fourier_features.py
3187
0644
edit
dl
rm
reservoir_sampling.py
3013
0644
edit
dl
rm
sampling_train.py
2210
0644
edit
dl
rm
sampling_trainable_mixin.py
1366
0644
edit
dl
rm
select_record_by_context.py
2381
0644
edit
dl
rm
semi_random_features.py
5809
0644
edit
dl
rm
sparse_dropout_with_replacement.py
3943
0644
edit
dl
rm
sparse_feature_hash.py
4618
0644
edit
dl
rm
sparse_itemwise_dropout_with_replacement.py
3944
0644
edit
dl
rm
sparse_lookup.py
22170
0644
edit
dl
rm
split.py
2257
0644
edit
dl
rm
tags.py
4114
0644
edit
dl
rm
uniform_sampling.py
2779
0644
edit
dl
rm
__init__.py
943
0644
edit
dl
rm
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, )
Save
cmd:
run