Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3525d5d0
提交
3525d5d0
authored
4月 09, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed bug in split grad, added var() to tensor.basic
上级
405feb22
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
72 行增加
和
3 行删除
+72
-3
basic.py
theano/tensor/basic.py
+55
-3
test_basic.py
theano/tensor/tests/test_basic.py
+17
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
3525d5d0
...
...
@@ -4,6 +4,7 @@ __docformat__ = "restructuredtext en"
import
__builtin__
import
sys
# for sys.maxint
import
traceback
#for overriding Op.__call__
import
functools
import
numpy
...
...
@@ -1196,7 +1197,12 @@ pprint.assign(Sum(), printing.FunctionPrinter('sum'))
@constructor
def
mean
(
input
,
axis
=
None
):
"""WRITEME"""
"""Compute the mean value along the given axis of a tensor `input`
:param axis: compute the mean along this axis of the tensor. None means trailing axis.
:type axis: None or int or (list of int) (see `Sum`)
"""
if
str
(
input
.
dtype
)
.
startswith
(
'int'
):
# we need to cast eventually anyway, and this helps
# to prevents overflow
...
...
@@ -1211,6 +1217,43 @@ def mean(input, axis = None):
s
=
s
/
shp
[
i
]
return
s
@constructor
def
var
(
input
,
axis
=
None
):
"""Compute the variance along the given axis of a tensor `input`
:param axis: compute the variance along this axis of the tensor. None means trailing axis.
:type axis: None or int or (list of int) (see `Sum`)
"""
input_ndim
=
input
.
type
.
ndim
if
axis
==
None
:
axis
=
range
(
input_ndim
)
if
isinstance
(
axis
,
int
):
axis
=
[
axis
]
#make a pattern that will undo the reduction of dimensions caused by mean
pattern
=
[]
next_dim
=
0
for
i
in
range
(
input_ndim
):
if
i
in
axis
:
pattern
.
append
(
'x'
)
else
:
pattern
.
append
(
next_dim
)
next_dim
+=
1
#compute the axis-wise mean
mean_input_reduced
=
mean
(
input
,
axis
)
#broadcast that back out to match input
mean_input
=
DimShuffle
(
list
(
mean_input_reduced
.
type
.
broadcastable
),
pattern
)(
mean_input_reduced
)
#center the input
centered_input
=
input
-
mean_input
#return the mean sqr
return
mean
(
centered_input
**
2
,
axis
)
class
Repeat
(
gof
.
Op
):
...
...
@@ -1570,6 +1613,14 @@ class Split(Op):
def
__hash__
(
self
):
return
hash
(
Split
)
^
self
.
len_splits
def
__call__
(
self
,
*
inputs
,
**
kwargs
):
"""Override Op.__call__ to suppress unpacking of output list
"""
node
=
self
.
make_node
(
*
inputs
,
**
kwargs
)
node
.
tag
.
trace
=
traceback
.
extract_stack
()[:
-
1
]
return
node
.
outputs
def
make_node
(
self
,
x
,
axis
,
splits
):
"""WRITEME"""
...
...
@@ -1696,9 +1747,10 @@ class Join(Op):
"""
axis
,
tensors
=
axis_and_tensors
[
0
],
axis_and_tensors
[
1
:]
if
'float'
in
tensors
[
0
]
.
dtype
or
'complex'
in
tensors
[
0
]
.
dtype
:
# assume that this is
n't
differentiable
# assume that this is differentiable
split
=
Split
(
len
(
tensors
))
return
[
None
]
+
split
(
gz
,
axis
,
stack
(
*
[
shape
(
x
)[
axis
]
for
x
in
tensors
]))
split_gz
=
split
(
gz
,
axis
,
stack
(
*
[
shape
(
x
)[
axis
]
for
x
in
tensors
]))
return
[
None
]
+
split_gz
else
:
# assume that this isn't differentiable
return
[
None
]
*
(
1
+
len
(
tensors
))
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
3525d5d0
...
...
@@ -1859,6 +1859,23 @@ def test_reshape_member_fn():
y
=
x
.
reshape
((
4
,
5
,
6
))
assert
y
.
owner
.
op
==
Reshape
(
3
)
def
test_var
():
a
=
Tensor
(
dtype
=
'float64'
,
broadcastable
=
[
False
,
False
,
False
])()
f
=
function
([
a
],
var
(
a
))
a_val
=
numpy
.
arange
(
60
)
.
reshape
(
3
,
4
,
5
)
print
numpy
.
var
(
a_val
)
print
f
(
a_val
)
assert
numpy
.
allclose
(
numpy
.
var
(
a_val
),
f
(
a_val
))
f
=
function
([
a
],
var
(
a
,
axis
=
0
))
assert
numpy
.
allclose
(
numpy
.
var
(
a_val
,
axis
=
0
),
f
(
a_val
))
f
=
function
([
a
],
var
(
a
,
axis
=
1
))
assert
numpy
.
allclose
(
numpy
.
var
(
a_val
,
axis
=
1
),
f
(
a_val
))
f
=
function
([
a
],
var
(
a
,
axis
=
2
))
assert
numpy
.
allclose
(
numpy
.
var
(
a_val
,
axis
=
2
),
f
(
a_val
))
if
__name__
==
'__main__'
:
if
len
(
sys
.
argv
)
>=
2
and
sys
.
argv
[
1
]
==
'OPT'
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论