Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c7db795f
提交
c7db795f
authored
4月 10, 2008
作者:
bergstrj@iro.umontreal.ca
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
moved scalar_ops to scalar, removed old elemwise.py.
上级
f1153866
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
194 行增加
和
166 行删除
+194
-166
_test_elemwise2.py
_test_elemwise2.py
+1
-1
_test_gradient.py
_test_gradient.py
+1
-1
_test_scalar.py
_test_scalar.py
+1
-1
elemwise.py
elemwise.py
+0
-0
elemwise2.py
elemwise2.py
+4
-4
op.py
gof/op.py
+2
-0
gradient.py
gradient.py
+3
-5
scalar.py
scalar.py
+180
-0
scalar_ops.py
scalar_ops.py
+0
-152
tensor.py
tensor.py
+2
-2
没有找到文件。
_test_elemwise2.py
浏览文件 @
c7db795f
...
...
@@ -5,7 +5,7 @@ import unittest
from
gof
import
ResultBase
,
Op
,
Env
,
modes
import
gof
from
scalar
_ops
import
*
from
scalar
import
*
from
elemwise2
import
*
...
...
_test_gradient.py
浏览文件 @
c7db795f
...
...
@@ -22,7 +22,7 @@ class _test_grad_sources_inputs(unittest.TestCase):
try
:
grad_sources_inputs
([(
a
.
out
,
1
)],
None
)
except
ValueError
,
e
:
self
.
failUnless
(
e
[
0
]
is
gradient
.
_msg_ret
Non
e
)
self
.
failUnless
(
e
[
0
]
is
gradient
.
_msg_ret
Typ
e
)
return
self
.
fail
()
def
test_retNone1_b
(
self
):
...
...
_test_scalar
_ops
.py
→
_test_scalar.py
浏览文件 @
c7db795f
...
...
@@ -4,7 +4,7 @@ import unittest
from
gof
import
ResultBase
,
Op
,
Env
,
modes
import
gof
from
scalar
_ops
import
*
from
scalar
import
*
def
inputs
():
...
...
elemwise.py
deleted
100644 → 0
浏览文件 @
f1153866
差异被折叠。
点击展开。
elemwise2.py
浏览文件 @
c7db795f
...
...
@@ -4,8 +4,8 @@ import elemwise_cgen as cgen
import
numpy
from
gof
import
Op
,
Viewer
,
Destroyer
from
base_tensor
import
BaseTensor
as
Tensor
import
scalar
from
scalar
import
upcast
,
Scalar
import
scalar_ops
import
gof
from
gof.python25
import
all
...
...
@@ -424,7 +424,7 @@ class CAReduce(Op):
tosum
=
self
.
dimensions_to_reduce
if
tosum
==
():
return
Broadcast
(
scalar
_ops
.
Identity
,
(
input
,
))
.
_c_all
(
inames
,
onames
,
sub
)
return
Broadcast
(
scalar
.
Identity
,
(
input
,
))
.
_c_all
(
inames
,
onames
,
sub
)
order1
=
[
i
for
i
in
xrange
(
len
(
input
.
broadcastable
))
if
i
not
in
tosum
]
order
=
order1
+
list
(
tosum
)
...
...
@@ -522,7 +522,7 @@ def make_reduce(scalar_opclass, name = None):
New
.
__name__
=
"Reduce"
+
scalar_opclass
.
__name__
return
New
class
Sum
(
make_reduce
(
scalar
_ops
.
Add
)):
class
Sum
(
make_reduce
(
scalar
.
Add
)):
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
if
self
.
dimensions_to_reduce
==
():
return
gz
,
...
...
@@ -534,7 +534,7 @@ class Sum(make_reduce(scalar_ops.Add)):
else
:
new_dims
.
append
(
i
)
i
+=
1
return
Broadcast
(
scalar
_ops
.
Second
,
(
x
,
DimShuffle
(
gz
,
new_dims
)
.
out
))
.
out
,
return
Broadcast
(
scalar
.
Second
,
(
x
,
DimShuffle
(
gz
,
new_dims
)
.
out
))
.
out
,
def
reduce
(
op
):
...
...
gof/op.py
浏览文件 @
c7db795f
...
...
@@ -58,6 +58,8 @@ class Op(object):
#
# Python stdlib compatibility
#
# These are defined so that sets of Ops, Results will have a consistent
# ordering
def
__cmp__
(
self
,
other
):
return
cmp
(
id
(
self
),
id
(
other
))
...
...
gradient.py
浏览文件 @
c7db795f
...
...
@@ -3,7 +3,7 @@ import numpy #for numeric_grad
from
gof.python25
import
all
_msg_ret
None
=
'op.grad(...) returned None, consider returning [None]
'
_msg_ret
Type
=
'op.grad(...) returned a non-list
'
_msg_badlen
=
'op.grad(...) returned wrong number of gradients'
def
_unpack_result
(
lst
):
...
...
@@ -82,10 +82,8 @@ def grad_sources_inputs(sources, graph_inputs):
output_arg
=
g_outputs
input_arg
=
op
.
inputs
op_grad
=
op
.
grad
(
input_arg
,
output_arg
)
if
op_grad
is
None
:
raise
ValueError
(
_msg_retNone
,
op
.
__class__
)
if
isinstance
(
op_grad
,
float
):
raise
TypeError
(
'wtf!!!!!!!!'
,
op
)
if
not
isinstance
(
op_grad
,
(
list
,
tuple
)):
raise
ValueError
(
_msg_retType
,
op
.
__class__
)
g_inputs
=
op_grad
#_pack_result(op_grad)
assert
isinstance
(
g_inputs
,
(
list
,
tuple
))
if
len
(
g_inputs
)
!=
len
(
op
.
inputs
):
...
...
scalar.py
浏览文件 @
c7db795f
import
numpy
import
math
from
copy
import
copy
import
inspect
...
...
@@ -143,6 +144,35 @@ class Scalar(ResultBase):
cpy
.
data
=
self
.
data
return
cpy
#UNARY
def
__abs__
(
self
):
return
Abs
(
self
)
.
out
def
__neg__
(
self
):
return
Neg
(
self
)
.
out
#CASTS
def
__int__
(
self
):
return
AsInt
(
self
)
.
out
def
__float__
(
self
):
return
AsInt
(
self
)
.
out
def
__complex__
(
self
):
return
AsComplex
(
self
)
.
out
#COMPARISONS
def
__lt__
(
self
,
other
):
return
lt
(
self
,
other
)
def
__le__
(
self
,
other
):
return
le
(
self
,
other
)
def
__gt__
(
self
,
other
):
return
gt
(
self
,
other
)
def
__ge__
(
self
,
other
):
return
ge
(
self
,
other
)
#ARITHMETIC - NORMAL
def
__add__
(
self
,
other
):
return
add
(
self
,
other
)
def
__sub__
(
self
,
other
):
return
sub
(
self
,
other
)
def
__mul__
(
self
,
other
):
return
mul
(
self
,
other
)
def
__div__
(
self
,
other
):
return
div
(
self
,
other
)
def
__pow__
(
self
,
other
):
return
pow
(
self
,
other
)
#ARITHMETIC - RIGHT-OPERAND
def
__radd__
(
self
,
other
):
return
add
(
other
,
self
)
def
__rsub__
(
self
,
other
):
return
sub
(
other
,
self
)
def
__rmul__
(
self
,
other
):
return
mul
(
other
,
self
)
def
__rdiv__
(
self
,
other
):
return
div
(
other
,
self
)
def
__rpow__
(
self
,
other
):
return
pow
(
other
,
self
)
class
ScalarMixedOp
(
GuardedOp
):
...
...
@@ -201,4 +231,154 @@ class BinaryScalarOp(PureScalarOp):
class
Add
(
BinaryScalarOp
):
identity
=
0
def
impl
(
self
,
x
,
y
):
return
x
+
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s +
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
gz
,
gz
class
Sub
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
-
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s -
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
gz
,
neg
(
gz
)
class
Mul
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
*
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s *
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
mul
(
y
,
gz
),
mul
(
x
,
gz
)
class
Div
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
/
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s /
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
div
(
gz
,
y
),
neg
(
div
(
mul
(
x
,
gz
),
mul
(
y
,
y
)))
class
Pow
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
**
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s = pow(
%(x)
s,
%(y)
s);"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
mul
(
gz
,
mul
(
y
,
pow
(
x
,
sub
(
y
,
as_scalar
(
1
))))),
mul
(
gz
,
mul
(
log
(
x
),
pow
(
x
,
y
)))
class
First
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
gz
,
None
class
Second
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
None
,
gz
class
Identity
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
x
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
gz
,
class
Neg
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
-
x
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
neg
(
gz
),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = -
%(x)
s;"
%
locals
()
class
Abs
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
numpy
.
abs
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
mul
(
gz
,
sgn
(
x
)),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = abs(
%(x)
s);"
%
locals
()
class
Sgn
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
numpy
.
abs
(
x
)
/
x
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
None
,
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s/abs(
%(x)
s);"
%
locals
()
# TODO: C use copysign
class
Inv
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
1
/
x
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
div
(
neg
(
gz
),
mul
(
x
,
x
)),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = 1 /
%(x)
s;"
%
locals
()
class
Log
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
math
.
log
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
div
(
gz
,
x
),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = log(
%(x)
s);"
%
locals
()
class
Log2
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
numpy
.
log2
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
div
(
gz
,
mul
(
x
,
as_scalar
(
math
.
log
(
2.0
)))),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = log2(
%(x)
s);"
%
locals
()
class
Exp
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
math
.
exp
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
mul
(
gz
,
exp
(
x
)),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = exp(
%(x)
s);"
%
locals
()
class
Sqr
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
x
*
x
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
mul
(
gz
,
mul
(
x
,
as_scalar
(
2
))),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s *
%(x)
s;"
%
locals
()
class
Sqrt
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
math
.
sqrt
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
div
(
mul
(
gz
,
as_scalar
(
0.5
)),
sqrt
(
x
)),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = sqrt(
%(x)
s);"
%
locals
()
#NOTE WELL!!!
# The following adds functions to this module automatically.
# For every scalar op class, a lower-case symbol is added which is a constructor
# for that class.
from
gof
import
modes
modes
.
make_constructors
(
globals
())
scalar_ops.py
deleted
100644 → 0
浏览文件 @
f1153866
from
scalar
import
*
import
math
class
Add
(
BinaryScalarOp
):
identity
=
0
def
impl
(
self
,
x
,
y
):
return
x
+
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s +
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
gz
,
gz
class
Sub
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
-
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s -
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
gz
,
neg
(
gz
)
class
Mul
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
*
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s *
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
mul
(
y
,
gz
),
mul
(
x
,
gz
)
class
Div
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
/
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s /
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
div
(
gz
,
y
),
neg
(
div
(
mul
(
x
,
gz
),
mul
(
y
,
y
)))
class
Pow
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
**
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s = pow(
%(x)
s,
%(y)
s);"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
mul
(
gz
,
mul
(
y
,
pow
(
x
,
sub
(
y
,
as_scalar
(
1
))))),
mul
(
gz
,
mul
(
log
(
x
),
pow
(
x
,
y
)))
class
First
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
x
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
gz
,
None
class
Second
(
BinaryScalarOp
):
def
impl
(
self
,
x
,
y
):
return
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
None
,
gz
class
Identity
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
x
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
gz
,
class
Neg
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
-
x
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
neg
(
gz
),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = -
%(x)
s;"
%
locals
()
class
Abs
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
numpy
.
abs
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
mul
(
gz
,
sgn
(
x
)),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = abs(
%(x)
s);"
%
locals
()
class
Sgn
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
numpy
.
abs
(
x
)
/
x
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
None
,
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s/abs(
%(x)
s);"
%
locals
()
# TODO: C use copysign
class
Inv
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
1
/
x
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
div
(
neg
(
gz
),
mul
(
x
,
x
)),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = 1 /
%(x)
s;"
%
locals
()
class
Log
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
math
.
log
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
div
(
gz
,
x
),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = log(
%(x)
s);"
%
locals
()
class
Log2
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
numpy
.
log2
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
div
(
gz
,
mul
(
x
,
as_scalar
(
math
.
log
(
2.0
)))),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = log2(
%(x)
s);"
%
locals
()
class
Exp
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
math
.
exp
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
mul
(
gz
,
exp
(
x
)),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = exp(
%(x)
s);"
%
locals
()
class
Sqr
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
x
*
x
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
mul
(
gz
,
mul
(
x
,
as_scalar
(
2
))),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s *
%(x)
s;"
%
locals
()
class
Sqrt
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
return
math
.
sqrt
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
div
(
mul
(
gz
,
as_scalar
(
0.5
)),
sqrt
(
x
)),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = sqrt(
%(x)
s);"
%
locals
()
# class Sigmoid(UnaryComposite):
# def expand_impl(self, x):
# return 1.0 / (1.0 + exp(-x))
from
gof
import
modes
modes
.
make_constructors
(
globals
())
tensor.py
浏览文件 @
c7db795f
...
...
@@ -12,7 +12,7 @@ from base_tensor import BaseTensor, BaseTensorOp
import
blas
# for gemm, dot
import
elemwise2
as
s2t
import
scalar
_ops
as
scal
import
scalar
as
scal
class
Tensor
(
BaseTensor
):
...
...
@@ -69,7 +69,7 @@ class Tensor(BaseTensor):
def
__getslice__
(
self
,
*
args
):
return
subtensor
(
self
,
slice
(
*
args
))
s2t
.
Tensor
=
Tensor
# alternate Tensor constructor
def
astensor
(
data
,
broadcastable
=
None
,
role
=
None
,
name
=
None
):
"""Return a Tensor containing given data"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论