-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
334 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"import gbiz_torch.loss import CoxRegressionLoss" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"labels = torch.randint(0, 2, (8, 1))\n", | ||
"predict = torch.randint(0, 100, (8, 1))/100\n", | ||
"\n", | ||
"cox_loss = CoxRegressionLoss()\n", | ||
"cox_loss(labels, predict)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"import gbiz_torch.loss import MCCrossEntropy" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"##test MCCrossEntropy\n", | ||
"\n", | ||
"labels_d = torch.cat([torch.eye(5), torch.eye(5)], dim=0)\n", | ||
"predict = torch.randn((10, 5), requires_grad=True)\n", | ||
"\n", | ||
"MCCross_Entropy = MCCrossEntropy()\n", | ||
"MCCross_Entropy(labels_d, predict)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"import gbiz_torch.loss import HuberLoss" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"##test HuberLoss\n", | ||
"\n", | ||
"labels = torch.randn(8, 5)\n", | ||
"predict = torch.randn(8, 5)\n", | ||
"\n", | ||
"Huber_Loss = HuberLoss()\n", | ||
"Huber_Loss(labels, predict)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"import gbiz_torch.loss import LogLoss" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"##test LogLoss\n", | ||
"\n", | ||
"labels = torch.randint(0, 5, (8, 1))\n", | ||
"predict = torch.randn((8, 5), requires_grad=True)\n", | ||
"\n", | ||
"Log_loss = LogLoss()\n", | ||
"Log_loss(labels, predict)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .cox_regression_loss import CoxRegressionLoss | ||
from .common_improved_loss import LogLoss, HuberLoss, MCCrossEntropy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# coding:utf-8 | ||
# @Author: Haowen Wang | ||
|
||
import torch | ||
import torch.nn as nn | ||
from torch.nn import NLLLoss | ||
|
||
|
||
class LogLoss(object): | ||
"""Wraps torch.nn.NLLLoss to compute loss in gbiz_torch.""" | ||
|
||
def __init__(self, dim=-1): | ||
""" | ||
Args: | ||
dim: Log soft_max dim | ||
""" | ||
self.m = nn.LogSoftmax(dim=dim) | ||
self.loss = nn.NLLLoss() | ||
|
||
def __call__(self, labels, model_output, weights=1.0): | ||
""" | ||
Args: | ||
labels: label tensor, shape (batchsize) | ||
model_output: model output tensors, shape (batchsize, Class_Num) | ||
weights: sample weights when calculating loss. See | ||
torch.nn.NLLLoss for more details. | ||
""" | ||
labels = labels.reshape([-1]) | ||
log_out = self.m(model_output) | ||
|
||
res = self.loss(log_out, labels) | ||
|
||
return res | ||
|
||
|
||
class MCCrossEntropy(object): | ||
""" | ||
Wraps torch.nn.MultiLabelSoftMarginLoss to compute loss in gbiz_torch. | ||
""" | ||
|
||
def __init__(self): | ||
self.loss = nn.MultiLabelSoftMarginLoss() | ||
|
||
def __call__(self, labels, model_output): | ||
""" | ||
Args: | ||
labels: (N,C) label targets must have the same shape as the input. | ||
model_output: (N,C) where N is the batch size and C is the number of classes. | ||
""" | ||
return self.loss(labels, model_output) | ||
|
||
|
||
class HuberLoss(object): | ||
""" | ||
Wraps torch.nn.HuberLoss to compute HuberLoss in gbiz_torch. | ||
""" | ||
|
||
def __init__(self): | ||
self.loss = nn.HuberLoss() | ||
|
||
def __call__(self, target, input): | ||
""" | ||
Args: | ||
target: (*) same shape as the input | ||
input: (*) where * means any number of dimensions. | ||
""" | ||
res = self.loss(input, target) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# coding:utf-8 | ||
# @Author: Haowen Wang | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class CoxRegressionLoss(object): | ||
def __init__(self): | ||
self.predict_key = 'prediction' | ||
|
||
def __call__(self, labels, model_outputs): | ||
""" | ||
labels: binary input of {0, 1} in shape (batch_size, 1) | ||
model_outputs: probability in range (0, 1) in shape (batch_size, 1) | ||
""" | ||
logits = model_outputs | ||
logits = logits.reshape(-1, 1) | ||
labels = labels.reshape(-1, 1) | ||
|
||
binary_labels = torch.cat([labels, 1 - labels], axis=1) | ||
y_ = binary_labels.to(torch.float32) | ||
y = torch.concat([-torch.log(1.0 - torch.exp(-logits)), logits], 1) | ||
loss = torch.mean(torch.multiply(y, y_)) * 2.0 | ||
return loss |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# coding: utf-8 | ||
# @Author: Haowen Wang | ||
|
||
import torch | ||
import torch.nn as nn | ||
from gbiz_torch.layer import DNNLayer, CINLayer | ||
|
||
|
||
class xDeepFMModel(nn.Module): | ||
""" | ||
Model: xDeepFM Model | ||
Paper: xDeepFM: Combining Explicit and Implicit Feature Interactions for Recommender Systems | ||
Link: https://arxiv.org/abs/1803.05170 | ||
Author: Jianxun Lian, Xiaohuan Zhou, Fuzheng Zhang, Zhongxia Chen, Xing Xie, Guangzhong Sun | ||
Developer: Haowen Wang | ||
inputs: 3d tensor (batch_size, fields, n_dim) | ||
outputs: 2d tensor (batch_size, out_dim) | ||
""" | ||
|
||
def __init__(self, hidden_units, act_fn='relu', l2_reg=0.001, dropout_rate=0, use_bn=False, | ||
seed=1024, cin_hidden_units=[100, 100], cin_act_fn='relu', cin_l2_reg=0.001, name='xDeepFMModel'): | ||
""" | ||
Args: | ||
hidden_units: list, unit in each hidden layer | ||
act_fn: string, activation function | ||
l2_reg: float, regularization value | ||
dropout_rate: float, fraction of the units to dropout. | ||
use_bn: boolean, if True, apply BatchNormalization in each hidden layer | ||
seed: int, random value for initialization | ||
hidden_units: list, unit in each cin layer | ||
act_fn: string, activation function in cin layer | ||
l2_reg: float, regularization value in cin layer | ||
""" | ||
super(xDeepFMModel, self).__init__(name='xDeepFMModel') | ||
self.cin_layer = CINLayer(hidden_units=cin_hidden_units, act_fn=cin_act_fn, | ||
l2_reg=cin_l2_reg, name="{}_cin_layer".format(name)) | ||
self.dnn_layer = DNNLayer(hidden_units=hidden_units, activation=act_fn, l2_reg=l2_reg, | ||
dropout_rate=dropout_rate, use_bn=use_bn, seed=seed, name="{}_dnn_layer".format(name)) | ||
|
||
def call(self, inputs, training=None): | ||
""" | ||
Args: | ||
inputs: 3d tensor (batch_size, fields, n_dim) | ||
Returns: | ||
2d tensor (batch_size, out_dim) | ||
""" | ||
cin_output = self.cin_layer(inputs) | ||
|
||
flat_inputs = tf.keras.layers.Flatten()(inputs) | ||
tf.logging.info('xDeepFMModel: flat_inputs {}'.format(flat_inputs)) | ||
dnn_output = self.dnn_layer(flat_inputs, training=training) | ||
|
||
combined_output = tf.keras.layers.Concatenate()( | ||
[cin_output, dnn_output]) | ||
tf.logging.info( | ||
'xDeepFMModel: combined_output {}'.format(combined_output)) | ||
return combined_output |