Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c1cdabc8
提交
c1cdabc8
authored
10月 05, 2012
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow only modes None and 'c' in LoadFromDisk.
The other modes would trigger errors, segmentation faults, or wrong results, if the next Op was working inplace.
上级
21de50e6
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
50 行增加
和
14 行删除
+50
-14
io.py
theano/tensor/io.py
+16
-1
test_io.py
theano/tensor/tests/test_io.py
+34
-13
没有找到文件。
theano/tensor/io.py
浏览文件 @
c1cdabc8
...
@@ -19,6 +19,9 @@ class LoadFromDisk(Op):
...
@@ -19,6 +19,9 @@ class LoadFromDisk(Op):
def
__init__
(
self
,
dtype
,
broadcastable
,
mmap_mode
=
None
):
def
__init__
(
self
,
dtype
,
broadcastable
,
mmap_mode
=
None
):
self
.
dtype
=
numpy
.
dtype
(
dtype
)
# turn "float64" into numpy.float64
self
.
dtype
=
numpy
.
dtype
(
dtype
)
# turn "float64" into numpy.float64
self
.
broadcastable
=
broadcastable
self
.
broadcastable
=
broadcastable
if
mmap_mode
not
in
(
None
,
'c'
):
raise
ValueError
(
"The only supported values for mmap_mode "
"are None and 'c', got
%
s"
%
mmap_mode
)
self
.
mmap_mode
=
mmap_mode
self
.
mmap_mode
=
mmap_mode
self
.
_info
=
(
dtype
,
broadcastable
,
mmap_mode
)
self
.
_info
=
(
dtype
,
broadcastable
,
mmap_mode
)
...
@@ -49,7 +52,19 @@ class LoadFromDisk(Op):
...
@@ -49,7 +52,19 @@ class LoadFromDisk(Op):
def
load
(
path
,
dtype
,
broadcastable
,
mmap_mode
=
None
):
def
load
(
path
,
dtype
,
broadcastable
,
mmap_mode
=
None
):
"""
"""
Load an array from an .npy file
Load an array from an .npy file.
:param path: A Generic symbolic variable, that will contain a string
:param dtype: The data type of the array to be read.
:param broadcastable: The broadcastable pattern of the loaded array,
for instance, (False,) for a vector, (False, True) for a column,
(False, False) for a matrix.
:param mmap_mode: How the file will be loaded. None means that the
data will be copied into an array in memory, 'c' means that the file
will be mapped into virtual memory, so only the parts that are
needed will be actually read from disk and put into memory.
Other modes supported by numpy.load ('r', 'r+', 'w+') cannot
be supported by Theano.
>>> from theano import *
>>> from theano import *
>>> path = Variable(Generic())
>>> path = Variable(Generic())
...
...
theano/tensor/tests/test_io.py
浏览文件 @
c1cdabc8
...
@@ -6,28 +6,49 @@ import os
...
@@ -6,28 +6,49 @@ import os
class
T_load_tensor
(
unittest
.
TestCase
):
class
T_load_tensor
(
unittest
.
TestCase
):
def
test0
(
self
):
def
setUp
(
self
):
data
=
numpy
.
arange
(
5
,
dtype
=
numpy
.
int32
)
self
.
data
=
numpy
.
arange
(
5
,
dtype
=
numpy
.
int32
)
filename
=
os
.
path
.
join
(
self
.
filename
=
os
.
path
.
join
(
theano
.
config
.
base_compiledir
,
theano
.
config
.
base_compiledir
,
"_test.npy"
)
"_test.npy"
)
numpy
.
save
(
filename
,
data
)
numpy
.
save
(
self
.
filename
,
self
.
data
)
def
test0
(
self
):
path
=
Variable
(
Generic
())
path
=
Variable
(
Generic
())
# Not specifying mmap_mode defaults to None, and the data is
# copied into main memory
x
=
tensor
.
load
(
path
,
'int32'
,
(
False
,))
x
=
tensor
.
load
(
path
,
'int32'
,
(
False
,))
y
=
x
*
2
y
=
x
*
2
fn
=
function
([
path
],
y
)
assert
(
fn
(
self
.
filename
)
==
(
self
.
data
*
2
))
.
all
()
def
test_invalid_modes
(
self
):
# Modes 'r+', 'r', and 'w+' cannot work with Theano, becausei
# the output array may be modified inplace, and that should not
# modify the original file.
path
=
Variable
(
Generic
())
for
mmap_mode
in
(
'r+'
,
'r'
,
'w+'
,
'toto'
):
self
.
assertRaises
(
ValueError
,
tensor
.
load
,
path
,
'int32'
,
(
False
,),
mmap_mode
)
def
test1
(
self
):
path
=
Variable
(
Generic
())
# 'c' means "copy-on-write", which allow the array to be overwritten
# by an inplace Op in the graph, without modifying the underlying
# file.
x
=
tensor
.
load
(
path
,
'int32'
,
(
False
,),
'c'
)
# x ** 2 has been chosen because it will work inplace.
y
=
(
x
**
2
)
.
sum
()
fn
=
function
([
path
],
y
)
fn
=
function
([
path
],
y
)
assert
(
fn
(
filename
)
==
data
*
2
)
.
all
()
# Call fn() twice, to check that inplace ops do not cause trouble
assert
(
fn
(
self
.
filename
)
==
(
self
.
data
**
2
)
.
sum
())
.
all
()
assert
(
fn
(
self
.
filename
)
==
(
self
.
data
**
2
)
.
sum
())
.
all
()
def
test_memmap
(
self
):
def
test_memmap
(
self
):
data
=
numpy
.
arange
(
5
,
dtype
=
numpy
.
int32
)
filename
=
os
.
path
.
join
(
theano
.
config
.
base_compiledir
,
"_test.npy"
)
numpy
.
save
(
filename
,
data
)
path
=
Variable
(
Generic
())
path
=
Variable
(
Generic
())
x
=
tensor
.
load
(
path
,
'int32'
,
(
False
,),
mmap_mode
=
'
r+
'
)
x
=
tensor
.
load
(
path
,
'int32'
,
(
False
,),
mmap_mode
=
'
c
'
)
fn
=
function
([
path
],
x
)
fn
=
function
([
path
],
x
)
assert
type
(
fn
(
filename
))
==
numpy
.
core
.
memmap
assert
type
(
fn
(
self
.
filename
))
==
numpy
.
core
.
memmap
def
tearDown
(
self
):
def
tearDown
(
self
):
os
.
remove
(
os
.
path
.
join
(
os
.
remove
(
os
.
path
.
join
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论