Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
250d45dd
提交
250d45dd
authored
4月 08, 2008
作者:
olivier@olivier-desktop
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
more cleanup in tensor.py
上级
14ab328e
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
4 行增加
和
81 行删除
+4
-81
_test_tensor.py
_test_tensor.py
+1
-5
base_tensor.py
base_tensor.py
+1
-1
tensor.py
tensor.py
+2
-75
没有找到文件。
_test_tensor.py
浏览文件 @
250d45dd
...
@@ -398,13 +398,9 @@ class T_abs(unittest.TestCase):
...
@@ -398,13 +398,9 @@ class T_abs(unittest.TestCase):
verify_grad
(
self
,
Abs
,
[
numpy
.
ones
(())])
verify_grad
(
self
,
Abs
,
[
numpy
.
ones
(())])
verify_grad
(
self
,
Abs
,
[
numpy
.
ones
(
3
)])
verify_grad
(
self
,
Abs
,
[
numpy
.
ones
(
3
)])
class
AbsBadGrad
(
tensor
.
_Elemwise
):
class
AbsBadGrad
(
Abs
):
def
impl
(
self
,
x
):
return
numpy
.
abs
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
mul
(
gz
*
sgn
(
x
),
0.9
),
return
mul
(
gz
*
sgn
(
x
),
0.9
),
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"z_i = abs(x_i);"
def
test_badgrad
(
self
):
def
test_badgrad
(
self
):
try
:
try
:
...
...
base_tensor.py
浏览文件 @
250d45dd
"""A simple class to store ndarray data """
"""A simple class to store ndarray data """
from
gof
import
ResultBase
,
Op
,
utils
from
gof
import
ResultBase
,
Op
,
utils
,
AbstractFunctionError
import
numpy
import
numpy
from
copy
import
copy
from
copy
import
copy
...
...
tensor.py
浏览文件 @
250d45dd
...
@@ -9,7 +9,6 @@ import gof.result
...
@@ -9,7 +9,6 @@ import gof.result
import
gof.op
import
gof.op
from
base_tensor
import
BaseTensor
,
BaseTensorOp
from
base_tensor
import
BaseTensor
,
BaseTensorOp
from
elemwise
import
Elemwise
import
blas
# for gemm, dot
import
blas
# for gemm, dot
import
elemwise2
as
s2t
import
elemwise2
as
s2t
...
@@ -159,78 +158,6 @@ class _Binary:
...
@@ -159,78 +158,6 @@ class _Binary:
nin
=
2
nin
=
2
class
_Elemwise
(
Elemwise
,
_Op
):
@staticmethod
def
extract_name
(
name
):
if
name
.
endswith
(
"_i"
):
return
name
[:
-
2
]
else
:
return
name
@staticmethod
def
is_loop_var
(
name
):
return
name
.
endswith
(
"_i"
)
def
var_desc
(
self
):
cls
=
self
.
__class__
(
self
,
inames
,
onames
),
_1
,
_2
,
_3
=
inspect
.
getargspec
(
cls
.
c_foreach
)
return
([(
cls
.
extract_name
(
name
),
cls
.
is_loop_var
(
name
))
for
name
in
inames
],
[(
cls
.
extract_name
(
name
),
cls
.
is_loop_var
(
name
))
for
name
in
onames
])
def
propagate_broadcastable
(
self
,
*
inputs
):
idesc
,
odesc
=
self
.
var_desc
()
nonloop_o
=
[
o
[
0
]
for
o
in
odesc
if
not
o
[
1
]]
if
nonloop_o
:
raise
Exception
(
"Cannot infer broadcastable for non-loop variable(s)
%
s"
%
nonloop_o
)
all_bcast
=
[
broadcastable
for
broadcastable
,
i
in
zip
(
inputs
,
idesc
)
if
i
[
1
]]
if
reduce
(
lambda
x
,
y
:
x
is
not
False
and
x
==
y
and
y
,
[
len
(
x
)
for
x
in
all_bcast
])
is
False
:
raise
TypeError
(
_Elemwise
.
propagate_broadcastable
.
E_ndim
,
self
.
__class__
)
ret
=
[]
for
arr
in
zip
(
*
all_bcast
):
if
0
in
arr
:
ret
.
append
(
0
)
else
:
ret
.
append
(
1
)
return
[
ret
]
*
self
.
nout
propagate_broadcastable
.
E_ndim
\
=
"Inputs that are loop variables do not all have the same number of dimensions."
def
c_init
(
self
,
inputs
,
outputs
):
raise
AbstractFunctionError
()
def
c_foreach
(
self
,
inputs
,
outputs
):
raise
AbstractFunctionError
()
def
c_finalize
(
self
,
inputs
,
outputs
):
raise
AbstractFunctionError
()
def
c_code_init
(
self
):
return
self
.
c_init
(
self
.
inputs
,
self
.
outputs
)
def
c_code_foreach
(
self
):
return
self
.
c_foreach
(
self
.
inputs
,
self
.
outputs
)
def
c_code_finalize
(
self
):
return
self
.
c_finalize
(
self
.
inputs
,
self
.
outputs
)
class
TensorScalarOp
(
_Elemwise
):
def
var_desc
(
self
):
return
[(
'x'
,
1
),
(
'a'
,
0
)],
[(
'z'
,
1
)]
def
c_code_init
(
self
):
return
"""
dtype_
%(a)
s _
%(a)
s;
if (PyArray_SIZE(
%(a)
s) != 1) {
PyErr_SetString(PyExc_ValueError,
\"
The size of the scalar argument is not 1.
\"
);
%(fail)
s
}
_
%(a)
s = ((dtype_
%(a)
s*)PyArray_DATA(
%(a)
s))[0];
"""
def
c_code_foreach
(
self
):
return
"
%%(z)
s_i =
%
s;"
%
self
.
c_expr
##########################
##########################
# Unary Operations
# Unary Operations
##########################
##########################
...
@@ -314,14 +241,14 @@ class TransposeInplace(_Op, Viewer):
...
@@ -314,14 +241,14 @@ class TransposeInplace(_Op, Viewer):
def
grad
(
self
,
x
,
gz
):
def
grad
(
self
,
x
,
gz
):
return
transpose
(
gz
)
return
transpose
(
gz
)
def
c_
impl
(
self
,
x
,
z
):
def
c_
code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"""
return
"""
PyArrayObject* transposed = (PyArrayObject*)PyArray_Transpose(
%(x)
s, NULL);
PyArrayObject* transposed = (PyArrayObject*)PyArray_Transpose(
%(x)
s, NULL);
if (
%(z)
s) {
if (
%(z)
s) {
Py_XDECREF(
%(z)
s);
Py_XDECREF(
%(z)
s);
}
}
%(z)
s = transposed;
%(z)
s = transposed;
"""
"""
%
locals
()
transpose_inplace
=
gof
.
op
.
constructor
(
TransposeInplace
)
transpose_inplace
=
gof
.
op
.
constructor
(
TransposeInplace
)
def
transpose
(
x
,
**
kwargs
):
def
transpose
(
x
,
**
kwargs
):
return
transpose_inplace
(
tensor_copy
(
x
),
**
kwargs
)
return
transpose_inplace
(
tensor_copy
(
x
),
**
kwargs
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论