提交 24294c33 authored 作者: James Bergstra's avatar James Bergstra

tensor - introduced use of tsor_apply.Apply which handles forward-propagation of

variable.tag.shape
上级 b2fb02b1
...@@ -13,7 +13,8 @@ import numpy, theano ...@@ -13,7 +13,8 @@ import numpy, theano
from copy import copy from copy import copy
from theano import gof from theano import gof
from theano.gof import Variable, Op, utils, Type, Constant, Apply, Value from theano.gof import Variable, Op, utils, Type, Constant, Value
from .tsor_apply import Apply
from theano import gradient from theano import gradient
......
...@@ -4,7 +4,7 @@ import sys, traceback, logging, copy, os ...@@ -4,7 +4,7 @@ import sys, traceback, logging, copy, os
import numpy import numpy
import numpy.distutils import numpy.distutils
from theano.configparser import config, AddConfigVar, StrParam from theano.configparser import config, AddConfigVar, StrParam
from theano.gof import (utils, Op, Apply, view_roots, PatternSub, DestroyHandler, from theano.gof import (utils, Op, view_roots, PatternSub, DestroyHandler,
SeqOptimizer, local_optimizer, Optimizer, LocalOptimizer, OpKeyOptimizer, SeqOptimizer, local_optimizer, Optimizer, LocalOptimizer, OpKeyOptimizer,
InconsistencyError, toolbox, SequenceDB, EquilibriumOptimizer) InconsistencyError, toolbox, SequenceDB, EquilibriumOptimizer)
from theano.printing import pprint, FunctionPrinter from theano.printing import pprint, FunctionPrinter
...@@ -13,6 +13,8 @@ from theano.gof.python25 import any ...@@ -13,6 +13,8 @@ from theano.gof.python25 import any
import theano.scalar import theano.scalar
import basic as T import basic as T
from .tsor_apply import Apply
#NB: this clobbers the builtin 'compile' symbol #NB: this clobbers the builtin 'compile' symbol
from theano import compile #to register the optimizer built by this file from theano import compile #to register the optimizer built by this file
......
...@@ -3,7 +3,7 @@ import elemwise_cgen as cgen ...@@ -3,7 +3,7 @@ import elemwise_cgen as cgen
import numpy, theano import numpy, theano
from theano import gof from theano import gof
from theano.gof import Op, Apply from theano.gof import Op
from theano import scalar from theano import scalar
from theano.scalar import Scalar from theano.scalar import Scalar
from theano import printing from theano import printing
...@@ -11,6 +11,8 @@ from theano.printing import pprint ...@@ -11,6 +11,8 @@ from theano.printing import pprint
from theano.gof.python25 import all, any from theano.gof.python25 import all, any
from copy import copy, deepcopy from copy import copy, deepcopy
from .tsor_apply import Apply
# tensor depends on elemwise to provide definitions for several ops # tensor depends on elemwise to provide definitions for several ops
# but elemwise needs to make TensorType instances, so we have these as # but elemwise needs to make TensorType instances, so we have these as
...@@ -150,6 +152,7 @@ class DimShuffle(Op): ...@@ -150,6 +152,7 @@ class DimShuffle(Op):
output = TensorType(dtype = input.type.dtype, output = TensorType(dtype = input.type.dtype,
broadcastable = ob).make_variable() broadcastable = ob).make_variable()
return Apply(self, [input], [output]) return Apply(self, [input], [output])
def __eq__(self, other): def __eq__(self, other):
......
...@@ -9,6 +9,7 @@ import numpy ...@@ -9,6 +9,7 @@ import numpy
import theano import theano
import theano.tensor as tensor import theano.tensor as tensor
from theano import gof, Op, tensor, config from theano import gof, Op, tensor, config
from theano.tensor.tsor_apply import Apply
from theano.gof.python25 import any from theano.gof.python25 import any
import logging import logging
_logger=logging.getLogger("theano.signal.conv") _logger=logging.getLogger("theano.signal.conv")
...@@ -481,7 +482,7 @@ class ConvOp(Op): ...@@ -481,7 +482,7 @@ class ConvOp(Op):
broadcastable=[_inputs.broadcastable[0], broadcastable=[_inputs.broadcastable[0],
_kerns.broadcastable[0], False, False]); _kerns.broadcastable[0], False, False]);
return gof.Apply(self, [_inputs, _kerns], [output]) return Apply(self, [_inputs, _kerns], [output])
def infer_shape(self, node, input_shapes): def infer_shape(self, node, input_shapes):
imshp = input_shapes[0] imshp = input_shapes[0]
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
:note: TODO: factor this out into a neural-network toolbox. :note: TODO: factor this out into a neural-network toolbox.
""" """
import numpy
from theano import gof from theano import gof
from theano import printing from theano import printing
...@@ -9,7 +10,7 @@ from theano.tensor import basic as tensor ...@@ -9,7 +10,7 @@ from theano.tensor import basic as tensor
from theano.tensor import elemwise, dmatrix, fmatrix, dvector, fvector from theano.tensor import elemwise, dmatrix, fmatrix, dvector, fvector
from theano.tensor import opt from theano.tensor import opt
from theano.compile import optdb from theano.compile import optdb
import numpy from theano.tensor.tsor_apply import Apply
from theano.tensor.nnet.sigm import sigmoid, softplus from theano.tensor.nnet.sigm import sigmoid, softplus
...@@ -53,7 +54,7 @@ class SoftmaxWithBias(gof.Op): ...@@ -53,7 +54,7 @@ class SoftmaxWithBias(gof.Op):
raise ValueError('b must be 1-d tensor of floats') raise ValueError('b must be 1-d tensor of floats')
sm = x.type.make_variable() sm = x.type.make_variable()
return gof.Apply(self, [x, b], [sm]) return Apply(self, [x, b], [sm])
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
x, b = input_storage x, b = input_storage
...@@ -220,7 +221,7 @@ class SoftmaxGrad(gof.Op): ...@@ -220,7 +221,7 @@ class SoftmaxGrad(gof.Op):
def make_node(self, dy, sm, **kwargs): def make_node(self, dy, sm, **kwargs):
dy = tensor.as_tensor_variable(dy) dy = tensor.as_tensor_variable(dy)
sm = tensor.as_tensor_variable(sm) sm = tensor.as_tensor_variable(sm)
return gof.Apply(self, [dy, sm], [sm.type.make_variable()]) return Apply(self, [dy, sm], [sm.type.make_variable()])
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
dy, sm = input_storage dy, sm = input_storage
...@@ -322,7 +323,7 @@ class Softmax(gof.Op): ...@@ -322,7 +323,7 @@ class Softmax(gof.Op):
raise ValueError('x must be 1-d or 2-d tensor of floats') raise ValueError('x must be 1-d or 2-d tensor of floats')
if x.ndim == 1: if x.ndim == 1:
x = tensor.shape_padleft(x, n_ones=1) x = tensor.shape_padleft(x, n_ones=1)
return gof.Apply(self, [x], [x.type()]) return Apply(self, [x], [x.type()])
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
x, = input_storage x, = input_storage
...@@ -449,7 +450,7 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op): ...@@ -449,7 +450,7 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
# nll = TensorType(x.dtype, y.broadcastable) # nll = TensorType(x.dtype, y.broadcastable)
sm = x.type.make_variable() sm = x.type.make_variable()
am = y_idx.type.make_variable() am = y_idx.type.make_variable()
return gof.Apply(self, [x, b, y_idx], [nll, sm, am]) return Apply(self, [x, b, y_idx], [nll, sm, am])
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
""" """
The math, where x is an input vector, and t is a target index: The math, where x is an input vector, and t is a target index:
...@@ -627,7 +628,7 @@ class CrossentropySoftmax1HotWithBiasDx (gof.Op): ...@@ -627,7 +628,7 @@ class CrossentropySoftmax1HotWithBiasDx (gof.Op):
dy = tensor.as_tensor_variable(dy) dy = tensor.as_tensor_variable(dy)
sm = tensor.as_tensor_variable(sm) sm = tensor.as_tensor_variable(sm)
y_idx = tensor.as_tensor_variable(y_idx) y_idx = tensor.as_tensor_variable(y_idx)
return gof.Apply(self, [dy, sm, y_idx],[sm.type.make_variable()]) return Apply(self, [dy, sm, y_idx],[sm.type.make_variable()])
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
dy, sm, y_idx = input_storage dy, sm, y_idx = input_storage
dx = numpy.zeros_like(sm) dx = numpy.zeros_like(sm)
...@@ -765,7 +766,7 @@ class CrossentropyCategorical1HotGrad(gof.Op): ...@@ -765,7 +766,7 @@ class CrossentropyCategorical1HotGrad(gof.Op):
def __str__(self): def __str__(self):
return self.__class__.__name__ return self.__class__.__name__
def make_node(self, g_y, coding_dist, true_one_of_n): def make_node(self, g_y, coding_dist, true_one_of_n):
return gof.Apply(self, [g_y, coding_dist, true_one_of_n], [coding_dist.type()]) return Apply(self, [g_y, coding_dist, true_one_of_n], [coding_dist.type()])
def perform(self, node, (g_y, coding_dist, true_one_of_n), (g_coding_strg,)): def perform(self, node, (g_y, coding_dist, true_one_of_n), (g_coding_strg,)):
g_coding = numpy.zeros_like(coding_dist) g_coding = numpy.zeros_like(coding_dist)
for i in xrange(len(g_y)): for i in xrange(len(g_y)):
...@@ -813,7 +814,7 @@ class CrossentropyCategorical1Hot(gof.Op): ...@@ -813,7 +814,7 @@ class CrossentropyCategorical1Hot(gof.Op):
'(got type: %s instead of: %s)' % (_true_one_of_n.type, '(got type: %s instead of: %s)' % (_true_one_of_n.type,
tensor.lvector)) tensor.lvector))
return gof.Apply(self, [_coding_dist, _true_one_of_n], return Apply(self, [_coding_dist, _true_one_of_n],
[tensor.Tensor(dtype=_coding_dist.dtype, broadcastable=[False])()]) [tensor.Tensor(dtype=_coding_dist.dtype, broadcastable=[False])()])
def perform(self, node, (coding, one_of_n), (y_out,)): def perform(self, node, (coding, one_of_n), (y_out,)):
...@@ -1270,7 +1271,7 @@ class Prepend_scalar_constant_to_each_row(gof.Op): ...@@ -1270,7 +1271,7 @@ class Prepend_scalar_constant_to_each_row(gof.Op):
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
TypeError("the value to prepend don't have the same type as the matrix") TypeError("the value to prepend don't have the same type as the matrix")
node = gof.Apply(op=self, inputs=[mat], outputs=[tensor.matrix()]) node = Apply(op=self, inputs=[mat], outputs=[tensor.matrix()])
return node return node
def perform(self, node, (mat, ), (output, )): def perform(self, node, (mat, ), (output, )):
...@@ -1311,7 +1312,7 @@ class Prepend_scalar_to_each_row(gof.Op): ...@@ -1311,7 +1312,7 @@ class Prepend_scalar_to_each_row(gof.Op):
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
TypeError("the value to prepend don't have the same type as the matrix") TypeError("the value to prepend don't have the same type as the matrix")
node = gof.Apply(op=self, inputs=[val,mat], outputs=[tensor.matrix()]) node = Apply(op=self, inputs=[val,mat], outputs=[tensor.matrix()])
return node return node
def perform(self, node, (val,mat), (output, )): def perform(self, node, (val,mat), (output, )):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论