本文整理汇总了Python中theano.tensor.bmatrix方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.bmatrix方法的具体用法?Python tensor.bmatrix怎么用?Python tensor.bmatrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.bmatrix方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_all_grad
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import bmatrix [as 别名]
def test_all_grad(self):
x = tensor.bmatrix('x')
x_all = x.all()
gx = theano.grad(x_all, x)
f = theano.function([x], gx)
x_random = self.rng.binomial(n=1, p=0.5, size=(5, 7)).astype('int8')
for x_val in (x_random,
numpy.zeros_like(x_random),
numpy.ones_like(x_random)):
gx_val = f(x_val)
assert gx_val.shape == x_val.shape
assert numpy.all(gx_val == 0)
示例2: test_any_grad
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import bmatrix [as 别名]
def test_any_grad(self):
x = tensor.bmatrix('x')
x_all = x.any()
gx = theano.grad(x_all, x)
f = theano.function([x], gx)
x_random = self.rng.binomial(n=1, p=0.5, size=(5, 7)).astype('int8')
for x_val in (x_random,
numpy.zeros_like(x_random),
numpy.ones_like(x_random)):
gx_val = f(x_val)
assert gx_val.shape == x_val.shape
assert numpy.all(gx_val == 0)
示例3: make_node
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import bmatrix [as 别名]
def make_node(self, state, time):
state = T.as_tensor_variable(state)
time = T.as_tensor_variable(time)
return theano.Apply(self, [state, time], [T.bmatrix()])
# Python implementation:
示例4: BuildModel
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import bmatrix [as 别名]
def BuildModel(modelSpecs, forTrain=True):
rng = np.random.RandomState()
## x is for sequential features and y for matrix (or pairwise) features
x = T.tensor3('x')
y = T.tensor4('y')
## mask for x and y, respectively
xmask = T.bmatrix('xmask')
ymask = T.btensor3('ymask')
xem = None
##if any( k in modelSpecs['seq2matrixMode'] for k in ('SeqOnly', 'Seq+SS') ):
if config.EmbeddingUsed(modelSpecs):
xem = T.tensor3('xem')
distancePredictor = ResNet4DistMatrix( rng, seqInput=x, matrixInput=y, mask_seq=xmask, mask_matrix=ymask, embedInput=xem, modelSpecs=modelSpecs )
else:
distancePredictor = ResNet4DistMatrix( rng, seqInput=x, matrixInput=y, mask_seq=xmask, mask_matrix=ymask, modelSpecs=modelSpecs )
## labelList is a list of label tensors, each having shape (batchSize, seqLen, seqLen) or (batchSize, seqLen, seqLen, valueDims[response] )
labelList = []
if forTrain:
## when this model is used for training. We need to define the label variable
for response in modelSpecs['responses']:
labelType = Response2LabelType(response)
rValDims = config.responseValueDims[labelType]
if labelType.startswith('Discrete'):
if rValDims > 1:
## if one response is a vector, then we use a 4-d tensor
## wtensor is for 16bit integer
labelList.append( T.wtensor4('Tlabel4' + response ) )
else:
labelList.append( T.wtensor3('Tlabel4' + response ) )
else:
if rValDims > 1:
labelList.append( T.tensor4('Tlabel4' + response ) )
else:
labelList.append( T.tensor3('Tlabel4' + response ) )
## weightList is a list of label weight tensors, each having shape (batchSize, seqLen, seqLen)
weightList = []
if len(labelList)>0 and modelSpecs['UseSampleWeight']:
weightList = [ T.tensor3('Tweight4'+response) for response in modelSpecs['responses'] ]
## for prediction, both labelList and weightList are empty
return distancePredictor, x, y, xmask, ymask, xem, labelList, weightList
示例5: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import bmatrix [as 别名]
def __init__(self, nin, nout, nhid, numpy_rng, scale=1.0):
self.nin = nin
self.nout = nout
self.nhid = nhid
self.numpy_rng = numpy_rng
self.theano_rng = RandomStreams(1)
self.scale = np.float32(scale)
self.inputs = T.fmatrix('inputs')
self.targets = T.imatrix('targets')
self.masks = T.bmatrix('masks')
self.batchsize = self.inputs.shape[0]
self.inputs_frames = self.inputs.reshape((
self.batchsize, self.inputs.shape[1]/nin, nin)).dimshuffle(1,0,2)
self.targets_frames = self.targets.T
self.masks_frames = self.masks.T
self.win = theano.shared(value=self.numpy_rng.normal(
loc=0, scale=0.001, size=(nin, nhid)
).astype(theano.config.floatX), name='win')
self.wrnn = theano.shared(value=self.scale * np.eye(
nhid, dtype=theano.config.floatX), name='wrnn')
self.wout = theano.shared(value=self.numpy_rng.uniform(
low=-0.01, high=0.01, size=(nhid, nout)
).astype(theano.config.floatX), name='wout')
self.bout = theano.shared(value=np.zeros(
nout, dtype=theano.config.floatX), name='bout')
self.params = [self.win, self.wrnn, self.wout, self.bout]
(self.hiddens, self.outputs), self.updates = theano.scan(
fn=self.step, sequences=self.inputs_frames,
outputs_info=[self.theano_rng.uniform(low=0, high=1, size=(
self.batchsize, nhid), dtype=theano.config.floatX), None])
self.probabilities = T.nnet.softmax(self.outputs.reshape((
self.outputs.shape[0] * self.outputs.shape[1],
self.nout)))
self.probabilities = T.clip(self.probabilities, 1e-6, 1-1e-6)
self._stepcosts = T.nnet.categorical_crossentropy(
self.probabilities, self.targets_frames.flatten()).reshape(
self.targets_frames.shape)
self._cost = T.switch(T.gt(self.masks_frames, 0), self._stepcosts, 0).mean()
self._grads = T.grad(self._cost, self.params)
self.get_classifications = theano.function(
[self.inputs], T.argmax(self.probabilities.reshape(self.outputs.shape), axis=2).T)
示例6: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import bmatrix [as 别名]
def __init__(self, K, vocab_size, num_chars, W_init,
nhidden, embed_dim, dropout, train_emb, char_dim, use_feat, gating_fn,
save_attn=False):
self.nhidden = nhidden
self.embed_dim = embed_dim
self.dropout = dropout
self.train_emb = train_emb
self.char_dim = char_dim
self.learning_rate = LEARNING_RATE
self.num_chars = num_chars
self.use_feat = use_feat
self.save_attn = save_attn
self.gating_fn = gating_fn
self.use_chars = self.char_dim!=0
if W_init is None: W_init = lasagne.init.GlorotNormal().sample((vocab_size, self.embed_dim))
doc_var, query_var, cand_var = T.itensor3('doc'), T.itensor3('quer'), \
T.wtensor3('cand')
docmask_var, qmask_var, candmask_var = T.bmatrix('doc_mask'), T.bmatrix('q_mask'), \
T.bmatrix('c_mask')
target_var = T.ivector('ans')
feat_var = T.imatrix('feat')
doc_toks, qry_toks= T.imatrix('dchars'), T.imatrix('qchars')
tok_var, tok_mask = T.imatrix('tok'), T.bmatrix('tok_mask')
cloze_var = T.ivector('cloze')
self.inps = [doc_var, doc_toks, query_var, qry_toks, cand_var, target_var, docmask_var,
qmask_var, tok_var, tok_mask, candmask_var, feat_var, cloze_var]
self.predicted_probs, predicted_probs_val, self.network, W_emb, attentions = (
self.build_network(K, vocab_size, W_init))
self.loss_fn = T.nnet.categorical_crossentropy(self.predicted_probs, target_var).mean()
self.eval_fn = lasagne.objectives.categorical_accuracy(self.predicted_probs,
target_var).mean()
loss_fn_val = T.nnet.categorical_crossentropy(predicted_probs_val, target_var).mean()
eval_fn_val = lasagne.objectives.categorical_accuracy(predicted_probs_val,
target_var).mean()
self.params = L.get_all_params(self.network, trainable=True)
updates = lasagne.updates.adam(self.loss_fn, self.params, learning_rate=self.learning_rate)
self.train_fn = theano.function(self.inps,
[self.loss_fn, self.eval_fn, self.predicted_probs],
updates=updates,
on_unused_input='warn')
self.validate_fn = theano.function(self.inps,
[loss_fn_val, eval_fn_val, predicted_probs_val]+attentions,
on_unused_input='warn')
示例7: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import bmatrix [as 别名]
def __init__(self, nin, nout, nhid, numpy_rng, scale=1.0):
self.nin = nin
self.nout = nout
self.nhid = nhid
self.numpy_rng = numpy_rng
self.scale = np.float32(scale)
self.inputs = T.fmatrix('inputs')
self.inputs.tag.test_value = numpy_rng.uniform(
low=-1., high=1.,
size=(16, 5 * self.nin)
).astype(np.float32)
self.targets = T.fmatrix('targets')
self.targets.tag.test_value = np.ones(
(16, 5 * nout), dtype=np.float32)
self.masks = T.bmatrix('masks')
self.masks.tag.test_value = np.ones(
(16, 5), dtype=np.int8)
self.batchsize = self.inputs.shape[0]
self.inputs_frames = self.inputs.reshape((
self.batchsize, self.inputs.shape[1] / nin,
nin)).dimshuffle(1, 0, 2)
self.targets_frames = self.targets.reshape((
self.batchsize, self.targets.shape[1] / nout,
nout)).dimshuffle(1, 0, 2)
self.masks_frames = self.masks.T
self.h0 = theano.shared(value=np.ones(
nhid, dtype=theano.config.floatX) * np.float32(.5), name='h0')
self.win = theano.shared(value=self.numpy_rng.normal(
loc=0, scale=0.001, size=(nin, nhid)
).astype(theano.config.floatX), name='win')
self.wrnn = theano.shared(value=self.scale * np.eye(
nhid, dtype=theano.config.floatX), name='wrnn')
self.wout = theano.shared(value=self.numpy_rng.uniform(
low=-0.01, high=0.01, size=(nhid, nout)
).astype(theano.config.floatX), name='wout')
self.bout = theano.shared(value=np.zeros(
nout, dtype=theano.config.floatX), name='bout')
self.params = [self.win, self.wrnn, self.wout, self.bout]
(self.hiddens, self.outputs), self.updates = theano.scan(
fn=self.step, sequences=self.inputs_frames,
outputs_info=[T.alloc(
self.h0, self.batchsize, self.nhid), None])
self._stepcosts = T.sum((self.targets_frames - self.outputs)**2, axis=2)
self._cost = T.switch(self.masks_frames > 0, self._stepcosts, 0).mean()
self._grads = T.grad(self._cost, self.params)
self.getoutputs = theano.function(
[self.inputs], self.outputs)