提交 3775cef2 authored 作者: nouiz's avatar nouiz

Merge pull request #669 from mrocklin/load-op

Load op
......@@ -559,7 +559,8 @@ def orphans(i, o):
def clone(i, o, copy_inputs = True):
""" WRITEME
"""
Copies the subgraph contained between i and o.
:type i: list
:param i: input L{Variable}s
......@@ -568,8 +569,7 @@ def clone(i, o, copy_inputs = True):
:type copy_inputs: bool
:param copy_inputs: if True, the inputs will be copied (defaults to False)
Copies the subgraph contained between i and o and returns the
outputs of that copy (corresponding to o).
Returns the inputs and outputs of that copy.
"""
equiv = clone_get_equiv(i, o, copy_inputs)
return [equiv[input] for input in i], [equiv[output] for output in o]
......
......@@ -30,6 +30,7 @@ import sharedvar # adds shared-variable constructors
# `theano.shared` and `tensor._shared`.
from sharedvar import tensor_constructor as _shared
from io import *
def shared(*args, **kw):
"""
......
......@@ -2021,7 +2021,6 @@ def cast(x, dtype):
'imag(), angle() or abs()'))
return _cast_mapping[dtype](x)
##########################
# Unary Operations
##########################
......
import numpy
import theano
from theano import gof
from theano.gof import Apply, Constant, Generic, Op, Type, Value, Variable
from basic import tensor
##########################
# Disk Access
##########################
class LoadFromDisk(Op):
"""
An operation to load an array from disk
See Also
load
@note: Non-differentiable.
"""
def __init__(self, dtype, broadcastable, mmap_mode=None):
self.dtype = numpy.dtype(dtype) # turn "float64" into numpy.float64
self.broadcastable = broadcastable
self.mmap_mode = mmap_mode
self._info = (dtype, broadcastable, mmap_mode)
def __eq__(self, other):
return (type(self) == type(other) and self._info == other._info)
def __hash__(self):
return hash(self._info)
def make_node(self, path):
if isinstance(path, str):
path = Constant(Generic(), path)
return gof.Apply(self, [path], [tensor(self.dtype,
broadcastable=self.broadcastable)])
def perform(self, node, inp, out):
path = inp[0]
if (path.split('.')[-1] == 'npz'):
raise ValueError("Expected a .npy file, got %s instead"%path)
result = numpy.load(path, mmap_mode=self.mmap_mode)
if result.dtype != self.dtype:
raise TypeError("Expected an array of type %s, got %s instead"%
(self.dtype, result.dtype))
out[0][0] = result
def __str__(self):
return "Load{dtype:%s, broadcastable:%s, mmep:%s}"%self._info
def load(path, dtype, broadcastable, mmap_mode=None):
"""
Load an array from an .npy file
>>> from theano import *
>>> path = Variable(Generic())
>>> x = tensor.load(path, 'int64', (False,))
>>> y = x*2
>>> fn = function([path], y)
>>> fn("stored-array.npy")
array([0, 2, 4, 6, 8], dtype=int64)
"""
return LoadFromDisk(dtype, broadcastable, mmap_mode)(path)
......@@ -3964,7 +3964,6 @@ class T_scalarfromtensor(unittest.TestCase):
self.assertTrue(isinstance(v, numpy.int64))
self.assertTrue(v.shape == (),v.shape)
class test_grad(unittest.TestCase):
class O(gof.op.Op):
def __init__(self):
......
import unittest
import theano
from theano import tensor, function, Variable, Generic
import numpy
import os
class T_load_tensor(unittest.TestCase):
def test0(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())
x = tensor.load(path, 'int32', (False,))
y = x*2
fn = function([path], y)
assert (fn(filename) == data*2).all()
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())
x = tensor.load(path, 'int32', (False,), mmap_mode='r+')
fn = function([path], x)
assert type(fn(filename)) == numpy.core.memmap
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论