Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ae7a178e
提交
ae7a178e
authored
6月 01, 2012
作者:
Eric Larsen
提交者:
Frederic
6月 08, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove duplicate old file.
上级
180891e8
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
0 行增加
和
219 行删除
+0
-219
bartlett_ver1.py
extra_ops.py/bartlett_ver1.py
+0
-90
fill_diagonalcopy_ver1.py
extra_ops.py/fill_diagonalcopy_ver1.py
+0
-129
没有找到文件。
extra_ops.py/bartlett_ver1.py
deleted
100644 → 0
浏览文件 @
180891e8
import
numpy
from
theano
import
gof
,
tensor
,
function
from
theano.tests
import
unittest_tools
as
utt
class
Bartlett
(
gof
.
Op
):
"""
An instance of this class returns the Bartlett spectral window in the
time-domain. The Bartlett window is very similar to a triangular window,
except that the end points are at zero. It is often used in signal
processing for tapering a signal, without generating too much ripple in
the frequency domain.
input : (integer scalar) Number of points in the output window. If zero or
less, an empty vector is returned.
output : (vector of doubles) The triangular window, with the maximum value
normalized to one (the value one appears only if the number of samples is
odd), with the first and last samples equal to zero.
"""
def
__init__
(
self
):
gof
.
Op
.
__init__
(
self
)
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
def
__str__
(
self
):
return
self
.
__class__
.
__name__
def
make_node
(
self
,
M
):
M
=
tensor
.
as_tensor_variable
(
M
)
if
M
.
ndim
!=
0
:
raise
TypeError
(
'
%
s only works on scalar input'
%
self
.
__class__
.
__name__
)
elif
not
M
.
dtype
.
startswith
(
'int'
):
# dtype is a theano attribute here
raise
TypeError
(
'
%
s only works on integer input'
%
self
.
__class__
.
__name__
)
return
gof
.
Apply
(
self
,
[
M
],
[
tensor
.
dvector
()])
def
perform
(
self
,
node
,
inputs
,
out_
):
M
=
inputs
[
0
]
if
not
M
.
dtype
.
name
.
startswith
(
'int'
):
# dtype is an instance of numpy dtype class here
raise
TypeError
(
'
%
s only works on integers'
%
self
.
__class__
.
__name__
)
out
,
=
out_
out
[
0
]
=
numpy
.
bartlett
(
M
)
def
infer_shape
(
self
,
node
,
in_shapes
):
M
=
node
.
inputs
[
0
]
return
[[
M
]]
def
grad
(
self
,
inputs
,
output_grads
):
return
[
None
for
i
in
inputs
]
def
bartlett
(
M
):
localop
=
Bartlett
()
return
localop
(
M
)
class
TestBartlett
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
super
(
TestBartlett
,
self
)
.
setUp
()
self
.
op_class
=
Bartlett
self
.
op
=
bartlett
def
test_perform
(
self
):
x
=
tensor
.
lscalar
()
f
=
function
([
x
],
self
.
op
(
x
))
M
=
numpy
.
random
.
random_integers
(
3
,
50
,
size
=
())
assert
numpy
.
allclose
(
f
(
M
),
numpy
.
bartlett
(
M
))
def
test_infer_shape
(
self
):
x
=
tensor
.
lscalar
()
self
.
_compile_and_check
([
x
],
[
self
.
op
(
x
)],
[
numpy
.
random
.
random_integers
(
3
,
50
,
size
=
())],
self
.
op_class
)
if
__name__
==
"__main__"
:
t
=
TestBartlett
(
'setUp'
)
t
.
setUp
()
t
.
test_perform
()
t
.
test_infer_shape
()
extra_ops.py/fill_diagonalcopy_ver1.py
deleted
100644 → 0
浏览文件 @
180891e8
import
numpy
from
theano
import
tensor
,
gof
,
function
,
scalar
from
theano.sandbox.linalg.ops
import
diag
from
theano.tests
import
unittest_tools
as
utt
class
FillDiagonalCopy
(
gof
.
Op
):
"""
An instance of this class returns a copy of an array with all elements of
the main diagonal set to a specified scalar value.
inputs:
a : Rectangular array of at least two dimensions.
val : Scalar value to fill the diagonal whose type must be compatible with
that of array 'a' (i.e. 'val' must not be an upcasting of 'a').
output:
An array identical to 'a' except that its main diagonal is filled with
scalar 'val'. (For an array 'a' with a.ndim >= 2, the main diagonal is the
list of locations a[i, i, ..., i] (i.e. with indices all identical).)
"""
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
def
__hash_
(
self
):
return
hash
(
type
(
self
))
def
__str__
(
self
):
return
self
.
__class__
.
__name__
def
infer_shape
(
self
,
node
,
in_shapes
):
return
[
in_shapes
[
0
]]
def
make_node
(
self
,
a
,
val
):
a
=
tensor
.
as_tensor_variable
(
a
)
val
=
tensor
.
as_tensor_variable
(
val
)
if
a
.
ndim
<
2
:
raise
TypeError
(
'
%
s: first parameter must have at least'
' two dimensions'
%
self
.
__class__
.
__name__
)
elif
val
.
ndim
!=
0
:
raise
TypeError
(
'
%
s: second parameter must be a scalar'
%
self
.
__class__
.
__name__
)
val
=
tensor
.
cast
(
val
,
dtype
=
scalar
.
upcast
(
a
.
dtype
,
val
.
dtype
))
if
val
.
dtype
!=
a
.
dtype
:
raise
TypeError
(
'
%
s: type of second parameter must be compatible'
' with first
\'
s'
%
self
.
__class__
.
__name__
)
return
gof
.
Apply
(
self
,
[
a
,
val
],
[
a
.
type
()])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
if
inputs
[
0
]
.
ndim
<
2
:
raise
TypeError
(
'
%
s: first parameter must have at least'
' two dimensions'
%
self
.
__class__
.
__name__
)
elif
inputs
[
1
]
.
ndim
!=
0
:
raise
TypeError
(
'
%
s: second parameter must be a scalar'
%
self
.
__class__
.
__name__
)
a
=
inputs
[
0
]
.
copy
()
val
=
inputs
[
1
]
numpy
.
fill_diagonal
(
a
,
val
)
output_storage
[
0
][
0
]
=
a
def
grad
(
self
,
inp
,
cost_grad
):
"""
Note: The gradient is currently implemented for matrices
only.
"""
a
,
val
=
inp
grad
=
cost_grad
[
0
]
if
(
a
.
dtype
==
'complex64'
or
a
.
dtype
==
'complex128'
or
val
.
dtype
==
'complex64'
or
val
.
dtype
==
'complex128'
):
return
[
None
,
None
]
elif
a
.
ndim
>
2
:
raise
NotImplementedError
(
'
%
s: gradient is currently implemented'
' for matrices only'
%
self
.
__class__
.
__name__
)
wr_a
=
grad
.
copy
()
wr_a
=
fill_diagonal
(
wr_a
,
0
)
# valid for any number of dimensions
wr_val
=
diag
(
grad
)
.
sum
()
# diag is only valid for matrices
return
[
wr_a
,
wr_val
]
def
fill_diagonal
(
in_a
,
in_val
):
localop
=
FillDiagonalCopy
()
return
localop
(
in_a
,
in_val
)
class
TestFillDiagonalCopy
(
utt
.
InferShapeTester
):
rng
=
numpy
.
random
.
RandomState
(
43
)
def
setUp
(
self
):
super
(
TestFillDiagonalCopy
,
self
)
.
setUp
()
self
.
op_class
=
FillDiagonalCopy
self
.
op
=
fill_diagonal
def
test_perform
(
self
):
x
=
tensor
.
dmatrix
()
y
=
tensor
.
dscalar
()
f
=
function
([
x
,
y
],
fill_diagonal
(
x
,
y
))
g
=
function
([
x
],
diag
(
x
))
a
=
numpy
.
random
.
rand
(
8
,
5
)
val
=
numpy
.
random
.
rand
()
out
=
f
(
a
,
val
)
numpy
.
fill_diagonal
(
a
,
val
)
# remember that numpy.fill_diagonal works in place
assert
numpy
.
allclose
(
out
,
a
)
def
test_gradient
(
self
):
# TODO: check why gradient wrto val does not match when a has more rows
# than cols: might be problem with testing procedure
utt
.
verify_grad
(
fill_diagonal
,
[
numpy
.
random
.
rand
(
5
,
8
),
numpy
.
random
.
rand
()],
n_tests
=
1
,
rng
=
TestFillDiagonalCopy
.
rng
)
def
test_infer_shape
(
self
):
x
=
tensor
.
dmatrix
()
y
=
tensor
.
dscalar
()
self
.
_compile_and_check
([
x
,
y
],
[
self
.
op
(
x
,
y
)],
[
numpy
.
random
.
rand
(
8
,
5
),
numpy
.
random
.
rand
()],
self
.
op_class
)
if
__name__
==
"__main__"
:
t
=
TestFillDiagonalCopy
(
'setUp'
)
t
.
setUp
()
t
.
test_perform
()
t
.
test_gradient
()
t
.
test_infer_shape
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论