提交 211bc60d authored 作者: Matthew Rocklin's avatar Matthew Rocklin

Add LoadFromDisk op

Has issues currently. I'm having difficulty putting the path input in make_node Defaulting to placing it in the op construction instead
上级 bc7478d6
...@@ -2021,6 +2021,38 @@ def cast(x, dtype): ...@@ -2021,6 +2021,38 @@ def cast(x, dtype):
'imag(), angle() or abs()')) 'imag(), angle() or abs()'))
return _cast_mapping[dtype](x) return _cast_mapping[dtype](x)
##########################
# Disk Access
##########################
class LoadFromDisk(Op):
"""
@note: Non-differentiable.
"""
def __init__(self, path, dtype):
self.path = path
self.dtype = dtype
def __eq__(self, other):
return (type(self) == type(other) and
self.path == other.path and
self.dtype == other.dtype)
def __hash__(self):
return hash((type(self), self.path, self.dtype))
def make_node(self):
return gof.Apply(self, [], [tensor(self.dtype, broadcastable=(False,))])
def perform(self, node, inp, out):
d = numpy.load(self.path)
out[0][0] = d[d.keys()[0]].astype(self.dtype)
def __str__(self):
return "Load: %s"%self.path
def load(path, dtype='float64'):
return LoadFromDisk(path, dtype)()
########################## ##########################
# Unary Operations # Unary Operations
......
...@@ -3964,6 +3964,15 @@ class T_scalarfromtensor(unittest.TestCase): ...@@ -3964,6 +3964,15 @@ class T_scalarfromtensor(unittest.TestCase):
self.assertTrue(isinstance(v, numpy.int64)) self.assertTrue(isinstance(v, numpy.int64))
self.assertTrue(v.shape == (),v.shape) 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)
x = tensor.load(filename, 'int64')
y = x*2
fn = function([], [y])
assert (fn() == data*2).all()
class test_grad(unittest.TestCase): class test_grad(unittest.TestCase):
class O(gof.op.Op): class O(gof.op.Op):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论