Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Pytorch Widedeep
提交
453220f6
P
Pytorch Widedeep
项目概览
Greenplum
/
Pytorch Widedeep
11 个月 前同步成功
通知
9
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Pytorch Widedeep
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
453220f6
编写于
7月 17, 2020
作者:
J
jrzaurin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added precision. Need to test the multiclass case. For binary seems to work fine. More test needed
上级
31c2d8ef
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
76 addition
and
4 deletion
+76
-4
examples/adult_script.py
examples/adult_script.py
+2
-2
examples/airbnb_script_multiclass.py
examples/airbnb_script_multiclass.py
+2
-2
pytorch_widedeep/metrics.py
pytorch_widedeep/metrics.py
+72
-0
未找到文件。
examples/adult_script.py
浏览文件 @
453220f6
...
...
@@ -4,7 +4,7 @@ import pandas as pd
from
pytorch_widedeep.optim
import
RAdam
from
pytorch_widedeep.models
import
Wide
,
WideDeep
,
DeepDense
from
pytorch_widedeep.metrics
import
BinaryAccuracy
from
pytorch_widedeep.metrics
import
BinaryAccuracy
,
Accuracy
,
Precision
from
pytorch_widedeep.callbacks
import
(
LRHistory
,
EarlyStopping
,
...
...
@@ -76,7 +76,7 @@ if __name__ == "__main__":
EarlyStopping
,
ModelCheckpoint
(
filepath
=
"model_weights/wd_out"
),
]
metrics
=
[
BinaryAccuracy
]
metrics
=
[
Precision
]
model
.
compile
(
method
=
"binary"
,
...
...
examples/airbnb_script_multiclass.py
浏览文件 @
453220f6
...
...
@@ -3,7 +3,7 @@ import torch
import
pandas
as
pd
from
pytorch_widedeep.models
import
Wide
,
WideDeep
,
DeepDense
from
pytorch_widedeep.metrics
import
CategoricalAccuracy
from
pytorch_widedeep.metrics
import
CategoricalAccuracy
,
Accuracy
from
pytorch_widedeep.preprocessing
import
WidePreprocessor
,
DensePreprocessor
use_cuda
=
torch
.
cuda
.
is_available
()
...
...
@@ -48,7 +48,7 @@ if __name__ == "__main__":
continuous_cols
=
continuous_cols
,
)
model
=
WideDeep
(
wide
=
wide
,
deepdense
=
deepdense
,
pred_dim
=
3
)
model
.
compile
(
method
=
"multiclass"
,
metrics
=
[
Categorical
Accuracy
])
model
.
compile
(
method
=
"multiclass"
,
metrics
=
[
Accuracy
])
model
.
fit
(
X_wide
=
X_wide
,
...
...
pytorch_widedeep/metrics.py
浏览文件 @
453220f6
...
...
@@ -46,6 +46,78 @@ class MetricCallback(Callback):
self
.
container
.
reset
()
class
Precision
(
Metric
):
def
__init__
(
self
):
self
.
true_positives
=
0
self
.
all_positives
=
0
self
.
eps
=
1e-20
self
.
_name
=
"prec"
def
reset
(
self
)
->
None
:
self
.
true_positives
=
0
self
.
all_positives
=
0
def
__call__
(
self
,
y_pred
:
Tensor
,
y_true
:
Tensor
)
->
np
.
ndarray
:
num_class
=
y_pred
.
size
(
1
)
if
num_class
==
1
:
y_pred
=
y_pred
.
round
()
y_true
=
y_true
.
view
(
-
1
,
1
)
elif
num_class
>
1
:
y_true
=
torch
.
eye
(
num_class
)[
y_true
.
long
()]
y_pred
=
y_pred
.
topk
(
1
,
1
)[
1
].
view
(
-
1
)
y_pred
=
torch
.
eye
(
num_class
)[
y_pred
.
long
()]
self
.
true_positives
+=
(
y_true
*
y_pred
).
sum
().
item
()
self
.
all_positives
+=
y_pred
.
sum
(
dim
=
0
)
precision
=
(
self
.
true_positives
/
(
self
.
all_positives
+
self
.
eps
)).
mean
().
item
()
return
precision
class
Accuracy
(
Metric
):
r
"""Class to calculate the accuracy for both binary and categorical problems
Parameters
----------
top_k: int
Accuracy will be computed using the top k most likely classes in
multiclass problems
"""
def
__init__
(
self
,
top_k
=
1
):
self
.
top_k
=
top_k
self
.
correct_count
=
0
self
.
total_count
=
0
self
.
_name
=
"acc"
def
reset
(
self
):
"""
resets counters to 0
"""
self
.
correct_count
=
0
self
.
total_count
=
0
def
__call__
(
self
,
y_pred
:
Tensor
,
y_true
:
Tensor
)
->
np
.
ndarray
:
num_classes
=
y_pred
.
size
(
1
)
if
num_classes
==
1
:
y_pred_round
=
y_pred
.
round
()
self
.
correct_count
+=
y_pred_round
.
eq
(
y_true
.
view
(
-
1
,
1
)).
sum
().
item
()
elif
num_classes
>
1
:
top_k
=
y_pred
.
topk
(
self
.
top_k
,
1
)[
1
]
true_k
=
y_true
.
view
(
-
1
,
1
).
expand_as
(
top_k
)
# type: ignore
self
.
correct_count
+=
top_k
.
eq
(
true_k
).
sum
().
item
()
self
.
total_count
+=
len
(
y_pred
)
# type: ignore
accuracy
=
float
(
self
.
correct_count
)
/
float
(
self
.
total_count
)
return
accuracy
class
CategoricalAccuracy
(
Metric
):
r
"""Class to calculate the categorical accuracy for multicategorical problems
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录