提交 ccb01355 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Add value_zeros method to {Tensor,CudaNdarray}Type

That way, we can more easily allocate an array full of zeros of the right type.
上级 5ec01548
...@@ -1089,15 +1089,9 @@ def _get_preallocated_maps(node, thunk, prealloc_modes, def_val, ...@@ -1089,15 +1089,9 @@ def _get_preallocated_maps(node, thunk, prealloc_modes, def_val,
# Initial allocation # Initial allocation
init_strided = {} init_strided = {}
for r in node.outputs: for r in node.outputs:
if isinstance(r.type, TensorType): if isinstance(r.type, (TensorType, CudaNdarrayType)):
# Create a buffer twice as large in every dimension # Create a buffer twice as large in every dimension
new_buf = numpy.zeros( new_buf = r.type.value_zeros(
shape=[(s * 2) for s in r_vals[r].shape],
dtype=r_vals[r].dtype)
init_strided[r] = new_buf
elif isinstance(r.type, CudaNdarrayType):
new_buf = CudaNdarray.zeros(
[(s * 2) for s in r_vals[r].shape]) [(s * 2) for s in r_vals[r].shape])
init_strided[r] = new_buf init_strided[r] = new_buf
......
...@@ -54,6 +54,11 @@ class CudaNdarrayType(Type): ...@@ -54,6 +54,11 @@ class CudaNdarrayType(Type):
A cyclic dependency is avoided by not hardcoding this class. A cyclic dependency is avoided by not hardcoding this class.
""" """
value_zeros = staticmethod(cuda.CudaNdarray.zeros)
"""
Create an CudaNdarray full of 0 values
"""
def __init__(self, broadcastable, name=None, dtype=None): def __init__(self, broadcastable, name=None, dtype=None):
if dtype != None and dtype != 'float32': if dtype != None and dtype != 'float32':
raise TypeError('%s only supports dtype float32 for now. Tried ' raise TypeError('%s only supports dtype float32 for now. Tried '
......
...@@ -1024,6 +1024,13 @@ class TensorType(Type): ...@@ -1024,6 +1024,13 @@ class TensorType(Type):
else: else:
return () return ()
def value_zeros(self, shape):
"""
Create an numpy ndarray full of 0 values.
"""
return numpy.zeros(shape, dtype=self.dtype)
# Register CudaNdarrayType to the OutputGuard list of known types # Register CudaNdarrayType to the OutputGuard list of known types
# to have OutputGuard generate C code for this type. # to have OutputGuard generate C code for this type.
theano.compile.mode.register_OutputGuard_c_code(TensorType) theano.compile.mode.register_OutputGuard_c_code(TensorType)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论