rename head -> gears
parent
55943da6d1
commit
586af751ac
|
@ -18,10 +18,10 @@ import importlib
|
|||
import paddle.nn as nn
|
||||
|
||||
from . import backbone
|
||||
from . import head
|
||||
from . import gears
|
||||
|
||||
from .backbone import *
|
||||
from .head import *
|
||||
from .gears import *
|
||||
from .utils import *
|
||||
|
||||
__all__ = ["build_model", "RecModel"]
|
||||
|
|
|
@ -19,10 +19,11 @@ from .fc import FC
|
|||
|
||||
__all__ = ['build_head']
|
||||
|
||||
|
||||
def build_head(config):
|
||||
support_dict = ['ArcMargin', 'CosMargin', 'CircleMargin', 'FC']
|
||||
module_name = config.pop('name')
|
||||
assert module_name in support_dict, Exception('head only support {}'.format(
|
||||
support_dict))
|
||||
assert module_name in support_dict, Exception(
|
||||
'head only support {}'.format(support_dict))
|
||||
module_class = eval(module_name)(**config)
|
||||
return module_class
|
|
@ -16,30 +16,32 @@ import math
|
|||
import paddle
|
||||
import paddle.nn as nn
|
||||
import paddle.nn.functional as F
|
||||
|
||||
|
||||
|
||||
class CircleMargin(nn.Layer):
|
||||
def __init__(self, embedding_size,
|
||||
class_num,
|
||||
margin,
|
||||
scale):
|
||||
def __init__(self, embedding_size, class_num, margin, scale):
|
||||
super(CircleSoftmax, self).__init__()
|
||||
self.scale = scale
|
||||
self.scale = scale
|
||||
self.margin = margin
|
||||
self.embedding_size = embedding_size
|
||||
self.class_num = class_num
|
||||
|
||||
weight_attr = paddle.ParamAttr(initializer = paddle.nn.initializer.XavierNormal())
|
||||
self.fc0 = paddle.nn.Linear(self.embedding_size, self.class_num, weight_attr=weight_attr)
|
||||
|
||||
weight_attr = paddle.ParamAttr(
|
||||
initializer=paddle.nn.initializer.XavierNormal())
|
||||
self.fc0 = paddle.nn.Linear(
|
||||
self.embedding_size, self.class_num, weight_attr=weight_attr)
|
||||
|
||||
def forward(self, input, label):
|
||||
feat_norm = paddle.sqrt(paddle.sum(paddle.square(input), axis=1, keepdim=True))
|
||||
feat_norm = paddle.sqrt(
|
||||
paddle.sum(paddle.square(input), axis=1, keepdim=True))
|
||||
input = paddle.divide(input, feat_norm)
|
||||
|
||||
weight = self.fc0.weight
|
||||
weight_norm = paddle.sqrt(paddle.sum(paddle.square(weight), axis=0, keepdim=True))
|
||||
weight_norm = paddle.sqrt(
|
||||
paddle.sum(paddle.square(weight), axis=0, keepdim=True))
|
||||
weight = paddle.divide(weight, weight_norm)
|
||||
|
||||
logits = paddle.matmul(input, weight)
|
||||
|
||||
logits = paddle.matmul(input, weight)
|
||||
|
||||
alpha_p = paddle.clip(-logits.detach() + 1 + self.margin, min=0.)
|
||||
alpha_n = paddle.clip(logits.detach() + self.margin, min=0.)
|
||||
|
@ -51,5 +53,5 @@ class CircleMargin(nn.Layer):
|
|||
logits_n = alpha_n * (logits - delta_n)
|
||||
pre_logits = logits_p * m_hot + logits_n * (1 - m_hot)
|
||||
pre_logits = self.scale * pre_logits
|
||||
|
||||
|
||||
return pre_logits
|
|
@ -16,35 +16,41 @@ import paddle
|
|||
import math
|
||||
import paddle.nn as nn
|
||||
|
||||
|
||||
class CosMargin(paddle.nn.Layer):
|
||||
def __init__(self, embedding_size,
|
||||
class_num,
|
||||
margin=0.35,
|
||||
scale=64.0):
|
||||
def __init__(self, embedding_size, class_num, margin=0.35, scale=64.0):
|
||||
super(CosMargin, self).__init__()
|
||||
self.scale = scale
|
||||
self.margin = margin
|
||||
self.embedding_size = embedding_size
|
||||
self.class_num = class_num
|
||||
|
||||
weight_attr = paddle.ParamAttr(initializer = paddle.nn.initializer.XavierNormal())
|
||||
self.fc = nn.Linear(self.embedding_size, self.class_num, weight_attr=weight_attr, bias_attr=False)
|
||||
|
||||
|
||||
weight_attr = paddle.ParamAttr(
|
||||
initializer=paddle.nn.initializer.XavierNormal())
|
||||
self.fc = nn.Linear(
|
||||
self.embedding_size,
|
||||
self.class_num,
|
||||
weight_attr=weight_attr,
|
||||
bias_attr=False)
|
||||
|
||||
def forward(self, input, label):
|
||||
label.stop_gradient = True
|
||||
|
||||
input_norm = paddle.sqrt(paddle.sum(paddle.square(input), axis=1, keepdim=True))
|
||||
input = paddle.divide(input, x_norm)
|
||||
input_norm = paddle.sqrt(
|
||||
paddle.sum(paddle.square(input), axis=1, keepdim=True))
|
||||
input = paddle.divide(input, x_norm)
|
||||
|
||||
weight = self.fc.weight
|
||||
weight_norm = paddle.sqrt(paddle.sum(paddle.square(weight), axis=0, keepdim=True))
|
||||
weight_norm = paddle.sqrt(
|
||||
paddle.sum(paddle.square(weight), axis=0, keepdim=True))
|
||||
weight = paddle.divide(weight, weight_norm)
|
||||
|
||||
cos = paddle.matmul(input, weight)
|
||||
cos = paddle.matmul(input, weight)
|
||||
cos_m = cos - self.margin
|
||||
|
||||
|
||||
one_hot = paddle.nn.functional.one_hot(label, self.class_num)
|
||||
one_hot = paddle.squeeze(one_hot, axis=[1])
|
||||
output = paddle.multiply(one_hot, cos_m) + paddle.multiply((1.0 - one_hot), cos)
|
||||
output = paddle.multiply(one_hot, cos_m) + paddle.multiply(
|
||||
(1.0 - one_hot), cos)
|
||||
output = output * self.scale
|
||||
return output
|
|
@ -19,14 +19,16 @@ from __future__ import print_function
|
|||
import paddle
|
||||
import paddle.nn as nn
|
||||
|
||||
|
||||
class FC(nn.Layer):
|
||||
def __init__(self, embedding_size,
|
||||
class_num):
|
||||
def __init__(self, embedding_size, class_num):
|
||||
super(FC, self).__init__()
|
||||
self.embedding_size = embedding_size
|
||||
self.embedding_size = embedding_size
|
||||
self.class_num = class_num
|
||||
weight_attr = paddle.ParamAttr(initializer = paddle.nn.initializer.XavierNormal())
|
||||
self.fc = paddle.nn.Linear(self.embedding_size, self.class_num, weight_attr=weight_attr)
|
||||
weight_attr = paddle.ParamAttr(
|
||||
initializer=paddle.nn.initializer.XavierNormal())
|
||||
self.fc = paddle.nn.Linear(
|
||||
self.embedding_size, self.class_num, weight_attr=weight_attr)
|
||||
|
||||
def forward(self, input, label):
|
||||
out = self.fc(input)
|
Loading…
Reference in New Issue