Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d68c9e07
提交
d68c9e07
authored
1月 17, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
clever commit message
上级
928ad5c0
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
85 行增加
和
284 行删除
+85
-284
compile.py
compile.py
+2
-1
core.py
core.py
+66
-23
grad.py
grad.py
+17
-3
test.py
test.py
+0
-257
没有找到文件。
compile.py
浏览文件 @
d68c9e07
...
...
@@ -2,6 +2,7 @@
import
gof
import
opt
from
copy
import
copy
#prog(inputs, outputs)
#single(*outputs)
...
...
@@ -52,8 +53,8 @@ class prog(gof.Prog):
def
to_func
(
inputs
,
outputs
):
# print gof.Env(inputs, outputs).io_toposort()
## p = prog([copy(input) for input in inputs], gof.graph.clone(inputs, outputs))
p
=
prog
(
inputs
,
outputs
)
print
p
.
env
def
f
(
*
args
):
for
input
,
value
in
zip
(
inputs
,
args
):
p
[
input
]
=
value
...
...
core.py
浏览文件 @
d68c9e07
...
...
@@ -3,6 +3,7 @@ import gof
from
gof
import
current_mode
,
set_mode
,
build_mode
,
eval_mode
,
build_eval_mode
,
pop_mode
,
UNCOMPUTED
,
UNDEFINED
,
PythonR
import
numpy
import
weakref
from
copy
import
copy
as
pycopy
...
...
@@ -51,6 +52,9 @@ def print_graph(*rs):
print
as_string
(
*
rs
)
literals_db
=
{}
literals_id_db
=
weakref
.
WeakValueDictionary
()
def
input
(
x
):
if
isinstance
(
x
,
numpy
.
ndarray
):
return
NumpyR
(
x
)
...
...
@@ -72,37 +76,60 @@ def wrap(x):
return
wrap
(
x
.
_obj
)
else
:
return
literal
(
x
)
def
_hashable
(
x
):
try
:
x
in
{}
return
True
except
TypeError
:
# x is unhashable
return
False
def
_literal_hashable
(
x
):
# try:
# present = x in literals_db
# hashable = True
# except TypeError: # x is unhashable
# present = False
# hashable = False
if
x
in
literals_db
:
return
literals_db
[
x
]
else
:
r
=
input
(
x
)
r
.
constant
=
True
literals_db
[
x
]
=
r
return
r
# elif isinstance(x, numpy.ndarray):
# ret
urn NumpyR(x
)
# ret
= NumpyR(x, constant = True
)
# elif isinstance(x, (int, float)):
# return NumpyR(numpy.array(x))
# ret = NumpyR(numpy.array(x), constant = True)
# elif isinstance(x, gof.Result):
# raise TypeError("%s is already a result." % x)
# else:
# return PythonR(x)
# return PythonR(x
, constant = True
)
def
literal
(
x
):
try
:
present
=
x
in
gof
.
literals_db
hashable
=
True
except
TypeError
:
# x is unhashable
present
=
False
hashable
=
False
# if hashable:
# literals_db[x] = ret
if
present
:
return
gof
.
literals_db
.
get
(
x
)
elif
isinstance
(
x
,
numpy
.
ndarray
):
ret
=
NumpyR
(
x
,
constant
=
True
)
elif
isinstance
(
x
,
(
int
,
float
)):
ret
=
NumpyR
(
numpy
.
array
(
x
),
constant
=
True
)
elif
isinstance
(
x
,
gof
.
Result
):
raise
TypeError
(
"
%
s is already a result."
%
x
)
# return ret
def
_literal_unhashable
(
x
):
idx
=
id
(
x
)
if
idx
in
literals_id_db
:
return
literals_id_db
[
idx
]
else
:
return
PythonR
(
x
,
constant
=
True
)
r
=
input
(
x
)
r
.
constant
=
True
literals_id_db
[
idx
]
=
r
return
r
if
hashable
:
gof
.
literals_db
[
x
]
=
ret
return
ret
def
literal
(
x
):
if
_hashable
(
x
):
return
_literal_hashable
(
x
)
else
:
return
_literal_unhashable
(
x
)
inplace
=
gof
.
Destroyer
...
...
@@ -249,6 +276,22 @@ class proto_add_scalar(omega_op):
class
add_scalar
(
proto_add_scalar
):
impl
=
tensor_scalar_op
(
numpy
.
ndarray
.
__add__
)
# def c_impl(x, s, z):
# """
# if (*__z == NULL) {
# *__z = new ndarray
# }
# ndarray& z = **__z
# """
# return """
# z.resize_like(x);
# for (int i = 0; i < z.size(); i++) {
# z[i] = x[i] * s;
# }
# return z;
# """
class
iadd_scalar
(
proto_add_scalar
,
inplace
):
impl
=
tensor_scalar_op
(
numpy
.
ndarray
.
__iadd__
)
...
...
grad.py
浏览文件 @
d68c9e07
...
...
@@ -23,15 +23,15 @@ class Grad(object):
self
.
add_output
(
key
,
val
)
def
__contains__
(
self
,
item
):
return
i
d
(
item
)
in
self
.
map
return
i
tem
in
self
.
map
def
__getitem__
(
self
,
item
):
"""Map item to its id and retrieve it."""
return
self
.
map
[
id
(
item
)]
return
self
.
map
[
core
.
wrap
(
item
)]
def
__setitem__
(
self
,
item
,
val
):
"""Map item to its id and store internally."""
self
.
map
[
i
d
(
item
)
]
=
val
self
.
map
[
i
tem
]
=
val
def
add_output
(
self
,
r
,
dr
):
self
.
add
(
r
,
dr
)
...
...
@@ -134,6 +134,7 @@ def grad(cost, param=None, cost_grad = 1.0):
else
:
return
rval
(
param
)
#
# UNIT TEST
#
...
...
@@ -141,6 +142,7 @@ import unittest
import
numpy
import
compile
class
_testCase
(
unittest
.
TestCase
):
def
setUp
(
self
):
numpy
.
random
.
seed
(
1
)
...
...
@@ -194,6 +196,18 @@ class _testCase (unittest.TestCase):
self
.
assertEqual
((
'2.67327580893'
,
'0.000438649434819'
),
self
.
matinv_compiled
(
3
))
def
test_grad_wrt_ndarray_pointer
(
self
):
"""
Tests if it is possible to index the gradient by a pointer to a ndarray
that is used as a node of the computation graph.
"""
a
=
numpy
.
ones
((
4
,
4
))
b
=
numpy
.
ones
((
4
,
4
))
c
=
numpy
.
ones
((
4
,
4
))
expr
=
core
.
sum
(
core
.
dot
(
core
.
add
(
a
,
b
),
c
))
g
=
grad
(
expr
)
g
[
a
]
def
tearDown
(
self
):
core
.
pop_mode
()
...
...
test.py
deleted
100644 → 0
浏览文件 @
928ad5c0
# import gof
# gof.stealth.method_wrap(int, '__add__', [2, 1], )
# x = gof.stealth.wrap(3)
# y = gof.stealth.wrap(4)
# print x + y
import
gof
import
core
import
numpy
import
compile
import
grad
# a = core.NumpyR(numpy.ones((3, 3)))
# b = core.NumpyR(numpy.ones((3, 3)))
# w = core.dot #core.wrapper(numpy.dot)
# core.start_build()
# r = a * (b * b)
# core.end_build()
# #r = w(a, w(b, b))
# print r
# print r.owner
# env = gof.Env([a, b], [r._obj])
# print env
# print r
# gof.ThunkLinker()(env)()
# print r
# core.start_build()
# a += b + c
# a = a + b
# a += a + core.transpose(b)
# core.end_build()
# # env = gof.Env(gof.graph.inputs([a]), [a])
# # print env
# # gof.ThunkLinker()(env)()
# # print a
# print gof.Env(gof.graph.inputs([a]), [a])
# prog = compile.single(a)
# print prog.env
# prog()
# print a
############################
# #core.build_mode()
# dim = core.wrap(())
# dim2 = core.wrap((2, 2))
# a = core.zeros(dim, dtype='int32') #(core.NumpyR(numpy.ones((3, 3))))
# b = core.ones(dim2, 'int32') #(core.NumpyR(numpy.ones((3, 3))))
# c = core.zeros(dim, dtype='int32')
# d = a + (b + b) + c + numpy.ones(())
# e = d + (b * c)
# #core.pop_mode()
# print e
# #print e
# #print gof.graph.ops([dim], [e])
# #1/0
# #print gof.Env([dim], [e])
# #f = compile.to_func([dim], [e])
# # f = compile.to_func([a, b, c], [e])
# # print f(1, 2, 3)
# # #print f((2,2))
############################
# a = core.ones((2, 2))
# b = core.ones((2, 2))
# def f():
# return (a + b) + (a + b)
# r = core.build(f)
# env = gof.Env([a, b], [r])
# print env
# gof.opt.MergeOptimizer().optimize(env)
# print env
# print compile.to_func([a, b], [r])(1, 2)
############################
# a = core.ones((2, 2))
# b = core.ones((2, 2))
# def f():
# return (a + b) + (a + b)
# r = core.build(f)
# g = grad.grad(r, a)
# core.print_graph(g)
# print [id(input) for input in g.owner.inputs]
# print gof.literals_db
# core.print_graph(r)
############################
def
dataset_1hot
(
x
,
targ
,
n
):
"""Return an looping iterator over 1-hot vectors
This function is a generator for the integers range(n) that works by
side-effect on the numpy ndarray mat.
On each iteration, mat is set (in-place) to the next element of an infinite
sequence of 1-hot vectors.
"""
assert
targ
.
size
==
1
for
i
in
xrange
(
n
):
idx
=
i
%
x
.
shape
[
1
]
x
[:]
=
0
x
[
0
,
idx
]
=
1
targ
[
0
]
=
idx
yield
i
class
sigmoid
(
core
.
omega_op
):
def
impl
(
x
):
return
1.0
/
(
1.0
+
numpy
.
exp
(
-
x
))
def
grad
(
x
,
gz
):
return
gz
*
sigmoid
(
x
)
*
(
1
-
sigmoid
(
x
))
numpy
.
random
.
seed
(
1
)
core
.
build_eval_mode
()
x
=
core
.
zeros
((
1
,
10
))
w
=
core
.
input
(
numpy
.
random
.
rand
(
10
,
15
))
core
.
pop_mode
()
# x = numpy.zeros((1, 10))
# w = numpy.random.rand(10, 15)
#print x.data, w.data
# import inspect
# def omega_compile(f):
# args, varargs, kwargs, defaults = inspect.getargspec(f)
# assert not varargs
# assert not kwargs
# def ret(*args):
# outputs = core.build(f, *args)
# return compile.prog(args, outputs)
# return ret
# @omega_compile
def
autoassociator
(
w
,
x
):
forward
=
sigmoid
(
core
.
dot
(
sigmoid
(
core
.
dot
(
x
,
w
)),
w
.
T
))
rec_error
=
core
.
sum
(
core
.
sqr
(
x
-
forward
))
w
-=
0.1
*
grad
.
grad
(
rec_error
,
w
)
return
w
,
rec_error
w2
,
rec_error
=
core
.
build
(
autoassociator
,
w
,
x
)
f
=
compile
.
to_func
([
w
,
x
],
[
w2
,
rec_error
])
#f = compile.single(w2, rec_error)
for
i
in
dataset_1hot
(
x
.
data
,
numpy
.
ndarray
((
1
,
)),
10000
):
# w.up_to_date = True
# x.up_to_date = True
w2
,
rec_error
=
f
(
w
.
data
,
x
.
data
)
if
not
(
i
%
1000
):
print
rec_error
print
"done!"
print
w
.
data
############################
# def fun():
# a = core.NumpyR(numpy.zeros(()) + 200)
# # b = numpy.ones(())
# # a = a * core.sqrt(core.isqr(a))
# a = a * core.isqr(a)
# return a
# f = core.build(fun)
# g = compile.to_func(gof.graph.inputs([f]), [f])
############################
# print core.ones((2, 2)) + 1
# print numpy.ones((2, 2)) ** numpy.ones((2, 2))
############################
# x = core.ones((2, 2))
# y = core.zeros((1, 1))
# #print "?", gof.graph.ops([], [x + y])
# # x + x
# # print "1", gof.eval_env#.ops()
# # y + y
# # print "2", gof.eval_env#.ops()
# # x + x
# # print "3", gof.eval_env#.ops()
# core.build_eval_mode()
# x = core.ones((2, 2))
# y = core.ones((2, 2)) * 2
# x += y.T
# # z = core.iadd(x, y)
# # core.iadd(x, y)
# print x
# core.pop_mode()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论