本文整理匯總了Python中theano.tensor.imatrix方法的典型用法代碼示例。如果您正苦於以下問題:Python tensor.imatrix方法的具體用法?Python tensor.imatrix怎麽用?Python tensor.imatrix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類theano.tensor
的用法示例。
在下文中一共展示了tensor.imatrix方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _make_rnn
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def _make_rnn(self, seq_length=4):
self.embedding_dim = embedding_dim = 3
self.vocab_size = vocab_size = 10
self.seq_length = seq_length
def compose_network(h_prev, inp, embedding_dim, model_dim, vs, name="compose"):
# Just add the two embeddings!
W = T.concatenate([T.eye(model_dim), T.eye(model_dim)], axis=0)
i = T.concatenate([h_prev, inp], axis=1)
return i.dot(W)
X = T.imatrix("X")
training_mode = T.scalar("training_mode")
vs = VariableStore()
embeddings = np.arange(vocab_size).reshape(
(vocab_size, 1)).repeat(embedding_dim, axis=1)
self.model = RNN(
embedding_dim, embedding_dim, vocab_size, seq_length, compose_network,
IdentityLayer, training_mode, None, vs,
X=X, make_test_fn=True, initial_embeddings=embeddings)
示例2: setUp
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def setUp(self):
if 'gpu' not in theano.config.device:
raise RuntimeError("Thin stack only defined for GPU usage")
self.embedding_dim = self.model_dim = 2
self.vocab_size = 5
self.batch_size = 2
self.num_classes = 2
self.vs = VariableStore()
self.compose_network = util.TreeLSTMLayer
self.embedding_proj = IdentityLayer
self.skip_embeddings = False
self.X = T.imatrix("X")
self.transitions = T.imatrix("transitions")
self.y = T.ivector("y")
示例3: create
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def create(self):
self._input_name = 'text'
self._output_name = 'output'
self.add_input(
name=self._input_name,
input_shape=(self._config.max_input_time_steps, self._config.input_dim,))
self.inputs['text'].input = T.imatrix()
self.add_node(Embedding(
self._config.input_dim,
self._config.textual_embedding_dim,
mask_zero=True),
name='embedding', input='text')
self.add_node(
self._config.recurrent_encoder(
self._config.hidden_state_dim,
return_sequences=False,
go_backwards=self._config.go_backwards),
name='recurrent', input='embedding')
self.add_node(Dropout(0.5), name='dropout', input='recurrent')
self.add_node(Dense(self._config.output_dim), name='dense', input='dropout')
self.add_node(Activation('softmax'), name='softmax', input='dense')
self.add_output(name=self._output_name, input='softmax')
示例4: __init__
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def __init__(self, input_dim, proj_dim=128,
init='uniform', activation='sigmoid', weights=None):
super(WordContextProduct, self).__init__()
self.input_dim = input_dim
self.proj_dim = proj_dim
self.init = initializations.get(init)
self.activation = activations.get(activation)
self.input = T.imatrix()
# two different embeddings for pivot word and its context
# because p(w|c) != p(c|w)
self.W_w = self.init((input_dim, proj_dim))
self.W_c = self.init((input_dim, proj_dim))
self.params = [self.W_w, self.W_c]
if weights is not None:
self.set_weights(weights)
示例5: add_input
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def add_input(self, name, ndim=2, dtype='float'):
if name in self.namespace:
raise Exception('Duplicate node identifier: ' + name)
self.namespace.add(name)
self.input_order.append(name)
layer = Layer() # empty layer
if dtype == 'float':
layer.input = ndim_tensor(ndim)
else:
if ndim == 2:
layer.input = T.imatrix()
else:
raise Exception('Type "int" can only be used with ndim==2.')
layer.input.name = name
self.inputs[name] = layer
self.input_config.append({'name': name, 'ndim': ndim, 'dtype': dtype})
示例6: test_shape
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def test_shape():
input_var = T.tensor3('input')
target_var = T.imatrix('target')
output_var, _, _ = memory_augmented_neural_network(
input_var, target_var,
batch_size=16,
nb_class=5,
memory_shape=(128, 40),
controller_size=200,
input_size=20 * 20,
nb_reads=4)
posterior_fn = theano.function([input_var, target_var], output_var)
test_input = np.random.rand(16, 50, 20 * 20)
test_target = np.random.randint(5, size=(16, 50)).astype('int32')
test_input_invalid_batch_size = np.random.rand(16 + 1, 50, 20 * 20)
test_input_invalid_depth = np.random.rand(16, 50, 20 * 20 - 1)
test_output = posterior_fn(test_input, test_target)
assert test_output.shape == (16, 50, 5)
with pytest.raises(ValueError) as e_info:
posterior_fn(test_input_invalid_batch_size, test_target)
with pytest.raises(ValueError) as e_info:
posterior_fn(test_input_invalid_depth, test_target)
示例7: test_sparseblockdot
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def test_sparseblockdot(self):
"""
Compares the numpy version of sparseblockgemv to sparse_block_dot.
"""
b = tensor.fmatrix()
W = tensor.ftensor4()
h = tensor.ftensor3()
iIdx = tensor.imatrix()
oIdx = tensor.imatrix()
o = sparse_block_dot(W, h, iIdx, b, oIdx)
f = theano.function([W, h, iIdx, b, oIdx], o, mode=self.mode)
W_val, h_val, iIdx_val, b_val, oIdx_val = \
BlockSparse_Gemv_and_Outer.gemv_data()
th_out = f(W_val, h_val, iIdx_val, b_val, oIdx_val)
ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy(
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
utt.assert_allclose(ref_out, th_out)
示例8: test_sparseblockgemv
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def test_sparseblockgemv(self):
"""
Compares the numpy and theano versions of sparseblockgemv.
"""
b = tensor.fmatrix()
W = tensor.ftensor4()
h = tensor.ftensor3()
iIdx = tensor.imatrix()
oIdx = tensor.imatrix()
o = self.gemv_op(b.take(oIdx, axis=0), W, h, iIdx, oIdx)
f = theano.function([W, h, iIdx, b, oIdx], o, mode=self.mode)
W_val, h_val, iIdx_val, b_val, oIdx_val = \
BlockSparse_Gemv_and_Outer.gemv_data()
th_out = f(W_val, h_val, iIdx_val, b_val, oIdx_val)
ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy(
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
utt.assert_allclose(ref_out, th_out)
示例9: test_sparseblockgemv_grad_shape
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def test_sparseblockgemv_grad_shape(self):
b = tensor.fmatrix()
W = tensor.ftensor4()
h = tensor.ftensor3()
iIdx = tensor.imatrix()
oIdx = tensor.imatrix()
o = self.gemv_op(b.take(oIdx, axis=0), W, h, iIdx, oIdx)
go = theano.grad(o.sum(), [b, W, h])
f = theano.function([W, h, iIdx, b, oIdx], go, mode=self.mode)
W_val, h_val, iIdx_val, b_val, oIdx_val = \
BlockSparse_Gemv_and_Outer.gemv_data()
# just make sure that it runs correcly and all the shapes are ok.
b_g, W_g, h_g = f(W_val, h_val, iIdx_val, b_val, oIdx_val)
assert b_g.shape == b_val.shape
assert h_g.shape == h_val.shape
assert W_g.shape == W_val.shape
示例10: test_sparseblockouter
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def test_sparseblockouter(self):
o = tensor.ftensor4()
x = tensor.ftensor3()
y = tensor.ftensor3()
xIdx = tensor.imatrix()
yIdx = tensor.imatrix()
out = self.outer_op(o, x, y, xIdx, yIdx)
f = theano.function([o, x, y, xIdx, yIdx], out,
on_unused_input="warn", mode=self.mode)
o_val, x_val, y_val, xIdx_val, yIdx_val = \
BlockSparse_Gemv_and_Outer.outer_data()
th_out = f(o_val, x_val, y_val, xIdx_val, yIdx_val)
ref_out = BlockSparse_Gemv_and_Outer.outer_numpy(
o_val, x_val, y_val, xIdx_val, yIdx_val)
utt.assert_allclose(ref_out, th_out)
示例11: test
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def test():
energies_var = T.tensor4('energies', dtype=theano.config.floatX)
targets_var = T.imatrix('targets')
masks_var = T.matrix('masks', dtype=theano.config.floatX)
layer_input = lasagne.layers.InputLayer([2, 2, 3, 3], input_var=energies_var)
out = lasagne.layers.get_output(layer_input)
loss = crf_loss(out, targets_var, masks_var)
prediction, acc = crf_accuracy(energies_var, targets_var)
fn = theano.function([energies_var, targets_var, masks_var], [loss, prediction, acc])
energies = np.array([[[[10, 15, 20], [5, 10, 15], [3, 2, 0]], [[5, 10, 1], [5, 10, 1], [5, 10, 1]]],
[[[5, 6, 7], [2, 3, 4], [2, 1, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]], dtype=np.float32)
targets = np.array([[0, 1], [0, 2]], dtype=np.int32)
masks = np.array([[1, 1], [1, 0]], dtype=np.float32)
l, p, a = fn(energies, targets, masks)
print l
print p
print a
示例12: __init__
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def __init__(self, id, data, hp):
self.type = 'LM'
self.id = id
self.filename = 'savedmodels\model_'+id+'.pkl'
self.hp = hp
self.X = T.imatrix()
self.Y = T.ivector()
self.seed_idx = T.iscalar()
self.X.tag.test_value = np.random.randn(hp.seq_size, hp.batch_size).astype(dtype=np.int32)
self.data = copy.copy(data)
for key in ('tr_X', 'va_X', 'te_X', 'tr_Y', 'va_Y', 'te_Y'):
if key in self.data:
self.data['len_'+key] = len(self.data[key])
self.data[key] = shared(self.data[key], borrow=True, dtype=np.int32)
if hp['debug']:
theano.config.optimizer = 'None'
theano.config.compute_test_value = 'ignore'
theano.config.exception_verbosity = 'high'
示例13: build_network_from_ae
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def build_network_from_ae(classn, fea_len):
input_var = T.tensor4('inputs');
aug_var = T.matrix('aug_var');
fea_var = T.matrix('fea_var');
target_var = T.imatrix('targets');
ae = pickle.load(open('model/conv_ae.pkl', 'rb'));
input_layer_index = map(lambda pair : pair[0], ae.layers).index('input');
first_layer = ae.get_all_layers()[input_layer_index + 1];
input_layer = layers.InputLayer(shape=(None, 3, 32, 32), input_var = input_var);
first_layer.input_layer = input_layer;
encode_layer_index = map(lambda pair : pair[0], ae.layers).index('encode_layer');
encode_layer = ae.get_all_layers()[encode_layer_index];
aug_layer = layers.InputLayer(shape=(None, classn), input_var = aug_var);
fea_layer = layers.InputLayer(shape=(None, fea_len), input_var = fea_var);
cat_layer = lasagne.layers.ConcatLayer([encode_layer, aug_layer, fea_layer], axis = 1);
hidden_layer = layers.DenseLayer(incoming = cat_layer, num_units = 100, nonlinearity = rectify);
network = layers.DenseLayer(incoming = hidden_layer, num_units = classn, nonlinearity = sigmoid);
return network, encode_layer, input_var, aug_var, fea_var, target_var;
示例14: build_network_from_ae
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import imatrix [as 別名]
def build_network_from_ae(classn):
input_var = T.tensor4('inputs');
aug_var = T.matrix('aug_var');
target_var = T.imatrix('targets');
ae = pickle.load(open('model/conv_ae.pkl', 'rb'));
input_layer_index = map(lambda pair : pair[0], ae.layers).index('input');
first_layer = ae.get_all_layers()[input_layer_index + 1];
input_layer = layers.InputLayer(shape=(None, 3, 32, 32), input_var = input_var);
first_layer.input_layer = input_layer;
encode_layer_index = map(lambda pair : pair[0], ae.layers).index('encode_layer');
encode_layer = ae.get_all_layers()[encode_layer_index];
aug_layer = layers.InputLayer(shape=(None, classn), input_var = aug_var);
cat_layer = lasagne.layers.ConcatLayer([encode_layer, aug_layer], axis = 1);
hidden_layer = layers.DenseLayer(incoming = cat_layer, num_units = 100, nonlinearity = rectify);
network = layers.DenseLayer(incoming = hidden_layer, num_units = classn, nonlinearity = sigmoid);
return network, encode_layer, input_var, aug_var, target_var;