我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.tensor.vector()。
def lyr_linear( self, name_, s_x_, idim_, odim_, init_=None, bias_=0., params_di_='params'): ''' dense matrix multiplication, optionally adding a bias vector ''' name_W = name_+'_w' name_B = name_+'_b' self.set_vars(params_di_) if init_ is None: init_ = dict(init_=[1.4/sqrt(idim_+odim_)]) v_W = self.get_variable(name_W, (idim_,odim_), **init_) if bias_ is None: s_ret = T.dot(s_x_, v_W) else: v_B = self.get_variable(name_B, (odim_,), bias_) s_ret = T.dot(s_x_, v_W) + v_B return s_ret
def lyr_linear( self, name_, s_x_, idim_, odim_, init_=None, bias_=0., params_group_='params' ): ''' dense matrix multiplication, optionally adding a bias vector ''' name_W = name_+'_w' name_B = name_+'_b' if init_ is None: init_ = [1.4/sqrt(idim_+odim_)] with self.get_group(params_group_): v_W = self.get_variable(name_W, (idim_,odim_), init_=init_) if bias_ is None: s_ret = T.dot(s_x_, v_W) else: with self.get_group(params_group_): v_B = self.get_variable(name_B, (odim_,), bias_) s_ret = T.dot(s_x_, v_W) + v_B return s_ret
def _build_validate_function(self): print 'building validate function' t1 = datetime.datetime.now() data = self.val_data captions = self.val_data_captions self._index_im_val = T.vector(dtype='int32') # index to the minibatch self._index_cap_val = T.vector(dtype='int32') self._cap_len_val = T.scalar(dtype='int32') self._validate_function = theano.function(inputs=[self._index_im_val, self._index_cap_val, self._cap_len_val, self._run_steps], outputs=[self._kl_final, self._logpxz, self._log_likelihood], updates=self._updates_train, givens={ self._x: data[self._index_im_val], self._y: captions[self._index_cap_val,0:self._cap_len_val] }) t2 = datetime.datetime.now() print (t2-t1)
def _build_validate_function(self): print 'building validate function' t1 = datetime.datetime.now() data = self.val_data captions = self.val_captions self._index_im_val = T.vector(dtype='int32') # index to the minibatch self._index_cap_val = T.vector(dtype='int32') self._validate_function = theano.function(inputs=[self._index_im_val, self._index_cap_val, self._run_steps], outputs=[self._kl_final, self._logpxz, self._log_likelihood], updates=self._updates_train, givens={ self._x: data[self._index_im_val], self._y: captions[self._index_cap_val] }) t2 = datetime.datetime.now() print (t2-t1)
def compile(self): x_train = T.tensor4('x_train') actions_train = T.matrix('actions_train') y_train = T.matrix('y_train') cost_function = self.squared_error(x_train, actions_train, y_train) self.train_function = theano.function([x_train, actions_train, y_train], cost_function, updates=self.sgd(cost_function, self.params), on_unused_input='ignore', allow_input_downcast=True) x_pred = T.tensor3('x_pred') actions_pred = T.vector('actions_pred') output_function = self.output(x_pred, actions_pred) self.predict_function = theano.function([x_pred, actions_pred], output_function, on_unused_input='ignore', allow_input_downcast=True) return self
def quick_cost(self, delta=0): # quickly evaluate objective (costs[0]) over the CG batch # for `current params` + delta # delta can be a flat vector or a list (else it is not used) if isinstance(delta, numpy.ndarray): delta = self.flat_to_list(delta) if type(delta) in (list, tuple): for i, d in zip(self.p, delta): i.set_value(i.get_value() + d) cost = numpy.mean([self.f_cost(*i)[0] for i in self.cg_dataset.iterate(update=False)]) if type(delta) in (list, tuple): for i, d in zip(self.p, delta): i.set_value(i.get_value() - d) return cost
def squareError(x): """Square error loss function.""" if x.ndim == 1: y = tt.vector('y') L = tt.mean((x - y) ** 2) elif x.ndim == 2: y = tt.matrix('y') L = tt.mean(tt.sum((x - y) ** 2, axis=1)) else: raise ValueError('x must be either a vector or a matrix.') L.name = 'loss' return y, L
def crossEntropy(x): """Cross entropy loss function. Only works for networks with one output.""" if x.ndim == 1: pass elif x.ndim == 2: x = x[:, 0] else: raise ValueError('x must be either a vector or a matrix.') y = tt.vector('y') L = -tt.mean(y * tt.log(x) + (1-y) * tt.log(1-x)) L.name = 'loss' return y, L
def accuracy(x): """Accuracy loss function. Mainly useful for validation.""" if x.ndim == 1: pass elif x.ndim == 2: x = x.argmax(axis=1) else: raise ValueError('x must be either a vector or a matrix.') y = tt.vector('y') L = 100.0 * tt.mean(tt.eq(y, x)) L.name = 'loss' return y, L
def times_diag(input, n_hidden, diag, swap_re_im): # input is a Ix2n_hidden matrix, where I is number # of training examples # diag is a n_hidden-dimensional real vector, which creates # the 2n_hidden x 2n_hidden complex diagonal matrix using # e.^{j.*diag}=cos(diag)+j.*sin(diag) d = T.concatenate([diag, -diag]) #d is 2n_hidden Re = T.cos(d).dimshuffle('x',0) Im = T.sin(d).dimshuffle('x',0) input_times_Re = input * Re input_times_Im = input * Im output = input_times_Re + input_times_Im[:, swap_re_im] return output
def train_one_batch(self): self.actions = tensor.vector(name='actions', dtype='int64') self.y = tensor.vector(name='y', dtype=theano.config.floatX) cost = self.output_vector[self.actions].sum() / self.actions.shape[0] coef = (self.y - self.output_vector[self.actions]).sum() / self.actions.shape[0] grads = tensor.grad(cost, wrt=self.params.values()) grads = [coef*t for t in grads] lr = tensor.scalar(name='lr') f_update = self._adadelta(lr, self.params, grads) def update_function(states, actions, y, yita): f_update(numpy.array(yita, dtype=theano.config.floatX)) return return update_function
def test_softmax(): from keras.activations import softmax as s # Test using a reference implementation of softmax def softmax(values): m = max(values) values = numpy.array(values) e = numpy.exp(values - m) dist = list(e / numpy.sum(e)) return dist x = T.vector() exp = s(x) f = theano.function([x], exp) test_values=get_standard_values() result = f(test_values) expected = softmax(test_values) print(str(result)) print(str(expected)) list_assert_equal(result, expected)
def test_relu(): ''' Relu implementation doesn't depend on the value being a theano variable. Testing ints, floats and theano tensors. ''' from keras.activations import relu as r assert r(5) == 5 assert r(-5) == 0 assert r(-0.1) == 0 assert r(0.1) == 0.1 x = T.vector() exp = r(x) f = theano.function([x], exp) test_values = get_standard_values() result = f(test_values) list_assert_equal(result, test_values) # because no negatives in test values
def test_tanh(): from keras.activations import tanh as t test_values = get_standard_values() x = T.vector() exp = t(x) f = theano.function([x], exp) result = f(test_values) expected = [math.tanh(v) for v in test_values] print(result) print(expected) list_assert_equal(result, expected)
def SquareError(x): """Square error loss function.""" if x.ndim == 1: y = tt.vector('y') L = tt.mean((x - y) ** 2) elif x.ndim == 2: y = tt.matrix('y') L = tt.mean(tt.sum((x - y) ** 2, axis=1)) else: raise ValueError('x must be either a vector or a matrix.') L.name = 'loss' return y, L
def CrossEntropy(x): """Cross entropy loss function. Only works for networks with one output.""" if x.ndim == 1: pass elif x.ndim == 2: x = x[:, 0] else: raise ValueError('x must be either a vector or a matrix.') y = tt.vector('y') L = -tt.mean(y * tt.log(x) + (1-y) * tt.log(1-x)) L.name = 'loss' return y, L
def Accuracy(x): """Accuracy loss function. Mainly useful for validation.""" if x.ndim == 1: pass elif x.ndim == 2: x = x.argmax(axis=1) else: raise ValueError('x must be either a vector or a matrix.') y = tt.vector('y') L = 100.0 * tt.mean(tt.eq(y, x)) L.name = 'loss' return y, L
def __init__(self, classifier, args): self.y = T.ivector('y') self.w = T.vector('w') if args.instance_weights_path: self.cost = classifier.negative_log_likelihood(self.y, self.w) else: self.cost = classifier.negative_log_likelihood(self.y) if args.L1_reg > 0: self.cost = self.cost + args.L1_reg * classifier.L1 if args.L2_reg > 0: self.cost = self.cost + args.L2_reg * classifier.L2_sqr if args.alpha and args.alpha > 0: self.cost = self.cost + args.alpha * classifier.log_Z_sqr self.test = ( T.mean(classifier.p_y_given_x(self.y)) )
def func_step_0(self, use_x0=False): """ Returns a Theano function. """ if use_x0: x0 = tensor.vector('x0') else: x0 = self.get('x0') Wout = self.get('Wout') bout = self.get('bout') r = self.f_hidden(x0) z = self.f_out(r.dot(Wout) + bout) args = [] if use_x0: args += [x0] return theano.function(args, [z, x0])
def get_update(Ws_s, bs_s): x, fx = train.get_model(Ws_s, bs_s) # Ground truth (who won) y = T.vector('y') # Compute loss (just log likelihood of a sigmoid fit) y_pred = sigmoid(fx) loss = -( y * T.log(y_pred) + (1 - y) * T.log(1 - y_pred)).mean() # Metrics on the number of correctly predicted ones frac_correct = ((fx > 0) * y + (fx < 0) * (1 - y)).mean() # Updates learning_rate_s = T.scalar(dtype=theano.config.floatX) momentum_s = T.scalar(dtype=theano.config.floatX) updates = train.nesterov_updates(loss, Ws_s + bs_s, learning_rate_s, momentum_s) f_update = theano.function( inputs=[x, y, learning_rate_s, momentum_s], outputs=[loss, frac_correct], updates=updates, ) return f_update
def score(self, x, y, w=None): '''Compute the mean accuracy on a set of labeled data. Parameters ---------- x : ndarray (num-examples, num-variables) An array containing examples to classify. Examples are given as the rows in this array. y : ndarray (num-examples, ) A vector of integer class labels, one for each row of input data. w : ndarray (num-examples, ) A vector of weights, one for each row of input data. Returns ------- score : float The (possibly weighted) mean accuracy of the model on the data. ''' eq = y == self.predict(x) if w is not None: return (w * eq).sum() / w.sum() return eq.mean()
def __init__(self): super(M, self).__init__() x = T.matrix('x') # input, target self.w = module.Member(T.matrix('w')) # weights self.a = module.Member(T.vector('a')) # hid bias self.b = module.Member(T.vector('b')) # output bias self.hid = T.tanh(T.dot(x, self.w) + self.a) hid = self.hid self.out = T.tanh(T.dot(hid, self.w.T) + self.b) out = self.out self.err = 0.5 * T.sum((out - x)**2) err = self.err params = [self.w, self.a, self.b] gparams = T.grad(err, params) updates = [(p, p - 0.01 * gp) for p, gp in zip(params, gparams)] self.step = module.Method([x], err, updates=dict(updates))
def test_gemm16_swap(): if nerv is None: raise SkipTest("nervanagpu not available") v = vector(dtype='float16') m = matrix(dtype='float16') m2 = matrix(dtype='float16') m32 = matrix(dtype='float32') # test that we don't try to replace anything but matrix x matrix in float16 f = function([v, m], dot(v, m), mode=mode_with_gpu) assert len([node for node in f.maker.fgraph.apply_nodes if isinstance(node.op, Gemm16)]) == 0 f = function([m32, m], dot(m32, m), mode=mode_with_gpu) assert len([node for node in f.maker.fgraph.apply_nodes if isinstance(node.op, Gemm16)]) == 0 f = function([m, m2], dot(m, m2), mode=mode_with_gpu) assert len([node for node in f.maker.fgraph.apply_nodes if isinstance(node.op, Gemm16)]) == 1
def test_multiple_outputs(self): m = tensor.matrix('m') v = tensor.vector('v') m_ = tensor.matrix('m_') v_ = tensor.vector('v_') mval = self.rng.uniform(size=(3, 7)).astype(theano.config.floatX) vval = self.rng.uniform(size=(7,)).astype(theano.config.floatX) m_val = self.rng.uniform(size=(3, 7)).astype(theano.config.floatX) v_val = self.rng.uniform(size=(7,)).astype(theano.config.floatX) rop_out1 = tensor.Rop([m, v, m + v], [m, v], [m_, v_]) assert isinstance(rop_out1, list) assert len(rop_out1) == 3 rop_out2 = tensor.Rop((m, v, m + v), [m, v], [m_, v_]) assert isinstance(rop_out2, tuple) assert len(rop_out2) == 3 all_outs = [] for o in rop_out1, rop_out2: all_outs.extend(o) f = theano.function([m, v, m_, v_], all_outs) f(mval, vval, m_val, v_val)
def test_local_csm_properties_csm(): data = tensor.vector() indices, indptr, shape = (tensor.ivector(), tensor.ivector(), tensor.ivector()) mode = theano.compile.mode.get_default_mode() mode = mode.including("specialize", "local_csm_properties_csm") for CS, cast in [(sparse.CSC, sp.csc_matrix), (sparse.CSR, sp.csr_matrix)]: f = theano.function([data, indices, indptr, shape], sparse.csm_properties( CS(data, indices, indptr, shape)), mode=mode) assert not any( isinstance(node.op, (sparse.CSM, sparse.CSMProperties)) for node in f.maker.fgraph.toposort()) v = cast(random_lil((10, 40), config.floatX, 3)) f(v.data, v.indices, v.indptr, v.shape)
def test_local_mul_s_v(): if not theano.config.cxx: raise SkipTest("G++ not available, so we need to skip this test.") mode = theano.compile.mode.get_default_mode() mode = mode.including("specialize", "local_mul_s_v") for sp_format in ['csr']: # Not implemented for other format inputs = [getattr(theano.sparse, sp_format + '_matrix')(), tensor.vector()] f = theano.function(inputs, sparse.mul_s_v(*inputs), mode=mode) assert not any(isinstance(node.op, sparse.MulSV) for node in f.maker.fgraph.toposort())
def test_local_structured_add_s_v(): if not theano.config.cxx: raise SkipTest("G++ not available, so we need to skip this test.") mode = theano.compile.mode.get_default_mode() mode = mode.including("specialize", "local_structured_add_s_v") for sp_format in ['csr']: # Not implemented for other format inputs = [getattr(theano.sparse, sp_format + '_matrix')(), tensor.vector()] f = theano.function(inputs, sparse.structured_add_s_v(*inputs), mode=mode) assert not any(isinstance(node.op, sparse.StructuredAddSV) for node in f.maker.fgraph.toposort())
def test_csm_grad(self): for sparsetype in ('csr', 'csc'): x = tensor.vector() y = tensor.ivector() z = tensor.ivector() s = tensor.ivector() call = getattr(sp, sparsetype + '_matrix') spm = call(random_lil((300, 400), config.floatX, 5)) out = tensor.grad(dense_from_sparse( CSM(sparsetype)(x, y, z, s) ).sum(), x) self._compile_and_check([x, y, z, s], [out], [spm.data, spm.indices, spm.indptr, spm.shape], (CSMGrad, CSMGradC) )
def test_csr_dense(self): x = theano.sparse.csr_matrix('x') y = theano.tensor.matrix('y') v = theano.tensor.vector('v') for (x, y, x_v, y_v) in [(x, y, self.x_csr, self.y), (x, v, self.x_csr, self.v_100), (v, x, self.v_10, self.x_csr)]: f_a = theano.function([x, y], theano.sparse.dot(x, y)) f_b = lambda x, y: x * y utt.assert_allclose(f_a(x_v, y_v), f_b(x_v, y_v)) # Test infer_shape self._compile_and_check([x, y], [theano.sparse.dot(x, y)], [x_v, y_v], (Dot, Usmm, UsmmCscDense))
def test_mul_s_v(self): sp_types = {'csc': sp.csc_matrix, 'csr': sp.csr_matrix} for format in ['csr', 'csc']: for dtype in ['float32', 'float64']: x = theano.sparse.SparseType(format, dtype=dtype)() y = tensor.vector(dtype=dtype) f = theano.function([x, y], mul_s_v(x, y)) spmat = sp_types[format](random_lil((4, 3), dtype, 3)) mat = numpy.asarray(numpy.random.rand(3), dtype=dtype) out = f(spmat, mat) utt.assert_allclose(spmat.toarray() * mat, out.toarray())
def test_structured_add_s_v(self): sp_types = {'csc': sp.csc_matrix, 'csr': sp.csr_matrix} for format in ['csr', 'csc']: for dtype in ['float32', 'float64']: x = theano.sparse.SparseType(format, dtype=dtype)() y = tensor.vector(dtype=dtype) f = theano.function([x, y], structured_add_s_v(x, y)) spmat = sp_types[format](random_lil((4, 3), dtype, 3)) spones = spmat.copy() spones.data = numpy.ones_like(spones.data) mat = numpy.asarray(numpy.random.rand(3), dtype=dtype) out = f(spmat, mat) utt.assert_allclose(as_ndarray(spones.multiply(spmat + mat)), out.toarray())
def setUp(self): self.test_vals = [numpy.array(x, dtype=config.floatX) for x in [ 0, 1, numpy.nan, numpy.inf, -numpy.inf, [numpy.nan, numpy.inf, -numpy.inf, 0, 1, -1], ]] self.scalar = tensor.scalar() self.vector = tensor.vector() self.mode = get_default_mode() if isinstance(self.mode, theano.compile.debugmode.DebugMode): # Disable the check preventing usage of NaN / Inf values. self.mode = copy(self.mode) self.mode.check_isfinite = False
def test_gt_grad(): """A user test that failed. Something about it made Elemwise.grad return something that was too complicated for get_scalar_constant_value to recognize as being 0, so gradient.grad reported that it was not a valid gradient of an integer. """ floatX = config.floatX T = theano.tensor input_ = T.vector(dtype=floatX) random_values = numpy.random.RandomState(1234).uniform( low=-1, high=1, size=(2, 2)) W_values = numpy.asarray(random_values, dtype=floatX) W = theano.shared(value=W_values, name='weights') correct_score = T.dot(input_, W) wrong_input = T.vector(dtype=floatX) wrong_score = theano.clone(correct_score, {input_: wrong_input}) # Hinge loss scores = T.ones_like(correct_score) - correct_score + wrong_score cost = (scores * (scores > 0)).sum() T.grad(cost, input_)
def test_recursive_lift(self): v = T.vector(dtype="float64") m = T.matrix(dtype="float64") out = ((v + 42) * (m + 84)).T g = FunctionGraph([v, m], [out]) init_str_g = ("[InplaceDimShuffle{1,0}(Elemwise{mul,no_inplace}" "(InplaceDimShuffle{x,0}(Elemwise{add,no_inplace}" "(<TensorType(float64, vector)>, " "InplaceDimShuffle{x}(TensorConstant{42}))), " "Elemwise{add,no_inplace}" "(<TensorType(float64, matrix)>, " "InplaceDimShuffle{x,x}(TensorConstant{84}))))]") self.assertTrue(str(g) == init_str_g) new_out = local_dimshuffle_lift.transform(g.outputs[0].owner)[0] new_g = FunctionGraph(g.inputs, [new_out]) opt_str_g = ("[Elemwise{mul,no_inplace}(Elemwise{add,no_inplace}" "(InplaceDimShuffle{0,x}(<TensorType(float64, vector)>), " "InplaceDimShuffle{x,x}(TensorConstant{42})), " "Elemwise{add,no_inplace}(InplaceDimShuffle{1,0}" "(<TensorType(float64, matrix)>), " "InplaceDimShuffle{x,x}(TensorConstant{84})))]") self.assertTrue(str(new_g) == opt_str_g) # Check stacktrace was copied over correctly after opt was applied self.assertTrue(check_stack_trace(new_g, ops_to_check='all'))
def test4(self): # basic test that the optimization doesn't work with broadcasting # ... It *could* be extended to, # ... but right now it doesn't, so it shouldn't try. x = tensor.matrix('x') y = tensor.vector('y') f = function([x, y], tensor.exp(x + y)[0], mode=mode_opt) # Opt doesn't apply, so no need for check_stack_trace # self.assertTrue(check_stack_trace(f, ops_to_check='all')) prog = f.maker.fgraph.toposort() assert isinstance(prog[0].op, tensor.DimShuffle) assert prog[1].op == tensor.add assert isinstance(prog[2].op, tensor.Subtensor) # first subtensor assert prog[3].op == inplace.exp_inplace assert len(prog) == 4 f([[0, 1], [2, 3]], [4, 5]) # let debugmode test something
def test5(self): # test that we don't lift when we reuse the output of the # elemwise for other computation. x = tensor.matrix('x') y = tensor.vector('y') f = function([x, y], [tensor.exp(x + y)[0], tensor.exp(x + y) + x], mode=mode_opt) # Opt doesn't apply, so no need for check_stack_trace # self.assertTrue(check_stack_trace(f, ops_to_check=Subtensor)) prog = f.maker.fgraph.toposort() assert isinstance(prog[0].op, tensor.DimShuffle) assert isinstance(prog[1].op.scalar_op, theano.scalar. Composite) # Composite{add,exp} assert prog[2].op == tensor.add assert isinstance(prog[3].op, tensor.Subtensor) # first subtensor assert len(prog) == 4 f([[0, 1], [2, 3]], [4, 5]) # let debugmode test something
def test6(self): # basic test that the optimization works with a scalar as input, # and a scalar as output (no broadcasting of the scalar needed). # The optimization used to fail and display an ERROR message. x = tensor.vector('x') y = tensor.scalar('y') f = function([x, y], tensor.exp(x + y)[0], mode=mode_opt) # Check stacktrace was copied over correctly after opt was applied self.assertTrue(check_stack_trace(f, ops_to_check=Subtensor)) prog = f.maker.fgraph.toposort() assert isinstance(prog[0].op, tensor.Subtensor) # Composite{add,exp} assert isinstance(prog[1].op.scalar_op, theano.scalar.Composite) assert len(prog) == 2 f([1, 2, 3], 4) # let debugmode test something
def test_local_one_plus_erf(self): val = numpy.asarray([-30, -3, -2, -1, 0, 1, 2, 3, 30], dtype=config.floatX) x = T.vector() f = theano.function([x], 1 + T.erf(x), mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [ T.mul, T.erfc], f.maker.fgraph.toposort() f(val) f = theano.function([x], T.erf(x) + 1, mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [ T.mul, T.erfc], f.maker.fgraph.toposort() f(val) f = theano.function([x], T.erf(x) + 2, mode=self.mode) topo = f.maker.fgraph.toposort() assert len(topo) == 2 assert topo[0].op == T.erf assert isinstance(topo[1].op, T.Elemwise) assert isinstance(topo[1].op.scalar_op, scal.Add) f(val)
def test_local_erf_minus_one(self): val = numpy.asarray([-30, -3, -2, -1, 0, 1, 2, 3, 30], dtype=config.floatX) x = T.vector() f = theano.function([x], T.erf(x) - 1, mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [T.erfc, T.mul] print(f(val)) f = theano.function([x], T.erf(x) + (-1), mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [T.erfc, T.mul] print(f(val)) f = theano.function([x], -1 + T.erf(x), mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [T.erfc, T.mul] print(f(val)) f = theano.function([x], T.erf(x) - 2, mode=self.mode) topo = f.maker.fgraph.toposort() assert len(topo) == 2 assert topo[0].op == T.erf assert isinstance(topo[1].op, T.Elemwise) assert isinstance(topo[1].op.scalar_op, scal.Add)\ or isinstance(topo[1].op.scalar_op, scal.Sub) print(f(val))
def test_local_one_minus_erfc(self): """ test opt: 1-erfc(x) => erf(x) and -erfc(x)+1 => erf(x) """ val = numpy.asarray([-30, -3, -2, -1, 0, 1, 2, 3, 30], dtype=config.floatX) x = T.vector('x') f = theano.function([x], 1 - T.erfc(x), mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [T.erf]\ , f.maker.fgraph.toposort() print(f(val)) f = theano.function([x], (-T.erfc(x)) + 1, mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [T.erf]\ , f.maker.fgraph.toposort() print(f(val)) f = theano.function([x], 2 - T.erfc(x), mode=self.mode) topo = f.maker.fgraph.toposort() assert len(topo) == 2, f.maker.fgraph.toposort() assert topo[0].op == T.erfc, f.maker.fgraph.toposort() assert isinstance(topo[1].op, T.Elemwise), f.maker.fgraph.toposort() assert isinstance(topo[1].op.scalar_op, scal.Sub)\ , f.maker.fgraph.toposort() print(f(val))
def test_local_erf_neg_minus_one(self): """ test opt: (-1)+erfc(-x)=>erf(x)""" val = numpy.asarray([-30, -3, -2, -1, 0, 1, 2, 3, 30], dtype=config.floatX) x = T.vector('x') f = theano.function([x], -1 + T.erfc(-x), mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [T.erf]\ , f.maker.fgraph.toposort() print(f(val)) f = theano.function([x], T.erfc(-x) - 1, mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [T.erf]\ , f.maker.fgraph.toposort() print(f(val)) f = theano.function([x], T.erfc(-x) + (-1), mode=self.mode) assert [n.op for n in f.maker.fgraph.toposort()] == [T.erf]\ , f.maker.fgraph.toposort() print(f(val))
def speed_local_log_erfc(self): val = numpy.random.rand(1e6) x = T.vector() mode = theano.compile.mode.get_mode("FAST_RUN") f1 = theano.function([x], T.log(T.erfc(x)), mode=mode. excluding("local_log_erfc")) f2 = theano.function([x], T.log(T.erfc(x)), mode=mode) print(f1.maker.fgraph.toposort()) print(f2.maker.fgraph.toposort()) t0 = time.time() f1(val) t1 = time.time() f2(val) t2 = time.time() print(t1 - t0, t2 - t1)
def test_broadcast1(self): # test switch(cst, matrix, row) x = theano.tensor.matrix('x', dtype='int32') y = theano.tensor.vector('y', dtype='int64') z = theano.tensor.switch(1, x, y) f = theano.function([x, y], z, mode=self.mode) assert len([node.op for node in f.maker.fgraph.toposort() if isinstance(node.op, theano.tensor.Elemwise) and not isinstance(node.op.scalar_op, theano.scalar.basic.Cast)]) == 0 vx = numpy.array([[1, 2, 3], [4, 5, 6]], dtype='int32') vy = numpy.array([10, 11, 12], dtype='int64') assert numpy.all(f(vx, vy) == vx) z = theano.tensor.switch(0, x, y) f = theano.function([x, y], z, mode=self.mode) assert len([node.op for node in f.maker.fgraph.toposort() if isinstance(node.op, theano.tensor.Elemwise)]) == 0 vx = numpy.array([[1, 2, 3], [4, 5, 6]], dtype='int32') vy = numpy.array([10, 11, 12], dtype='int64') assert numpy.all(f(vx, vy) == vy)
def test_broadcast2(self): # test switch(cst, vector, matrix) # This case is not optimized for now. x = theano.tensor.vector('x', dtype='int32') y = theano.tensor.matrix('y', dtype='int64') z = theano.tensor.switch(1, x, y) f = theano.function([x, y], z, mode=self.mode) assert len([node.op for node in f.maker.fgraph.toposort() if isinstance(node.op, theano.tensor.Elemwise) and not isinstance(node.op.scalar_op, theano.scalar.basic.Cast)]) == 0 vx = numpy.array([4, 5, 6], dtype='int32') vy = numpy.array([[7, 8, 9], [10, 11, 12]], dtype='int64') assert numpy.all(f(vx, vy) == vx) z = theano.tensor.switch(0, x, y) f = theano.function([x, y], z, mode=self.mode) assert len([node.op for node in f.maker.fgraph.toposort() if isinstance(node.op, theano.tensor.Elemwise)]) == 0 vx = numpy.array([4, 5, 6], dtype='int32') vy = numpy.array([[7, 8, 9], [10, 11, 12]], dtype='int64') assert numpy.all(f(vx, vy) == vy)
def test_local_join_make_vector(): a, b, c, d, e = tensor.scalars('abcde') v = tensor.vector('v') mv = MakeVector(config.floatX) s = tensor.join(0, mv(a), v, mv(b, c), mv(d, e)) f = function([a, b, c, d, e, v], s, mode=mode_opt) theano.printing.debugprint(f) val = f(1, 2, 3, 4, 6, [7, 8]) assert numpy.all(val == [1, 7, 8, 2, 3, 4, 6]) e = f.maker.fgraph.toposort() assert len([n for n in e if isinstance(n.op, Join)]) == 1 assert all([not isinstance(n.op, Join) or len(n.inputs) == 4 for n in e if isinstance(n.op, Join)]) assert f.maker.fgraph.outputs[0].dtype == config.floatX assert check_stack_trace(f, ops_to_check='all')
def test_local_add_specialize(): # test of non-zero dimension a = tensor.vector() s = tensor.add(tensor.zeros_like(a)) assert local_add_specialize.transform(s.owner) # test of 0-d a = tensor.scalar() s = tensor.add(tensor.zeros_like(a)) assert local_add_specialize.transform(s.owner) # Test when the 0 input is forcing upcasting a = tensor.constant(0, dtype='int64') b = tensor.constant(1, dtype='int32') s = a + b transformed = local_add_specialize.transform(s.owner) assert transformed assert transformed[0].type == s.type
def ndim_tensor(ndim): if ndim == 1: return T.vector() elif ndim == 2: return T.matrix() elif ndim == 3: return T.tensor3() elif ndim == 4: return T.tensor4() return T.matrix() # get int32 tensor
def build_model(tparams, leavesList, ancestorsList, options): dropoutRate = options['dropoutRate'] trng = RandomStreams(123) use_noise = theano.shared(numpy_floatX(0.)) x = T.tensor3('x', dtype=config.floatX) y = T.tensor3('y', dtype=config.floatX) mask = T.matrix('mask', dtype=config.floatX) lengths = T.vector('lengths', dtype=config.floatX) n_timesteps = x.shape[0] n_samples = x.shape[1] embList = [] for leaves, ancestors in zip(leavesList, ancestorsList): tempAttention = generate_attention(tparams, leaves, ancestors) tempEmb = (tparams['W_emb'][ancestors] * tempAttention[:,:,None]).sum(axis=1) embList.append(tempEmb) emb = T.concatenate(embList, axis=0) x_emb = T.tanh(T.dot(x, emb)) hidden = gru_layer(tparams, x_emb, options) hidden = dropout_layer(hidden, use_noise, trng, dropoutRate) y_hat = softmax_layer(tparams, hidden) * mask[:,:,None] logEps = 1e-8 cross_entropy = -(y * T.log(y_hat + logEps) + (1. - y) * T.log(1. - y_hat + logEps)) output_loglikelihood = cross_entropy.sum(axis=2).sum(axis=0) / lengths cost_noreg = T.mean(output_loglikelihood) if options['L2'] > 0.: cost = cost_noreg + options['L2'] * ((tparams['W_output']**2).sum() + (tparams['W_attention']**2).sum() + (tparams['v_attention']**2).sum()) return use_noise, x, y, mask, lengths, cost, cost_noreg, y_hat
def build_model(tparams, options): weightVector = T.vector('weightVector', dtype=theano.config.floatX) iVector = T.vector('iVector', dtype='int32') jVector = T.vector('jVector', dtype='int32') cost = weightVector * (((tparams['w'][iVector] * tparams['w_tilde'][jVector]).sum(axis=1) + tparams['b'][iVector] + tparams['b_tilde'][jVector] - T.log(weightVector)) ** 2) return weightVector, iVector, jVector, cost.sum()