Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
9708e582
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看板
提交
9708e582
编写于
6月 11, 2020
作者:
K
kingfo
提交者:
kpy
6月 15, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix TupleToArray & Cast operator issue
上级
1a82132d
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
126 addition
and
20 deletion
+126
-20
mindspore/common/parameter.py
mindspore/common/parameter.py
+5
-14
mindspore/nn/cell.py
mindspore/nn/cell.py
+6
-2
mindspore/ops/operations/array_ops.py
mindspore/ops/operations/array_ops.py
+7
-2
tests/st/pynative/test_ops.py
tests/st/pynative/test_ops.py
+31
-0
tests/ut/python/nn/optim/test_optimizer.py
tests/ut/python/nn/optim/test_optimizer.py
+2
-2
tests/ut/python/pynative_mode/nn/test_parameter_operation.py
tests/ut/python/pynative_mode/nn/test_parameter_operation.py
+67
-0
tests/ut/python/pynative_mode/test_parse_method.py
tests/ut/python/pynative_mode/test_parse_method.py
+8
-0
未找到文件。
mindspore/common/parameter.py
浏览文件 @
9708e582
...
...
@@ -15,7 +15,7 @@
"""Parameter for cell."""
import
numbers
from
copy
import
copy
,
deepcopy
from
copy
import
copy
from
mindspore
import
context
from
.
import
dtype
as
mstype
from
.initializer
import
initializer
,
Initializer
...
...
@@ -191,25 +191,16 @@ class Parameter:
return
self
.
default_input
def
__add__
(
self
,
other
):
res
=
deepcopy
(
self
)
res
.
default_input
=
res
.
default_input
+
other
return
res
return
self
.
default_input
+
other
def
__sub__
(
self
,
other
):
res
=
deepcopy
(
self
)
res
.
default_input
=
res
.
default_input
-
other
return
res
return
self
.
default_input
-
other
def
__mul__
(
self
,
other
):
res
=
deepcopy
(
self
)
default_input
=
res
.
default_input
*
other
res
.
default_input
=
Tensor
(
default_input
.
asnumpy
().
copy
())
return
res
return
self
.
default_input
*
other
def
__truediv__
(
self
,
other
):
res
=
deepcopy
(
self
)
res
.
default_input
=
res
.
default_input
/
other
return
res
return
self
.
default_input
/
other
def
__setitem__
(
self
,
index
,
value
):
return
self
...
...
mindspore/nn/cell.py
浏览文件 @
9708e582
...
...
@@ -202,6 +202,7 @@ class Cell:
if
context
.
get_context
(
"mode"
)
==
context
.
GRAPH_MODE
:
out
=
self
.
compile_and_run
(
*
inputs
)
return
out
self
.
init_parameters_data
()
orign_grad
=
[]
if
self
.
requires_grad
is
True
:
_pynative_exec
.
set_grad_flag
(
True
)
...
...
@@ -254,9 +255,12 @@ class Cell:
value
.
update_parameters_name
(
name
+
'.'
)
cells
[
name
]
=
value
elif
params
and
name
in
params
:
if
value
is
not
None
:
if
isinstance
(
value
,
Tensor
)
and
self
.
_params
[
name
]
is
not
None
:
self
.
_params
[
name
].
set_parameter_data
(
value
)
elif
value
is
not
None
:
raise
TypeError
(
"Expected type in (Parameter, ParameterTuple), but got {}."
.
format
(
type
(
value
)))
self
.
insert_param_to_cell
(
name
,
None
)
else
:
self
.
insert_param_to_cell
(
name
,
None
)
elif
cells
and
name
in
cells
:
if
value
is
not
None
:
raise
TypeError
(
"Expected type is cell, but got {}."
.
format
(
type
(
value
)))
...
...
mindspore/ops/operations/array_ops.py
浏览文件 @
9708e582
...
...
@@ -30,7 +30,7 @@ from ...common import dtype as mstype
from
...common.tensor
import
Tensor
from
..operations.math_ops
import
_infer_shape_reduce
from
.._utils
import
get_concat_offset
from
..primitive
import
Primitive
,
PrimitiveWithInfer
,
prim_attr_register
from
..primitive
import
Primitive
,
PrimitiveWithInfer
,
prim_attr_register
,
_run_op
from
..._c_expression
import
signature_rw
as
sig_rw
from
..._c_expression
import
signature_kind
as
sig_kind
from
..._c_expression
import
signature_dtype
as
sig_dtype
...
...
@@ -990,9 +990,14 @@ class TupleToArray(PrimitiveWithInfer):
ret
=
np
.
array
(
x
,
np
.
int32
)
else
:
ret
=
np
.
array
(
x
,
np
.
float32
)
return
Tensor
(
ret
)
def
__call__
(
self
,
x
):
args
=
list
()
if
isinstance
(
x
,
range
):
args
.
append
(
tuple
(
x
))
return
_run_op
(
self
,
self
.
name
,
args
)
class
ScalarToArray
(
PrimitiveWithInfer
):
"""
...
...
tests/st/pynative/test_ops.py
0 → 100644
浏览文件 @
9708e582
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import
numpy
as
np
import
mindspore
as
ms
import
mindspore.ops.operations
as
P
from
mindspore
import
context
,
Tensor
def
test_cast
():
""" tests cast for same dtype"""
context
.
set_context
(
mode
=
context
.
PYNATIVE_MODE
,
device_target
=
"Ascend"
)
input_np
=
np
.
random
.
randn
(
2
,
3
,
4
,
5
).
astype
(
np
.
float32
)
input_x
=
Tensor
(
input_np
)
type_dst
=
ms
.
float32
cast
=
P
.
Cast
()
result
=
cast
(
input_x
,
type_dst
)
assert
result
.
dtype
()
==
type_dst
tests/ut/python/nn/optim/test_optimizer.py
浏览文件 @
9708e582
...
...
@@ -52,11 +52,11 @@ class TestAdam():
use_nesterov
=
False
,
weight_decay
=
0.0
,
loss_scale
=
1.0
)
def
test_construct
(
self
):
with
pytest
.
raises
(
Typ
eError
):
with
pytest
.
raises
(
Runtim
eError
):
gradient
=
Tensor
(
np
.
zeros
([
1
,
2
,
3
]))
adam
=
Adam
(
params
,
learning_rate
=
1e-3
,
beta1
=
0.9
,
beta2
=
0.999
,
eps
=
1e-8
,
use_locking
=
False
,
use_nesterov
=
False
,
weight_decay
=
0.0
,
loss_scale
=
1.0
)
adam
.
construct
(
gradient
)
adam
(
gradient
)
class
TestSGD
():
...
...
tests/ut/python/pynative_mode/nn/test_parameter_operation.py
0 → 100644
浏览文件 @
9708e582
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
""" test_tensor_operation """
import
numpy
as
np
import
mindspore.nn
as
nn
from
mindspore
import
Tensor
,
Parameter
from
mindspore
import
context
def
setup_module
(
module
):
context
.
set_context
(
mode
=
context
.
PYNATIVE_MODE
)
def
test_parameter_add
():
x
=
Parameter
(
Tensor
(
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)),
name
=
"ref"
)
y
=
Tensor
(
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
))
expect
=
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)
*
2
z
=
x
+
y
assert
np
.
allclose
(
z
.
asnumpy
(),
expect
)
def
test_parameter_sub
():
x
=
Parameter
(
Tensor
(
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)
*
2
),
name
=
"ref"
)
y
=
Tensor
(
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
))
expect
=
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)
z
=
x
-
y
assert
np
.
allclose
(
z
.
asnumpy
(),
expect
)
def
test_parameter_mul
():
x
=
Parameter
(
Tensor
(
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)
*
2
),
name
=
"ref"
)
y
=
Tensor
(
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)
*
2
)
expect
=
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)
*
4
z
=
x
*
y
assert
np
.
allclose
(
z
.
asnumpy
(),
expect
)
def
test_parameter_div
():
x
=
Parameter
(
Tensor
(
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)
*
8
),
name
=
"ref"
)
y
=
Tensor
(
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)
*
2
)
expect
=
np
.
ones
((
3
,
3
)).
astype
(
np
.
float32
)
*
4
z
=
x
/
y
assert
np
.
allclose
(
z
.
asnumpy
(),
expect
)
class
ParameterNet
(
nn
.
Cell
):
def
__init__
(
self
):
super
(
ParameterNet
,
self
).
__init__
()
self
.
weight
=
Parameter
(
Tensor
(
np
.
array
([[
1.0
,
2.0
,
3.0
],
[
4.0
,
5.0
,
6.0
]],
np
.
float32
)),
name
=
"ref"
)
def
construct
(
self
,
x
):
self
.
weight
=
x
def
test_parameter_assign
():
"""test parameter assign with tensor"""
input_x
=
Tensor
(
np
.
array
([[
1.0
,
2.0
,
3.0
],
[
4.0
,
5.0
,
8.0
]],
np
.
float32
))
net
=
ParameterNet
()
net
(
input_x
)
assert
np
.
allclose
(
net
.
weight
.
data
.
asnumpy
(),
input_x
.
asnumpy
())
tests/ut/python/pynative_mode/test_parse_method.py
浏览文件 @
9708e582
...
...
@@ -31,6 +31,7 @@ from mindspore.common.api import ms_function
from
mindspore.common.tensor
import
Tensor
from
mindspore.ops.composite
import
core
from
mindspore.ops.primitive
import
constexpr
from
mindspore.ops
import
functional
as
F
from
..ut_filter
import
non_graph_engine
...
...
@@ -427,3 +428,10 @@ def test_expr():
def
tuple_len
(
x
):
assert
len
(
x
)
==
2
tuple_len
(
a
)
def
test_tuple_to_array
():
""" test range tuple to array """
range_x
=
range
(
10
)
res
=
F
.
tuple_to_array
(
range_x
)
print
(
res
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录