Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
62cdbab2
提交
62cdbab2
authored
4月 29, 2008
作者:
Joseph Turian
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Moved row_random_transformation to joseph-sandbox
上级
b6e2459d
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
0 行增加
和
143 行删除
+0
-143
_test_sparse.py
_test_sparse.py
+0
-54
sparse.py
sparse.py
+0
-89
没有找到文件。
_test_sparse.py
浏览文件 @
62cdbab2
...
@@ -299,59 +299,5 @@ class _testCase_dot(unittest.TestCase):
...
@@ -299,59 +299,5 @@ class _testCase_dot(unittest.TestCase):
self
.
failUnless
(
origloss
>
loss
)
self
.
failUnless
(
origloss
>
loss
)
class
T_RowRandomTransformation
(
unittest
.
TestCase
):
def
setUp
(
self
):
random
.
seed
(
44
)
numpy
.
random
.
seed
(
44
)
def
test_length
(
self
):
""" Test that if length is increased, we obtain the same results
(except longer). """
for
i
in
range
(
10
):
mtype
=
random
.
choice
(
_mtypes
)
rows
=
random
.
randint
(
1
,
20
)
cols
=
random
.
randint
(
1
,
20
)
fakeseed
=
random
.
randint
(
0
,
100
)
length
=
random
.
randint
(
1
,
10
)
extralength
=
random
.
randint
(
1
,
10
)
m
=
assparse
(
mtype
(
numpy
.
random
.
rand
(
rows
,
cols
)))
o1
=
row_random_transformation
(
m
,
length
,
initial_seed
=
fakeseed
)
o2
=
row_random_transformation
(
m
,
length
+
extralength
,
initial_seed
=
fakeseed
)
y1
=
compile
.
eval_outputs
([
o1
])
y2
=
compile
.
eval_outputs
([
o2
])
self
.
failUnless
((
y1
==
y2
[:,:
length
])
.
all
())
def
test_permute
(
self
):
""" Test that if the order of the rows is permuted, we obtain the same results. """
for
i
in
range
(
10
):
mtype
=
random
.
choice
(
_mtypes
)
rows
=
random
.
randint
(
2
,
20
)
cols
=
random
.
randint
(
1
,
20
)
fakeseed
=
random
.
randint
(
0
,
100
)
length
=
random
.
randint
(
1
,
10
)
permute
=
numpy
.
random
.
permutation
(
rows
)
m1
=
numpy
.
random
.
rand
(
rows
,
cols
)
m2
=
m1
[
permute
]
for
r
in
range
(
rows
):
self
.
failUnless
((
m2
[
r
]
==
m1
[
permute
[
r
]])
.
all
())
s1
=
assparse
(
mtype
(
m1
))
s2
=
assparse
(
mtype
(
m2
))
o1
=
row_random_transformation
(
s1
,
length
,
initial_seed
=
fakeseed
)
o2
=
row_random_transformation
(
s2
,
length
,
initial_seed
=
fakeseed
)
y1
=
compile
.
eval_outputs
([
o1
])
y2
=
compile
.
eval_outputs
([
o2
])
self
.
failUnless
(
y1
.
shape
==
y2
.
shape
)
for
r
in
range
(
rows
):
self
.
failUnless
((
y2
[
r
]
==
y1
[
permute
[
r
]])
.
all
())
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
sparse.py
浏览文件 @
62cdbab2
...
@@ -321,92 +321,3 @@ def dot(x, y, grad_preserves_dense=True):
...
@@ -321,92 +321,3 @@ def dot(x, y, grad_preserves_dense=True):
assert
y_is_sparse_result
assert
y_is_sparse_result
return
transpose
(
Dot
(
y
.
T
,
x
.
T
,
grad_preserves_dense
)
.
outputs
[
0
])
return
transpose
(
Dot
(
y
.
T
,
x
.
T
,
grad_preserves_dense
)
.
outputs
[
0
])
class
RowRandomTransformation
(
gof
.
op
.
Op
):
"""
Given C{x}, a (sparse) matrix with shape (exmpls, dimensions), we
multiply it by a deterministic random matrix of shape (dimensions,
length) to obtain random transformation output of shape (exmpls,
length).
Each element of the deterministic random matrix is selected uniformly
from [-1, +1).
@todo: Use another random distribution?
@note: This function should be written such that if length is
increased, we obtain the same results (except longer). Similarly,
the rows should be able to be permuted and get the same result.
@todo: This may be slow?
@todo: Rewrite for dense matrices too?
@todo: Is there any way to verify the convention that each row is
an example? Should I rename the variables in the code to make the
semantics more explicit?
@todo: AUTOTEST: This function should be written such that if length
is increased, we obtain the same results (except longer). Similarly,
the rows should be able to be permuted and get the same result. Also,
autotest that dense and spare versions of this are identical.
@todo: Rename? Is Row the correct name? Maybe column-wise?
@type x: L{scipy.sparse.spmatrix}
@param x: Sparse matrix to be randomly transformed with shape (exmpls, dimensions)
@type length: int
@param length: The number of transformations of C{x} to be performed.
@param initial_seed: Initial seed for the RNG.
@rtype: L{numpy.ndarray}
@return: Array with C{length} random transformations, with shape (exmpls, length)
"""
import
random
"""
RNG used for random transformations.
Does not share state with rest of program.
@todo: Make STATIC and private. Ask James or Olivier how to make this more Pythonic.
"""
_trng
=
random
.
Random
()
def
__init__
(
self
,
x
,
length
,
initial_seed
=
0
,
**
kwargs
):
"""
@todo: Which broadcastable values should I use?
"""
gof
.
op
.
Op
.
__init__
(
self
,
**
kwargs
)
x
=
assparse
(
x
)
self
.
initial_seed
=
initial_seed
self
.
length
=
length
self
.
inputs
=
[
x
]
self
.
outputs
=
[
tensor
.
Tensor
(
x
.
dtype
,
broadcastable
=
[
False
,
False
])]
# self.outputs = [tensor.Tensor(x.dtype, broadcastable=[True, True])]
def
impl
(
self
,
x
):
assert
_is_sparse
(
x
)
assert
len
(
x
.
shape
)
==
2
(
rows
,
cols
)
=
x
.
shape
tot
=
rows
*
cols
out
=
numpy
.
zeros
((
rows
,
self
.
length
))
for
l
in
range
(
self
.
length
):
for
i
in
range
(
x
.
getnnz
()):
(
r
,
c
)
=
x
.
rowcol
(
i
)
assert
c
<
cols
assert
r
<
rows
# Choose the random entry at (l, c)
rngidx
=
l
*
cols
+
c
# Set the random number state for this random entry
# Note: This may be slow
self
.
_trng
.
seed
(
rngidx
+
self
.
initial_seed
)
# Determine the value for this entry
val
=
self
.
_trng
.
uniform
(
-
1
,
+
1
)
# print "Exmpl #%d, dimension #%d => Random projection #%d has idx %d (+ seed %d) and value %f" % (r, c, j, rngidx, self.initial_seed, val)
out
[
r
][
l
]
+=
val
*
x
.
getdata
(
i
)
return
out
def
grad
(
self
,
(
x
,
y
),
(
gz
,)):
raise
NotImplementedError
def
__copy__
(
self
):
return
self
.
__class__
(
self
.
inputs
[
0
],
self
.
length
,
self
.
initial_seed
)
def
clone_with_new_inputs
(
self
,
*
new_inputs
):
return
self
.
__class__
(
new_inputs
[
0
],
self
.
length
,
self
.
initial_seed
)
def
desc
(
self
,
*
new_inputs
):
return
(
self
.
__class__
,
self
.
length
,
self
.
initial_seed
)
row_random_transformation
=
gof
.
op
.
constructor
(
RowRandomTransformation
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论