Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bfef7ff0
提交
bfef7ff0
authored
7月 20, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
差异文件
merge
上级
8f2c17ec
34736410
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
65 行增加
和
15 行删除
+65
-15
function.txt
doc/topics/function.txt
+4
-4
test_function.py
theano/compile/tests/test_function.py
+6
-0
cmodule.py
theano/gof/cmodule.py
+37
-11
test_misc.py
theano/tensor/tests/test_misc.py
+18
-0
没有找到文件。
doc/topics/function.txt
浏览文件 @
bfef7ff0
...
...
@@ -352,17 +352,17 @@ If a list of ``Variable`` or ``Out`` instances is given as argument, then the co
x, y, s = T.matrices('xys')
# print a list of 2 ndarrays
fn1 = theano.function([x], [x+x, Out((x+x).T, borrow=True])
print fn1(n
d
array([[1,0],[0,1]]))
fn1 = theano.function([x], [x+x, Out((x+x).T, borrow=True
)
])
print fn1(n
umpy.as
array([[1,0],[0,1]]))
# print a list of 1 ndarray
fn2 = theano.function([x], [x+x])
print fn2(n
d
array([[1,0],[0,1]]))
print fn2(n
umpy.as
array([[1,0],[0,1]]))
# print an ndarray
fn3 = theano.function([x], outputs=x+x)
print fn3(n
d
array([[1,0],[0,1]]))
print fn3(n
umpy.as
array([[1,0],[0,1]]))
.. _function_mode:
...
...
theano/compile/tests/test_function.py
浏览文件 @
bfef7ff0
...
...
@@ -275,6 +275,12 @@ class T_function(unittest.TestCase):
self
.
failUnless
(
dec
[
s
]
==
-
1
)
def
test_borrow_output
(
self
):
a
=
T
.
dmatrix
()
f
=
function
([
a
],
Out
(
a
,
borrow
=
False
))
o
=
N
.
ones
((
3
,
3
))
assert
o
is
not
f
(
o
)
class
T_picklefunction
(
unittest
.
TestCase
):
def
test_deepcopy
(
self
):
...
...
theano/gof/cmodule.py
浏览文件 @
bfef7ff0
...
...
@@ -7,7 +7,7 @@ import numpy.distutils #TODO: TensorType should handle this
import
compilelock
# we will abuse the lockfile mechanism when reading and writing the registry
_logger
=
logging
.
getLogger
(
"theano.gof.cmodule"
)
_logger
.
setLevel
(
logging
.
INFO
)
_logger
.
setLevel
(
logging
.
WARN
)
def
error
(
*
args
):
#sys.stderr.write('ERROR:'+ ' '.join(str(a) for a in args)+'\n')
...
...
@@ -124,7 +124,7 @@ class DynamicModule(object):
#TODO: add_type
def
dlimport
(
fullpath
,
suffix
=
None
):
"""Dynamically load a .so, .dll, or .py file
"""Dynamically load a .so, .
pyd, .
dll, or .py file
:type fullpath: string
:param fullpath: a fully-qualified path do a compiled python module
...
...
@@ -137,6 +137,8 @@ def dlimport(fullpath, suffix=None):
if
suffix
is
None
:
if
fullpath
.
endswith
(
'.so'
):
suffix
=
'.so'
elif
fullpath
.
endswith
(
'.pyd'
):
suffix
=
'.pyd'
elif
fullpath
.
endswith
(
'.dll'
):
suffix
=
'.dll'
elif
fullpath
.
endswith
(
'.py'
):
...
...
@@ -172,9 +174,9 @@ def module_name_from_dir(dirname):
"""Scan the contents of a cache directory and return full path of the dynamic lib in it.
"""
files
=
os
.
listdir
(
dirname
)
names
=
[
file
for
file
in
files
if
file
.
endswith
(
'.so'
)]
names
=
[
file
for
file
in
files
if
file
.
endswith
(
'.so'
)
or
file
.
endswith
(
'.pyd'
)
]
if
len
(
names
)
!=
1
:
raise
Exception
(
'Failed to load
.so
from dir'
,
dirname
)
raise
Exception
(
'Failed to load
dynamic libraries
from dir'
,
dirname
)
return
os
.
path
.
join
(
dirname
,
names
[
0
])
class
ModuleCache
(
object
):
...
...
@@ -210,7 +212,7 @@ class ModuleCache(object):
"""maps module names to loaded module objects"""
entry_from_key
=
{}
"""Maps keys to the filename of a .so
"""Maps keys to the filename of a .so
/.pyd.
"""
stats
=
[]
...
...
@@ -353,7 +355,13 @@ class ModuleCache(object):
key_pkl
=
os
.
path
.
join
(
location
,
'key.pkl'
)
key_file
=
file
(
key_pkl
,
'w'
)
try
:
cPickle
.
dump
(
key
,
key_file
,
cPickle
.
HIGHEST_PROTOCOL
)
if
sys
.
platform
==
'win32'
:
# Looks like there is a bug under Windows, where using the
# highest protocol will result in a pickle file that cannot
# be loaded afterwards.
cPickle
.
dump
(
key
,
key_file
)
else
:
cPickle
.
dump
(
key
,
key_file
,
cPickle
.
HIGHEST_PROTOCOL
)
key_file
.
close
()
key_broken
=
False
except
cPickle
.
PicklingError
:
...
...
@@ -449,6 +457,13 @@ def get_module_cache(dirname, force_fresh=None):
atexit
.
register
(
_module_cache
.
_on_atexit
)
return
_module_cache
def
get_lib_extension
():
"""Return the platform-dependent extension for compiled modules."""
if
sys
.
platform
==
'win32'
:
return
'pyd'
else
:
return
'so'
def
gcc_module_compile_str
(
module_name
,
src_code
,
location
=
None
,
include_dirs
=
[],
lib_dirs
=
[],
libs
=
[],
preargs
=
[],
tmpdir
=
None
):
#TODO: don't to the dlimport in this function
...
...
@@ -456,11 +471,21 @@ def gcc_module_compile_str(module_name, src_code, location=None, include_dirs=[]
preargs
.
append
(
'-fPIC'
)
no_opt
=
False
include_dirs
=
[
distutils
.
sysconfig
.
get_python_inc
()]
+
\
numpy
.
distutils
.
misc_util
.
get_numpy_include_dirs
()
\
+
include_dirs
libs
=
[
os
.
path
.
split
(
distutils
.
sysconfig
.
get_python_inc
())[
-
1
]]
+
libs
python_inc
=
distutils
.
sysconfig
.
get_python_inc
()
if
sys
.
platform
==
'win32'
:
# Typical include directory: C:\Python26\include
libname
=
os
.
path
.
basename
(
os
.
path
.
dirname
(
python_inc
))
.
lower
()
# Also add directory containing the Python library to the library
# directories.
python_lib_dir
=
os
.
path
.
join
(
os
.
path
.
dirname
(
python_inc
),
'libs'
)
lib_dirs
=
[
python_lib_dir
]
+
lib_dirs
else
:
# Typical include directory: /usr/include/python2.6
libname
=
os
.
path
.
basename
(
python_inc
)
libs
=
[
libname
]
+
libs
workdir
=
location
...
...
@@ -474,7 +499,8 @@ def gcc_module_compile_str(module_name, src_code, location=None, include_dirs=[]
cppfile
.
write
(
src_code
)
cppfile
.
close
()
lib_filename
=
os
.
path
.
join
(
workdir
,
'
%
s.so'
%
module_name
)
lib_filename
=
os
.
path
.
join
(
workdir
,
'
%
s.
%
s'
%
(
module_name
,
get_lib_extension
()))
debug
(
'Generating shared lib'
,
lib_filename
)
cmd
=
[
'g++'
,
'-shared'
,
'-g'
]
...
...
@@ -499,7 +525,6 @@ def gcc_module_compile_str(module_name, src_code, location=None, include_dirs=[]
file
(
os
.
path
.
join
(
workdir
,
"__init__.py"
),
'w'
)
.
close
()
rval
=
dlimport
(
lib_filename
)
return
rval
...
...
@@ -531,7 +556,8 @@ def nvcc_module_compile_str(module_name, src_code, location=None, include_dirs=[
try
:
cppfile
.
write
(
src_code
)
cppfile
.
close
()
lib_filename
=
os
.
path
.
join
(
workdir
,
'
%
s.so'
%
module_name
)
lib_filename
=
os
.
path
.
join
(
workdir
,
'
%
s.
%
s'
%
(
module_name
,
get_lib_extension
()))
debug
(
'Generating shared lib'
,
lib_filename
)
cmd
=
[
'nvcc'
,
'-shared'
,
'-g'
]
...
...
theano/tensor/tests/test_misc.py
浏览文件 @
bfef7ff0
...
...
@@ -13,3 +13,21 @@ def test_bug_2009_06_02_trac_387():
#z = tensor.lscalar('z')
#f = theano.function([z], tensor.DimShuffle([], ['x'])(z) / 2)
def
test_bug_2009_07_17_borrowed_output
():
"""Regression test for a bug where output was borrowed by mistake."""
a
=
theano
.
tensor
.
dmatrix
()
b
=
theano
.
tensor
.
dmatrix
()
# The output should *NOT* be borrowed.
g
=
theano
.
function
([
a
,
b
],
theano
.
Out
(
theano
.
tensor
.
dot
(
a
,
b
),
borrow
=
False
))
x
=
numpy
.
zeros
((
1
,
2
))
y
=
numpy
.
ones
((
2
,
5
))
z
=
g
(
x
,
y
)
print
z
# Should be zero.
x
.
fill
(
1
)
print
g
(
x
,
y
)
# Should be non-zero.
print
z
# Should still be zero.
assert
numpy
.
linalg
.
norm
(
z
)
==
0
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论