def __theano_build__(self): W1 = self.W1 W2 = self.W2 b1 = self.b1 b2 = self.b2 x = dvector(name="x") y = dvector(name="y") def forward(x): h = T.tanh(x.dot(W1) + b1) o = T.nnet.softmax(h.dot(W2) + b2) return o o = forward(x) prediction = T.argmax(o, axis=1) o_error = ((y-o)**2).sum() dW1 = T.grad(o_error, W1) dW2 = T.grad(o_error, W2) db1 = T.grad(o_error, b1) db2 = T.grad(o_error, b2) self.forward_propagation = theano.function([x], o) self.predict = theano.function([x], prediction) self.cerror = theano.function([x, y], o_error) learning_rate = T.scalar("learning_rate") self.sgd_step = theano.function([x,y,learning_rate], [], updates=[(self.W1, self.W1-learning_rate*dW1), (self.W2, self.W2-learning_rate*dW2), (self.b1, self.b1-learning_rate*db1), (self.b2, self.b2-learning_rate*db2)])
def make_node(self, M): M = basic.as_tensor_variable(M) if M.ndim != 0: raise TypeError( f"{self.__class__.__name__} only works on scalar input") elif M.dtype not in theano.tensor.integer_dtypes: # dtype is a theano attribute here raise TypeError( f"{self.__class__.__name__} only works on integer input") return Apply(self, [M], [basic.dvector()])
def test_2arg(self): x = dmatrix("x") x.tag.test_value = np.zeros((2, 2)) y = dvector("y") y.tag.test_value = [0, 0, 0, 0] @as_op([dmatrix, dvector], dvector) def cumprod_plus(x, y): return np.cumprod(x) + y fn = function([x, y], cumprod_plus(x, y)) r = fn([[1.5, 5], [2, 2]], [1, 100, 2, 200]) r0 = np.array([2.5, 107.5, 17.0, 230.0]) assert np.allclose(r, r0), (r, r0)
def make_node(self, x, weights): warnings.warn(( "Tile op is deprecated, use tile function instead."), stacklevel=3) x = basic.as_tensor_variable(x) if x.dtype not in BinCountOp.compatible_type: raise TypeError("Inputs dtype must be an integer.") # Some dtypes are not supported by numpy's implementation of bincount. # Until another one is available, we should fail at graph construction # time, not wait for execution. int_bitwidth = theano.gof.python_int_bitwidth() if int_bitwidth == 64: numpy_unsupported_dtypes = ('uint64',) if int_bitwidth == 32: numpy_unsupported_dtypes = ('uint32', 'int64', 'uint64') intp_bitwidth = theano.gof.local_bitwidth() if intp_bitwidth == 32: out_type = basic.ivector() elif intp_bitwidth == 64: out_type = basic.lvector() if x.dtype in numpy_unsupported_dtypes: raise TypeError( ("Input dtypes %s are not supported by numpy.bincount, " % numpy_unsupported_dtypes), x.dtype) if x.ndim != 1: raise TypeError("Inputs must be of dimension 1.") if weights is None: weights = theano.gof.Constant(theano.gof.Generic(), None) else: weights = basic.as_tensor_variable(weights) out_type = basic.dvector() if weights.ndim != 1: raise TypeError("Weights cannot have a number of" "dimension different of 1.") return theano.Apply(self, [x, weights], [out_type])
def test_infer_shape(self): x = dmatrix("x") x.tag.test_value = np.zeros((2, 2)) y = dvector("y") y.tag.test_value = [0, 0, 0, 0] def infer_shape(fgraph, node, shapes): x, y = shapes return [y] @as_op([dmatrix, dvector], dvector, infer_shape) def cumprod_plus(x, y): return np.cumprod(x) + y self._compile_and_check( [x, y], [cumprod_plus(x, y)], [[[1.5, 5], [2, 2]], [1, 100, 2, 200]], cumprod_plus.__class__, warn=False, )