Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
29e21479
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
29e21479
编写于
8月 21, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
8月 21, 2020
浏览文件
操作
浏览文件
下载
差异文件
!4845 Fix bugs about uncertainty toolbox and vae
Merge pull request !4845 from zhangxinfeng3/master
上级
e485a611
27ff97a5
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
26 addition
and
21 deletion
+26
-21
mindspore/nn/probability/dpn/vae/cvae.py
mindspore/nn/probability/dpn/vae/cvae.py
+5
-2
mindspore/nn/probability/dpn/vae/vae.py
mindspore/nn/probability/dpn/vae/vae.py
+4
-1
mindspore/nn/probability/toolbox/uncertainty_evaluation.py
mindspore/nn/probability/toolbox/uncertainty_evaluation.py
+15
-14
tests/st/probability/test_uncertainty.py
tests/st/probability/test_uncertainty.py
+2
-4
未找到文件。
mindspore/nn/probability/dpn/vae/cvae.py
浏览文件 @
29e21479
...
...
@@ -93,18 +93,21 @@ class ConditionalVAE(Cell):
recon_x
=
self
.
_decode
(
z_c
)
return
recon_x
,
x
,
mu
,
std
def
generate_sample
(
self
,
sample_y
,
generate_nums
=
None
,
shape
=
Non
e
):
def
generate_sample
(
self
,
sample_y
,
generate_nums
,
shap
e
):
"""
Randomly sample from latent space to generate sample.
Args:
sample_y (Tensor): Define the label of sample, int tensor.
generate_nums (int): The number of samples to generate.
shape(tuple): The shape of sample, it should be math:`(generate_nums, C, H, W)`.
shape(tuple): The shape of sample, it should be math:`(generate_nums, C, H, W)`
or math:`(-1, C, H, W)`
.
Returns:
Tensor, the generated sample.
"""
generate_nums
=
check_int_positive
(
generate_nums
)
if
not
isinstance
(
shape
,
tuple
)
or
len
(
shape
)
!=
4
or
shape
[
0
]
!=
generate_nums
or
shape
[
0
]
!=
-
1
:
raise
ValueError
(
'The shape should be (generate_nums, C, H, W) or (-1, C, H, W).'
)
sample_z
=
self
.
normal
((
generate_nums
,
self
.
latent_size
),
self
.
to_tensor
(
0.0
),
self
.
to_tensor
(
1.0
),
seed
=
0
)
sample_y
=
self
.
one_hot
(
sample_y
)
sample_c
=
self
.
concat
((
sample_z
,
sample_y
))
...
...
mindspore/nn/probability/dpn/vae/vae.py
浏览文件 @
29e21479
...
...
@@ -88,11 +88,14 @@ class VAE(Cell):
Args:
generate_nums (int): The number of samples to generate.
shape(tuple): The shape of sample, it should be math:`(generate_nums, C, H, W)`.
shape(tuple): The shape of sample, it should be math:`(generate_nums, C, H, W)`
or math:`(-1, C, H, W)`
.
Returns:
Tensor, the generated sample.
"""
generate_nums
=
check_int_positive
(
generate_nums
)
if
not
isinstance
(
shape
,
tuple
)
or
len
(
shape
)
!=
4
or
shape
[
0
]
!=
generate_nums
or
shape
[
0
]
!=
-
1
:
raise
ValueError
(
'The shape should be (generate_nums, C, H, W) or (-1, C, H, W).'
)
sample_z
=
self
.
normal
((
generate_nums
,
self
.
latent_size
),
self
.
to_tensor
(
0.0
),
self
.
to_tensor
(
1.0
),
seed
=
0
)
sample
=
self
.
_decode
(
sample_z
)
sample
=
self
.
reshape
(
sample
,
shape
)
...
...
mindspore/nn/probability/toolbox/uncertainty_evaluation.py
浏览文件 @
29e21479
...
...
@@ -13,18 +13,20 @@
# limitations under the License.
# ============================================================================
"""Toolbox for Uncertainty Evaluation."""
import
numpy
as
np
from
copy
import
deepcopy
import
numpy
as
np
from
mindspore._checkparam
import
check_int_positive
,
check_bool
from
mindspore.ops
import
composite
as
C
from
mindspore.ops
import
operations
as
P
from
mindspore.train
import
Model
from
mindspore.train.callback
import
LossMonitor
,
ModelCheckpoint
,
CheckpointConfig
from
mindspore.train.serialization
import
load_checkpoint
,
load_param_into_net
from
...cell
import
Cell
from
...layer.basic
import
Dense
,
Flatten
,
Dropout
from
...layer.conv
import
Conv2d
from
...layer.container
import
SequentialCell
from
...layer.conv
import
Conv2d
from
...loss
import
SoftmaxCrossEntropyWithLogits
,
MSELoss
from
...metrics
import
Accuracy
,
MSE
from
...optim
import
Adam
...
...
@@ -36,8 +38,7 @@ class UncertaintyEvaluation:
Args:
model (Cell): The model for uncertainty evaluation.
epi_train_dataset (Dataset): A dataset iterator to train model for obtain epistemic uncertainty.
ale_train_dataset (Dataset): A dataset iterator to train model for obtain aleatoric uncertainty.
train_dataset (Dataset): A dataset iterator to train model.
task_type (str): Option for the task types of model
- regression: A regression model.
- classification: A classification model.
...
...
@@ -45,22 +46,20 @@ class UncertaintyEvaluation:
If the task type is classification, it must be set; if not classification, it need not to be set.
Default: None.
epochs (int): Total number of iterations on the data. Default: 1.
epi_uncer_model_path (str): The save or read path of the epistemic uncertainty model.
ale_uncer_model_path (str): The save or read path of the aleatoric uncertainty model.
epi_uncer_model_path (str): The save or read path of the epistemic uncertainty model.
Default: None.
ale_uncer_model_path (str): The save or read path of the aleatoric uncertainty model.
Default: None.
save_model (bool): Save the uncertainty model or not, if True, the epi_uncer_model_path
and ale_uncer_model_path should not be None. If False, give the path of
the uncertainty model, it will load the model to evaluate, if not given
the path, it will not save or load the uncertainty model.
the path, it will not save or load the uncertainty model.
Default: False.
Examples:
>>> network = LeNet()
>>> param_dict = load_checkpoint('checkpoint_lenet.ckpt')
>>> load_param_into_net(network, param_dict)
>>> epi_ds_train = create_dataset('workspace/mnist/train')
>>> ale_ds_train = create_dataset('workspace/mnist/train')
>>> ds_train = create_dataset('workspace/mnist/train')
>>> evaluation = UncertaintyEvaluation(model=network,
>>> epi_train_dataset=epi_ds_train,
>>> ale_train_dataset=ale_ds_train,
>>> train_dataset=ds_train,
>>> task_type='classification',
>>> num_classes=10,
>>> epochs=1,
...
...
@@ -71,12 +70,12 @@ class UncertaintyEvaluation:
>>> aleatoric_uncertainty = evaluation.eval_aleatoric_uncertainty(eval_data)
"""
def
__init__
(
self
,
model
,
epi_train_dataset
,
ale_
train_dataset
,
task_type
,
num_classes
=
None
,
epochs
=
1
,
def
__init__
(
self
,
model
,
train_dataset
,
task_type
,
num_classes
=
None
,
epochs
=
1
,
epi_uncer_model_path
=
None
,
ale_uncer_model_path
=
None
,
save_model
=
False
):
self
.
epi_model
=
model
self
.
ale_model
=
model
self
.
epi_train_dataset
=
epi_
train_dataset
self
.
ale_train_dataset
=
ale_train_dataset
self
.
epi_train_dataset
=
train_dataset
self
.
ale_train_dataset
=
deepcopy
(
train_dataset
)
self
.
task_type
=
task_type
self
.
epochs
=
check_int_positive
(
epochs
)
self
.
epi_uncer_model_path
=
epi_uncer_model_path
...
...
@@ -93,6 +92,8 @@ class UncertaintyEvaluation:
raise
ValueError
(
'The task should be regression or classification.'
)
if
task_type
==
'classification'
:
self
.
num_classes
=
check_int_positive
(
num_classes
)
else
:
self
.
num_classes
=
num_classes
if
save_model
:
if
epi_uncer_model_path
is
None
or
ale_uncer_model_path
is
None
:
raise
ValueError
(
"If save_model is True, the epi_uncer_model_path and "
...
...
tests/st/probability/test_uncertainty.py
浏览文件 @
29e21479
...
...
@@ -119,12 +119,10 @@ if __name__ == '__main__':
param_dict
=
load_checkpoint
(
'checkpoint_lenet.ckpt'
)
load_param_into_net
(
network
,
param_dict
)
# get train and eval dataset
epi_ds_train
=
create_dataset
(
'workspace/mnist/train'
)
ale_ds_train
=
create_dataset
(
'workspace/mnist/train'
)
ds_train
=
create_dataset
(
'workspace/mnist/train'
)
ds_eval
=
create_dataset
(
'workspace/mnist/test'
)
evaluation
=
UncertaintyEvaluation
(
model
=
network
,
epi_train_dataset
=
epi_ds_train
,
ale_train_dataset
=
ale_ds_train
,
train_dataset
=
ds_train
,
task_type
=
'classification'
,
num_classes
=
10
,
epochs
=
1
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录