提交 512a28f3 authored 作者: Matthew Rocklin's avatar Matthew Rocklin

move LoadFromDisk op to new io.py file

Also moved relevant test
上级 78348f09
......@@ -2021,60 +2021,6 @@ def cast(x, dtype):
'imag(), angle() or abs()'))
return _cast_mapping[dtype](x)
##########################
# Disk Access
##########################
class LoadFromDisk(Op):
"""
An operation to load an array from disk
See Also
load
@note: Non-differentiable.
"""
def __init__(self, dtype, broadcastable):
self.dtype = dtype
self.broadcastable = broadcastable
def __eq__(self, other):
return (type(self) == type(other) and
self.broadcastable == other.broadcastable and
self.dtype == other.dtype)
def __hash__(self):
return hash((type(self), self.dtype, self.broadcastable))
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]
d = numpy.load(path)
out[0][0] = d[d.keys()[0]].astype(self.dtype)
def __str__(self):
return "Load: %s, %s"%(self.dtype, self.broadcastable)
def load(path, dtype, broadcastable):
"""
Load an array from a .npz file
>>> from theano import *
>>> path = Variable(Generic())
>>> x = tensor.load(path, 'int64', (False,))
>>> y = x*2
>>> fn = function([path], y)
>>> fn("stored-array.npz")
array([0, 2, 4, 6, 8], dtype=int64)
"""
return LoadFromDisk(dtype, broadcastable)(path)
##########################
# 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):
self.dtype = dtype
self.broadcastable = broadcastable
def __eq__(self, other):
return (type(self) == type(other) and
self.broadcastable == other.broadcastable and
self.dtype == other.dtype)
def __hash__(self):
return hash((type(self), self.dtype, self.broadcastable))
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]
d = numpy.load(path)
out[0][0] = d[d.keys()[0]].astype(self.dtype)
def __str__(self):
return "Load: %s, %s"%(self.dtype, self.broadcastable)
def load(path, dtype, broadcastable):
"""
Load an array from a .npz file
>>> from theano import *
>>> path = Variable(Generic())
>>> x = tensor.load(path, 'int64', (False,))
>>> y = x*2
>>> fn = function([path], y)
>>> fn("stored-array.npz")
array([0, 2, 4, 6, 8], dtype=int64)
"""
return LoadFromDisk(dtype, broadcastable)(path)
......@@ -3964,17 +3964,6 @@ class T_scalarfromtensor(unittest.TestCase):
self.assertTrue(isinstance(v, numpy.int64))
self.assertTrue(v.shape == (),v.shape)
class T_load_tensor(unittest.TestCase):
def test0(self):
data = numpy.arange(5)
filename = "_load_tensor_test_1.npz"
numpy.savez(filename, data)
path = Variable(Generic())
x = tensor.load(path, 'int64', (False,))
y = x*2
fn = function([path], [y])
assert (fn(filename) == data*2).all()
class test_grad(unittest.TestCase):
class O(gof.op.Op):
def __init__(self):
......
import unittest
from theano import tensor, function, Variable, Generic
import numpy
class T_load_tensor(unittest.TestCase):
def test0(self):
data = numpy.arange(5)
filename = "_load_tensor_test_1.npz"
numpy.savez(filename, data)
path = Variable(Generic())
x = tensor.load(path, 'int64', (False,))
y = x*2
fn = function([path], [y])
assert (fn(filename) == data*2).all()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论