Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
39235a34
提交
39235a34
authored
1月 20, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
1月 20, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add a scalar mean Op
上级
6fe9f839
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
60 行增加
和
2 行删除
+60
-2
basic.py
aesara/scalar/basic.py
+26
-0
math.py
aesara/tensor/math.py
+4
-1
test_basic.py
tests/scalar/test_basic.py
+30
-1
没有找到文件。
aesara/scalar/basic.py
浏览文件 @
39235a34
...
@@ -1857,6 +1857,32 @@ class Add(ScalarOp):
...
@@ -1857,6 +1857,32 @@ class Add(ScalarOp):
add
=
Add
(
upcast_out
,
name
=
"add"
)
add
=
Add
(
upcast_out
,
name
=
"add"
)
class
Mean
(
ScalarOp
):
identity
=
0
commutative
=
True
associative
=
False
nfunc_spec
=
(
"mean"
,
2
,
1
)
nfunc_variadic
=
"mean"
def
impl
(
self
,
*
inputs
):
return
sum
(
inputs
)
/
len
(
inputs
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
z
,)
=
outputs
if
not
inputs
:
return
f
"{z} = 0;"
else
:
return
f
"{z} = ({' + '.join(inputs)}) / ((double) {len(inputs)});"
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
gz
,)
=
gout
retval
=
[
gz
/
len
(
inputs
)]
*
len
(
inputs
)
return
retval
mean
=
Mean
(
float_out
,
name
=
"mean"
)
class
Mul
(
ScalarOp
):
class
Mul
(
ScalarOp
):
identity
=
1
identity
=
1
commutative
=
True
commutative
=
True
...
...
aesara/tensor/math.py
浏览文件 @
39235a34
...
@@ -1474,8 +1474,11 @@ def complex_from_polar(abs, angle):
...
@@ -1474,8 +1474,11 @@ def complex_from_polar(abs, angle):
class
Mean
(
CAReduce
):
class
Mean
(
CAReduce
):
__props__
=
(
"axis"
,)
nfunc_spec
=
(
"mean"
,
1
,
1
)
def
__init__
(
self
,
axis
=
None
):
def
__init__
(
self
,
axis
=
None
):
super
()
.
__init__
(
aes
.
add
,
axis
)
super
()
.
__init__
(
aes
.
mean
,
axis
)
assert
self
.
axis
is
None
or
len
(
self
.
axis
)
==
1
assert
self
.
axis
is
None
or
len
(
self
.
axis
)
==
1
def
__str__
(
self
):
def
__str__
(
self
):
...
...
tests/scalar/test_basic.py
浏览文件 @
39235a34
...
@@ -51,6 +51,7 @@ from aesara.scalar.basic import (
...
@@ -51,6 +51,7 @@ from aesara.scalar.basic import (
log1p
,
log1p
,
log2
,
log2
,
log10
,
log10
,
mean
,
mul
,
mul
,
neq
,
neq
,
rad2deg
,
rad2deg
,
...
@@ -64,7 +65,7 @@ from aesara.scalar.basic import (
...
@@ -64,7 +65,7 @@ from aesara.scalar.basic import (
true_div
,
true_div
,
uint8
,
uint8
,
)
)
from
aesara.tensor.type
import
fscalar
,
imatrix
,
matrix
from
aesara.tensor.type
import
fscalar
,
imatrix
,
iscalar
,
matrix
def
test_mul_add_true
():
def
test_mul_add_true
():
...
@@ -468,3 +469,31 @@ def test_constant():
...
@@ -468,3 +469,31 @@ def test_constant():
c
=
constant
(
2
,
dtype
=
"float32"
)
c
=
constant
(
2
,
dtype
=
"float32"
)
assert
c
.
name
is
None
assert
c
.
name
is
None
assert
c
.
dtype
==
"float32"
assert
c
.
dtype
==
"float32"
@pytest.mark.parametrize
(
"mode"
,
[
Mode
(
"py"
),
Mode
(
"cvm"
)])
def
test_mean
(
mode
):
a
=
iscalar
(
"a"
)
b
=
iscalar
(
"b"
)
z
=
mean
(
a
,
b
)
z_fn
=
aesara
.
function
([
a
,
b
],
z
,
mode
=
mode
)
res
=
z_fn
(
1
,
1
)
assert
np
.
allclose
(
res
,
1.0
)
a
=
fscalar
(
"a"
)
b
=
fscalar
(
"b"
)
c
=
fscalar
(
"c"
)
z
=
mean
(
a
,
b
,
c
)
z_fn
=
aesara
.
function
([
a
,
b
,
c
],
aesara
.
grad
(
z
,
[
a
]),
mode
=
mode
)
res
=
z_fn
(
3
,
4
,
5
)
assert
np
.
allclose
(
res
,
1
/
3
)
z_fn
=
aesara
.
function
([
a
,
b
,
c
],
aesara
.
grad
(
z
,
[
b
]),
mode
=
mode
)
res
=
z_fn
(
3
,
4
,
5
)
assert
np
.
allclose
(
res
,
1
/
3
)
z
=
mean
()
z_fn
=
aesara
.
function
([],
z
,
mode
=
mode
)
assert
z_fn
()
==
0
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论