Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a68aa7e2
提交
a68aa7e2
authored
3月 21, 2008
作者:
turian@grenat.iro.umontreal.ca
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added more comments + documentation
上级
0c987405
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
27 行增加
和
11 行删除
+27
-11
_test_tensor.py
_test_tensor.py
+22
-10
base_tensor.py
base_tensor.py
+5
-1
没有找到文件。
_test_tensor.py
浏览文件 @
a68aa7e2
...
@@ -457,38 +457,50 @@ class T_pow(unittest.TestCase):
...
@@ -457,38 +457,50 @@ class T_pow(unittest.TestCase):
def
test_scalar_r
(
self
):
def
test_scalar_r
(
self
):
verify_grad
(
self
,
PowScalarR
,
[
numpy
.
random
.
rand
(
3
),
numpy
.
asarray
(
3.0
)])
verify_grad
(
self
,
PowScalarR
,
[
numpy
.
random
.
rand
(
3
),
numpy
.
asarray
(
3.0
)])
class
_testCase_matinv
:
#
(unittest.TestCase):
class
_testCase_matinv
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
numpy
.
random
.
seed
(
1
)
numpy
.
random
.
seed
(
1
)
def
mat_recip
(
self
,
dim
):
def
mat_recip
rocal
(
self
,
dim
):
# symbolic program
# symbolic program
a
=
Tensor
(
'float64'
,
[
0
,
0
],
name
=
'a'
)
# broadcastable=[False,False] means that the shape of matrix is two dimensional,
b
=
Tensor
(
'float64'
,
[
0
,
0
],
name
=
'b'
)
# and none of the dimensions are constrained to have length 1.
# Note that Tensor's constructor does not actually allocate any memory.
# TODO: Make Tensor syntax more explicit, and maybe give shape or number of dimensions.
a
=
Tensor
(
'float64'
,
broadcastable
=
[
False
,
False
],
name
=
'a'
)
b
=
Tensor
(
'float64'
,
broadcastable
=
[
False
,
False
],
name
=
'b'
)
ab
=
a
*
b
ab
=
a
*
b
# Here, tinit actually uses the data allocated by numpy.
# TODO: Rename tinit to tensor
diff
=
ab
-
tinit
(
numpy
.
ones
((
dim
,
dim
)))
diff
=
ab
-
tinit
(
numpy
.
ones
((
dim
,
dim
)))
# Sum of squared errors
ssdiff
=
sum
((
diff
**
2.0
))
ssdiff
=
sum
((
diff
**
2.0
))
# May be able to abbreviate this by assuming default parameter
# TODO: Test that default works
g_b
=
gradient
.
grad
(
ssdiff
,
b
,
tinit
(
numpy
.
ones
(
1
),
name
=
'g_cost'
))
g_b
=
gradient
.
grad
(
ssdiff
,
b
,
tinit
(
numpy
.
ones
(
1
),
name
=
'g_cost'
))
#g_b = gradient.grad(ssdiff, b) # This should be the abbreviated version
# compilation to function
# compilation to function
# [a,b] are the inputs, [ssdiff,g_b] are the outputs
fn
=
Function
([
a
,
b
],
[
ssdiff
,
g_b
])
fn
=
Function
([
a
,
b
],
[
ssdiff
,
g_b
])
# use the function
# use the function
x
=
numpy
.
random
.
rand
(
dim
,
dim
)
+
0.1
# Initialized s.t. x is not too tiny
w
=
numpy
.
random
.
rand
(
dim
,
dim
)
w
=
numpy
.
random
.
rand
(
dim
,
dim
)
wi
=
numpy
.
random
.
rand
(
dim
,
dim
)
for
i
in
xrange
(
300
):
for
i
in
xrange
(
300
):
ssd
,
gw
=
fn
(
w
,
wi
)
ssd
,
gw
=
fn
(
x
,
w
)
#print ssd
#print ssd
, x*w, x, w
if
i
==
0
:
if
i
==
0
:
str0
=
str
(
ssd
)
str0
=
str
(
ssd
)
w
i
-=
0.4
*
gw
w
-=
0.4
*
gw
return
str0
,
str
(
ssd
)
return
str0
,
str
(
ssd
)
def
test_recip
(
self
):
def
test_recip
rocal
(
self
):
"""Matrix reciprocal by gradient descent"""
"""Matrix reciprocal by gradient descent"""
self
.
assertEqual
((
'
2.67327580893'
,
'0.000438649434819'
),
self
.
mat_recip
(
3
))
self
.
assertEqual
((
'
6.10141615619'
,
'0.00703816291711'
),
self
.
mat_reciprocal
(
3
))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
base_tensor.py
浏览文件 @
a68aa7e2
...
@@ -26,7 +26,11 @@ class BaseTensor(ResultBase):
...
@@ -26,7 +26,11 @@ class BaseTensor(ResultBase):
"""
"""
def
__init__
(
self
,
dtype
,
broadcastable
,
role
=
None
,
name
=
None
):
def
__init__
(
self
,
dtype
,
broadcastable
,
role
=
None
,
name
=
None
):
"""Initialize a Tensor"""
"""Initialize a Tensor
Note:
This does not actually allocate any data.
"""
# data is not given here. This may seem a bit strange, but when data was
# data is not given here. This may seem a bit strange, but when data was
# an argument, it made sense to use *either* the given dtype,
# an argument, it made sense to use *either* the given dtype,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论