Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
docs
提交
dd1eddaf
D
docs
项目概览
MindSpore
/
docs
通知
5
Star
3
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
dd1eddaf
编写于
8月 27, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
8月 27, 2020
浏览文件
操作
浏览文件
下载
差异文件
!759 fix code in the ops.md
Merge pull request !759 from lvmingfu/master
上级
ac6f99e2
acc93fed
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
30 addition
and
13 deletion
+30
-13
api/source_zh_cn/programming_guide/nn.md
api/source_zh_cn/programming_guide/nn.md
+1
-1
api/source_zh_cn/programming_guide/ops.md
api/source_zh_cn/programming_guide/ops.md
+29
-12
未找到文件。
api/source_zh_cn/programming_guide/nn.md
浏览文件 @
dd1eddaf
...
...
@@ -17,7 +17,7 @@ net = nn.PSNR()
img1
=
Tensor
(
np
.
random
.
random
((
1
,
3
,
16
,
16
)),
mindspore
.
float32
)
img2
=
Tensor
(
np
.
random
.
random
((
1
,
3
,
16
,
16
)),
mindspore
.
float32
)
output
=
net
(
img1
,
img2
)
print
(
"output =
"
,
output
)
print
(
"output ="
,
output
)
```
输出如下:
...
...
api/source_zh_cn/programming_guide/ops.md
浏览文件 @
dd1eddaf
...
...
@@ -37,7 +37,7 @@ input_x = mindspore.Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
input_y
=
3.0
pow
=
P
.
Pow
()
output
=
pow
(
input_x
,
input_y
)
print
(
"output =
"
,
output
)
print
(
"output ="
,
output
)
```
输出如下:
...
...
@@ -63,7 +63,7 @@ input_x = mindspore.Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
input_y
=
3.0
pow
=
P
.
Pow
()
output
=
pow
(
input_x
,
input_y
)
print
(
"output =
"
,
output
)
print
(
"output ="
,
output
)
```
使用functional的代码样例如下:
...
...
@@ -77,7 +77,7 @@ from mindspore.ops import functional as F
input_x
=
mindspore
.
Tensor
(
np
.
array
([
1.0
,
2.0
,
4.0
]),
mindspore
.
float32
)
input_y
=
3.0
output
=
F
.
tensor_pow
(
input_x
,
input_y
)
print
(
"output =
"
,
output
)
print
(
"output ="
,
output
)
```
输出如下:
...
...
@@ -91,34 +91,51 @@ composite提供了一些算子的组合,包括clip_by_value和random相关的
算子的组合可以直接像一般函数一样使用,例如使用
`normal`
生成一个随机分布:
```
python
from
mindspore.common
import
dtype
as
mstype
from
mindspore.ops
import
composite
as
C
from
mindspore
import
Tensor
mean
=
Tensor
(
1.0
,
mstype
.
float32
)
stddev
=
Tensor
(
1.0
,
mstype
.
float32
)
output
=
C
.
normal
((
4
,
16
),
mean
,
stddev
,
seed
=
5
)
output
=
C
.
normal
((
2
,
3
),
mean
,
stddev
,
seed
=
5
)
print
(
"ouput ="
,
output
)
```
输出如下:
```
output = [[2.4911082 0.7941146 1.3117087]
[0.30582333 1.772938 1.525996]]
```
> 以上代码运行于MindSpore的GPU版本。
针对涉及图变换的函数,用户可以使用
`MultitypeFuncGraph`
定义一组重载的函数,根据不同类型,走到不同实现。
代码样例如下:
```
python
import
numpy
as
np
from
mindspore.ops.composite
import
MultitypeFuncGraph
from
mindspore
import
Tensor
from
mindspore.ops
import
functional
as
F
add
=
MultitypeFuncGraph
(
'add'
)
@
add
.
register
(
"Number"
,
"Number"
)
def
add_scala
(
x
,
y
):
return
scala_add
(
x
,
y
)
def
add_scalar
(
x
,
y
):
return
F
.
scalar_add
(
x
,
y
)
@
add
.
register
(
"Tensor"
,
"Tensor"
)
def
add_tensor
(
x
,
y
):
return
tensor_add
(
x
,
y
)
return
F
.
tensor_add
(
x
,
y
)
tensor1
=
Tensor
(
np
.
array
([[
1.2
,
2.1
],
[
2.2
,
3.2
]]).
astype
(
'float32'
))
tensor2
=
Tensor
(
np
.
array
([[
1.2
,
2.1
],
[
2.2
,
3.2
]]).
astype
(
'float32'
))
print
(
'tensor'
,
mainf
(
tensor1
,
tensor2
))
print
(
'scal
e'
,
mainf
(
1
,
2
))
print
(
'tensor'
,
add
(
tensor1
,
tensor2
))
print
(
'scal
ar'
,
add
(
1
,
2
))
```
输出如下:
```
tensor [[2.4, 4.2], [4.4, 6.4]]
scale 3
tensor [[2.4, 4.2]
[4.4, 6.4]]
scalar 3
```
此外,高阶函数
`GradOperation`
提供了根据输入的函数,求这个函数对应的求梯度的函数的方式,详细可以参阅
[
API文档
](
https://www.mindspore.cn/api/zh-CN/master/api/python/mindspore/mindspore.ops.composite.html#mindspore.ops.composite.GradOperation
)
。
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录