Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
563e4adf
提交
563e4adf
authored
1月 27, 2012
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add dtype keyword to tensor.Prod.
Also add ".prod()" method to tensor variables.
上级
1005832b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
111 行增加
和
29 行删除
+111
-29
basic.py
theano/tensor/basic.py
+11
-3
elemwise.py
theano/tensor/elemwise.py
+100
-26
没有找到文件。
theano/tensor/basic.py
浏览文件 @
563e4adf
...
...
@@ -1422,6 +1422,10 @@ class _tensor_py_operators:
"""See `theano.tensor.sum`"""
return
sum
(
self
,
axis
=
axis
,
dtype
=
dtype
)
def
prod
(
self
,
axis
=
None
,
dtype
=
None
):
"""See `theano.tensor.prod`"""
return
prod
(
self
,
axis
=
axis
,
dtype
=
dtype
)
def
norm
(
self
,
L
,
axis
=
None
):
if
L
==
0
:
raise
NotImplementedError
()
...
...
@@ -2631,9 +2635,13 @@ pprint.assign(Sum(), printing.FunctionPrinter('sum'))
@constructor
def
prod
(
input
,
axis
=
None
):
"""WRITEME"""
return
elemwise
.
Prod
(
axis
)(
input
)
def
prod
(
input
,
axis
=
None
,
dtype
=
None
):
"""
Returns the Product of a tensor's elements along the given axis(es).
For full documentation see ``tensor.elemwise.Prod``.
"""
return
elemwise
.
Prod
(
axis
,
dtype
=
dtype
)(
input
)
class
Mean
(
elemwise
.
CAReduce
):
def
__init__
(
self
,
axis
=
None
):
...
...
theano/tensor/elemwise.py
浏览文件 @
563e4adf
...
...
@@ -1450,9 +1450,9 @@ class Prod(CAReduce):
difference that this defines the gradient of prod wrt its tensor
input.
"""
def
__init__
(
self
,
axis
=
None
,
no_zeros_in_input
=
False
):
def
__init__
(
self
,
axis
=
None
,
dtype
=
None
,
no_zeros_in_input
=
False
):
CAReduce
.
__init__
(
self
,
scalar
.
mul
,
axis
)
self
.
dtype
=
dtype
self
.
no_zeros_in_input
=
no_zeros_in_input
def
__setstate__
(
self
,
dct
):
...
...
@@ -1462,24 +1462,59 @@ class Prod(CAReduce):
self
.
no_zeros_in_input
=
False
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
scalar_op
==
other
.
scalar_op
and
self
.
axis
==
other
.
axis
and
self
.
no_zeros_in_input
==
other
.
no_zeros_in_input
return
(
type
(
self
)
==
type
(
other
)
and
self
.
scalar_op
==
other
.
scalar_op
and
self
.
axis
==
other
.
axis
and
self
.
dtype
==
other
.
dtype
and
self
.
no_zeros_in_input
==
other
.
no_zeros_in_input
)
def
__hash__
(
self
):
if
self
.
axis
is
None
:
return
hash
(
self
.
scalar_op
)
^
hash
(
self
.
no_zeros_in_input
)
else
:
return
hash
(
self
.
scalar_op
)
^
hash
(
tuple
(
self
.
axis
))
^
hash
(
self
.
no_zeros_in_input
)
return
(
CAReduce
.
__hash__
(
self
)
^
hash
(
self
.
no_zeros_in_input
)
^
hash
(
self
.
dtype
))
def
_output_dtype
(
self
,
idtype
):
# we want to protect against overflow
return
dict
(
int8
=
'int64'
,
int16
=
'int64'
,
int32
=
'int64'
,
uint8
=
'uint64'
,
uint16
=
'uint64'
,
uint32
=
'uint64'
,
)
.
get
(
idtype
,
idtype
)
dtype
=
self
.
dtype
if
dtype
is
None
:
# we want to protect against overflow
return
dict
(
int8
=
'int64'
,
int16
=
'int64'
,
int32
=
'int64'
,
uint8
=
'uint64'
,
uint16
=
'uint64'
,
uint32
=
'uint64'
,
)
.
get
(
idtype
,
idtype
)
elif
dtype
in
continuous_dtypes
and
idtype
in
discrete_dtypes
:
# Specifying a continuous output for discrete input is OK
return
dtype
else
:
# The conversion has to be considered an upcast.
upcasted_dtype
=
scalar
.
upcast
(
idtype
,
dtype
)
if
dtype
!=
upcasted_dtype
:
raise
TypeError
(
'Cannot build Prod node with input dtype
%
s '
'and output dtype
%
s, as precision would be lost. '
'To correct this error, you can either:
\n
'
' - not specify a dtype, or
\n
'
' - use a dtype at least as precise as
%
s.
\n
'
'If you are expecting the precision loss, you can '
'use tensor.cast(..., dtype="
%
s"), either on your '
'input, or on the output of the prod.'
%
(
idtype
,
dtype
,
upcasted_dtype
,
dtype
))
return
dtype
def
make_node
(
self
,
input
):
# We need to redefine make_node so that, if self.dtype is None,
# we can infer what dtype should be, and create a node from an Op
# of the appropriate dtype.
dtype
=
self
.
_output_dtype
(
input
.
dtype
)
if
dtype
==
self
.
dtype
:
# Don't build another instance
op
=
self
else
:
op
=
self
.
__class__
(
axis
=
self
.
axis
,
dtype
=
dtype
)
return
CAReduce
.
make_node
(
op
,
input
)
def
grad
(
self
,
inp
,
grads
):
'''
...
...
@@ -1632,19 +1667,58 @@ class MulWithoutZeros(scalar.BinaryScalarOp):
mul_without_zeros
=
MulWithoutZeros
(
scalar
.
upcast_out
,
name
=
'mul_without_zeros'
)
class
ProdWithoutZeros
(
CAReduce
):
def
__init__
(
self
,
axis
=
None
):
def
__init__
(
self
,
axis
=
None
,
dtype
=
None
):
CAReduce
.
__init__
(
self
,
mul_without_zeros
,
axis
)
self
.
dtype
=
dtype
def
__eq__
(
self
,
other
):
return
CAReduce
.
__eq__
(
self
,
other
)
and
self
.
dtype
==
other
.
dtype
def
__hash__
(
self
):
return
CAReduce
.
__hash__
(
self
)
^
hash
(
self
.
dtype
)
def
_output_dtype
(
self
,
idtype
):
# we want to protect against overflow
return
dict
(
int8
=
'int32'
,
int16
=
'int32'
,
int32
=
'int64'
,
uint8
=
'uint32'
,
uint16
=
'uint32'
,
uint32
=
'uint64'
,
)
.
get
(
idtype
,
idtype
)
dtype
=
self
.
dtype
if
dtype
is
None
:
# we want to protect against overflow
return
dict
(
int8
=
'int64'
,
int16
=
'int64'
,
int32
=
'int64'
,
uint8
=
'uint64'
,
uint16
=
'uint64'
,
uint32
=
'uint64'
,
)
.
get
(
idtype
,
idtype
)
elif
dtype
in
continuous_dtypes
and
idtype
in
discrete_dtypes
:
# Specifying a continuous output for discrete input is OK
return
dtype
else
:
# The conversion has to be considered an upcast.
upcasted_dtype
=
scalar
.
upcast
(
idtype
,
dtype
)
if
dtype
!=
upcasted_dtype
:
raise
TypeError
(
'Cannot build ProdWithoutZeros node with input dtype '
'
%
s and output dtype
%
s, as precision would be lost. '
'To correct this error, you can either:
\n
'
' - not specify a dtype, or
\n
'
' - use a dtype at least as precise as
%
s.
\n
'
'If you are expecting the precision loss, you can '
'use tensor.cast(..., dtype="
%
s"), either on your '
'input, or on the output of the prod_without_zeros.'
%
(
idtype
,
dtype
,
upcasted_dtype
,
dtype
))
return
dtype
def
make_node
(
self
,
input
):
# We need to redefine make_node so that, if self.dtype is None,
# we can infer what dtype should be, and create a node from an Op
# of the appropriate dtype.
dtype
=
self
.
_output_dtype
(
input
.
dtype
)
if
dtype
==
self
.
dtype
:
# Don't build another instance
op
=
self
else
:
op
=
self
.
__class__
(
axis
=
self
.
axis
,
dtype
=
dtype
)
return
CAReduce
.
make_node
(
op
,
input
)
def
__str__
(
self
):
if
self
.
axis
is
None
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论