本文整理汇总了Python中theano.tensor.ivector函数的典型用法代码示例。如果您正苦于以下问题:Python ivector函数的具体用法?Python ivector怎么用?Python ivector使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ivector函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: predict_next_batch
def predict_next_batch(self, session_ids, input_item_ids, predict_for_item_ids=None, batch=100):
'''
Gives predicton scores for a selected set of items. Can be used in batch mode to predict for multiple independent events (i.e. events of different sessions) at once and thus speed up evaluation.
If the session ID at a given coordinate of the session_ids parameter remains the same during subsequent calls of the function, the corresponding hidden state of the network will be kept intact (i.e. that's how one can predict an item to a session).
If it changes, the hidden state of the network is reset to zeros.
Parameters
--------
session_ids : 1D array
Contains the session IDs of the events of the batch. Its length must equal to the prediction batch size (batch param).
input_item_ids : 1D array
Contains the item IDs of the events of the batch. Every item ID must be must be in the training data of the network. Its length must equal to the prediction batch size (batch param).
predict_for_item_ids : 1D array (optional)
IDs of items for which the network should give prediction scores. Every ID must be in the training set. The default value is None, which means that the network gives prediction on its every output (i.e. for all items in the training set).
batch : int
Prediction batch size.
Returns
--------
out : pandas.DataFrame
Prediction scores for selected items for every event of the batch.
Columns: events of the batch; rows: items. Rows are indexed by the item IDs.
'''
if self.error_during_train: raise Exception
if self.predict is None or self.predict_batch!=batch:
X = T.ivector()
Y = T.ivector()
for i in range(len(self.layers)):
self.H[i].set_value(np.zeros((batch,self.layers[i]), dtype=theano.config.floatX), borrow=True)
if predict_for_item_ids is not None:
H_new, yhat, _ = self.model(X, self.H, Y, 0)
else:
H_new, yhat = self.model_test(X, self.H)
updatesH = OrderedDict()
for i in range(len(self.H)):
updatesH[self.H[i]] = H_new[i]
if predict_for_item_ids is not None:
self.predict = function(inputs=[X, Y], outputs=yhat, updates=updatesH, allow_input_downcast=True)
else:
self.predict = function(inputs=[X], outputs=yhat, updates=updatesH, allow_input_downcast=True)
self.current_session = np.ones(batch) * -1
self.predict_batch = batch
session_change = np.arange(batch)[session_ids != self.current_session]
if len(session_change) > 0:
for i in range(len(self.H)):
tmp = self.H[i].get_value(borrow=True)
tmp[session_change] = 0
self.H[i].set_value(tmp, borrow=True)
self.current_session=session_ids.copy()
in_idxs = self.itemidmap[input_item_ids]
if predict_for_item_ids is not None:
iIdxs = self.itemidmap[predict_for_item_ids]
preds = np.asarray(self.predict(in_idxs, iIdxs)).T
return pd.DataFrame(data=preds, index=predict_for_item_ids)
else:
in_idxs.values[np.isnan(in_idxs.values)] = 0
preds = np.asarray(self.predict(in_idxs)).T
return pd.DataFrame(data=preds, index=self.itemidmap.index)
示例2: test_multMatVect
def test_multMatVect():
A1 = tensor.lmatrix('A1')
s1 = tensor.ivector('s1')
m1 = tensor.iscalar('m1')
A2 = tensor.lmatrix('A2')
s2 = tensor.ivector('s2')
m2 = tensor.iscalar('m2')
g0 = rng_mrg.DotModulo()(A1, s1, m1, A2, s2, m2)
f0 = theano.function([A1, s1, m1, A2, s2, m2], g0)
i32max = numpy.iinfo(numpy.int32).max
A1 = numpy.random.randint(0, i32max, (3, 3)).astype('int64')
s1 = numpy.random.randint(0, i32max, 3).astype('int32')
m1 = numpy.asarray(numpy.random.randint(i32max), dtype="int32")
A2 = numpy.random.randint(0, i32max, (3, 3)).astype('int64')
s2 = numpy.random.randint(0, i32max, 3).astype('int32')
m2 = numpy.asarray(numpy.random.randint(i32max), dtype="int32")
f0.input_storage[0].storage[0] = A1
f0.input_storage[1].storage[0] = s1
f0.input_storage[2].storage[0] = m1
f0.input_storage[3].storage[0] = A2
f0.input_storage[4].storage[0] = s2
f0.input_storage[5].storage[0] = m2
r_a1 = rng_mrg.matVecModM(A1, s1, m1)
r_a2 = rng_mrg.matVecModM(A2, s2, m2)
f0.fn()
r_b = f0.output_storage[0].value
assert numpy.allclose(r_a1, r_b[:3])
assert numpy.allclose(r_a2, r_b[3:])
示例3: bsgd1
def bsgd1(nn, data, name='sgd', lr=0.022, alpha=0.3, batch_size=500, epochs = 10):
train_set_x, train_set_y = data[0]
valid_set_x, valid_set_y = data[1]
test_set_x, test_set_y = data[2]
# valid_y_numpy = y_numpy[0]
# test_y_numpy = y_numpy[1]
test_y_numpy = map_48_to_39(test_y_numpy)
valid_y_numpy = map_48_to_39(valid_y_numpy)
print test_y_numpy
num_samples = train_set_x.get_value(borrow=True).shape[0]
num_batches = num_samples / batch_size
layers = nn.layers
x = T.matrix('x')
y = T.ivector('y')
y_eval = T.ivector('y_eval')
cost = nn.cost(x, y)
accuracy = nn.calcAccuracy(x, y)
params = nn.params
delta_params = nn.delta_params
print theano.pp(cost)
# theano.pp(accuracy)
p_grads = [T.grad(cost=cost, wrt = p) for p in params]
# implementing gradient descent with momentum
print p_grads
updates = OrderedDict()
for dp, gp in zip(delta_params, p_grads):
updates[dp] = dp*alpha - gp*lr
for p, dp in zip(params, delta_params):
updates[p] = p + updates[dp]
# updates = [(p, p - lr*gp) for p, gp in zip(params, p_grads)]
index = T.ivector('index')
batch_sgd_train = theano.function(inputs=[index], outputs=[cost, accuracy], updates=updates, givens={x: train_set_x[index], y:train_set_y[index]})
batch_sgd_valid = theano.function(inputs=[], outputs=[nn.calcAccuracy(x, y), nn.calcAccuracyTimit(x,y)], givens={x: valid_set_x, y:valid_set_y})
batch_sgd_test = theano.function(inputs=[], outputs=nn.calcAccuracy(x, y), givens={x: test_set_x, y:test_set_y})
indices = np.arange(num_samples, dtype=np.dtype('int32'))
np.random.shuffle(indices)
for n in xrange(epochs):
np.random.shuffle(indices)
for i in xrange(num_batches):
batch = indices[i*batch_size: (i+1)*batch_size]
batch_sgd_train(batch)
# y_np = y.get_value()
# print y.eval()
print "epoch:", n, " validation accuracy:", batch_sgd_valid()
print batch_sgd_test()
示例4: __init__
def __init__(self, dnodex,inputdim,dim):
X=T.ivector()
Y=T.ivector()
Z=T.lscalar()
eta = T.scalar()
temperature=T.scalar()
self.dnodex=dnodex
num_input = inputdim
dnodex.umatrix=theano.shared(floatX(np.random.randn(*(self.dnodex.nuser,inputdim, inputdim))))
dnodex.pmatrix=theano.shared(floatX(np.random.randn(*(self.dnodex.npoi,inputdim))))
dnodex.p_l2_norm=(dnodex.pmatrix**2).sum()
dnodex.u_l2_norm=(dnodex.umatrix**2).sum()
num_hidden = dim
num_output = inputdim
inputs = InputPLayer(dnodex.pmatrix[X,:], dnodex.umatrix[Z,:,:], name="inputs")
lstm1 = LSTMLayer(num_input, num_hidden, input_layer=inputs, name="lstm1")
lstm2 = LSTMLayer(num_hidden, num_hidden, input_layer=lstm1, name="lstm2")
lstm3 = LSTMLayer(num_hidden, num_hidden, input_layer=lstm2, name="lstm3")
softmax = SoftmaxPLayer(num_hidden, num_output, dnodex.umatrix[Z,:,:], input_layer=lstm3, name="yhat", temperature=temperature)
Y_hat = softmax.output()
self.layers = inputs, lstm1,lstm2,lstm3,softmax
params = get_params(self.layers)
#caches = make_caches(params)
cost = T.mean(T.nnet.categorical_crossentropy(Y_hat, T.dot(dnodex.pmatrix[Y,:],dnodex.umatrix[Z,:,:])))+eta*dnodex.p_l2_norm+eta*dnodex.u_l2_norm
updates = PerSGD(cost,params,eta,X,Z,dnodex)#momentum(cost, params, caches, eta)
self.train = theano.function([X,Y,Z, eta, temperature], cost, updates=updates, allow_input_downcast=True)
predict_updates = one_step_updates(self.layers)
self.predict_char = theano.function([X, Z, temperature], Y_hat, updates=predict_updates, allow_input_downcast=True)
示例5: multMatVect
def multMatVect(v, A, m1, B, m2):
"""
multiply the first half of v by A with a modulo of m1
and the second half by B with a modulo of m2
Note: The parameters of dot_modulo are passed implicitly because passing
them explicitly takes more time then running the function's C-code.
"""
if multMatVect.dot_modulo is None:
A_sym = tensor.lmatrix("A")
s_sym = tensor.ivector("s")
m_sym = tensor.iscalar("m")
A2_sym = tensor.lmatrix("A2")
s2_sym = tensor.ivector("s2")
m2_sym = tensor.iscalar("m2")
o = DotModulo()(A_sym, s_sym, m_sym, A2_sym, s2_sym, m2_sym)
multMatVect.dot_modulo = function([A_sym, s_sym, m_sym, A2_sym, s2_sym, m2_sym], o)
# This way of calling the Theano fct is done to bypass Theano overhead.
f = multMatVect.dot_modulo
f.input_storage[0].storage[0] = A
f.input_storage[1].storage[0] = v[:3]
f.input_storage[2].storage[0] = m1
f.input_storage[3].storage[0] = B
f.input_storage[4].storage[0] = v[3:]
f.input_storage[5].storage[0] = m2
f.fn()
r = f.output_storage[0].storage[0]
return r
示例6: __theano_build__
def __theano_build__(self):
U, V, W = self.U, self.V, self.W
x = T.ivector('x')
y = T.ivector('y')
def forward_prop_step(x_t, s_t_prev, U, V, W):
s_t = T.tanh(U[:,x_t] + W.dot(s_t_prev))
o_t = T.nnet.softmax(V.dot(s_t))
return [o_t[0], s_t]
[o,s], updates = theano.scan(
forward_prop_step,
sequences=x,
outputs_info=[None, dict(initial=T.zeros(self.hidden_dim))],
non_sequences=[U, V, W],
truncate_gradient=self.bptt_truncate,
strict=True)
prediction = T.argmax(o, axis=1)
o_error = T.sum(T.nnet.categorical_crossentropy(o, y))
# Gradients
dU = T.grad(o_error, U)
dV = T.grad(o_error, V)
dW = T.grad(o_error, W)
# Assign functions
self.forward_propagation = theano.function([x], o)
self.predict = theano.function([x], prediction)
self.ce_error = theano.function([x, y], o_error)
self.bptt = theano.function([x, y], [dU, dV, dW])
# SGD
learning_rate = T.scalar('learning_rate')
self.sgd_step = theano.function([x,y,learning_rate], [],
updates=[(self.U, self.U - learning_rate * dU),
(self.V, self.V - learning_rate * dV),
(self.W, self.W - learning_rate * dW)])
示例7: main
def main(num_epochs=NUM_EPOCHS):
print("Building network ...")
# First, we build the network, starting with an input layer
# Recurrent layers expect input of shape
# (batch size, SEQ_LENGTH, num_inputs)
#The network model
l_in = lasagne.layers.InputLayer(shape=(BATCH_SIZE, SEQ_LENGTH, num_inputs))
l_forward_1 = lasagne.layers.LSTMLayer(l_in, N_HIDDEN, grad_clipping=GRAD_CLIP,nonlinearity=lasagne.nonlinearities.tanh)
l_forward_2 = lasagne.layers.LSTMLayer(l_forward_1, N_HIDDEN, grad_clipping=GRAD_CLIP,nonlinearity=lasagne.nonlinearities.tanh)
l_shp = lasagne.layers.ReshapeLayer(l_forward_2, (-1, N_HIDDEN))
l_dense = lasagne.layers.DenseLayer(l_shp, num_units=num_inputs, lasagne.nonlinearity=linear)
l_out = lasagne.layers.ReshapeLayer(l_dense, (-1, SEQ_LENGTH, num_inputs))
# create output out of input in order to save memory?
network_output = lasagne.layers.get_output(l_out)
cost = lasagne.objectives.squared_error(network_output,target_values).mean()
all_params = lasagne.layers.get_all_params(l_out,trainable=True)
updates = lasagne.updates.adagrad(cost, all_params, LEARNING_RATE)
input_values = T.ivector('target_output')
target_values = T.ivector('target_output')
# Theano functions for training and computing cost
print("Compiling functions ...")
train = theano.function([l_in.input_var, target_values], cost, updates=updates, allow_input_downcast=True)
compute_cost = theano.function([l_in.input_var, target_values], cost, allow_input_downcast=True)
示例8: initialize
def initialize(self):
users = T.ivector()
items = T.ivector()
ratings = T.vector()
self.U = theano.shared(
np.array(
np.random.normal(scale=0.001, size=(self.n_users, self.n_factors)),
dtype=theano.config.floatX
)
)
self.I = theano.shared(
np.array(
np.random.normal(scale=0.001, size=(self.n_items, self.n_factors)),
dtype=theano.config.floatX
)
)
predictions = (self.U[users] * self.I[items]).sum(axis=1)
train_error = (
((predictions - ratings) ** 2).mean() +
self.regularization * (
T.sum(self.U ** 2) +
T.sum(self.I ** 2)
)
)
test_error = ((predictions - ratings) ** 2).mean()
params = [self.U, self.I]
learning_rate = theano.shared(np.array(self.learning_rate, dtype=theano.config.floatX))
updates = self.optimizer(train_error, params, learning_rate=learning_rate)
self.train_theano = theano.function([users, items, ratings], train_error, updates=updates)
self.test_theano = theano.function([users, items, ratings], test_error)
self.predict_theano = theano.function([users, items], predictions)
示例9: evaluate_ready
def evaluate_ready(self, ispro = True):
var_x = T.ivector()
var_y = T.ivector()
print "adopt mention level evaluate ???????????????????? "+str(self.ismention)
if self.model_type == "softmax" or self.model_type == "softmax_reg":
if self.istransition:
output = self.structure1(var_x, ispro = False)
self.evafunc = theano.function([var_x], output)
else:
output = self.structure1(var_x, ispro)
self.evafunc = theano.function([var_x], output)
elif self.model_type == "maxneg":
out1, out2 = self.structure2(var_x,ispro)
self.evafunc = theano.function([var_x], [out1,out2])
elif self.model_type == "maxout":
out1, out2 = self.structure2(var_x,False)
self.evafunc = theano.function([var_x], [out1,out2])
else: raise Exception
示例10: __init__
def __init__(self, vocabulary_size, hidden_size, output_size):
X = tensor.ivector()
Y = tensor.ivector()
keep_prob = tensor.fscalar()
learning_rate = tensor.fscalar()
emb_layer = Embedding(vocabulary_size, hidden_size)
lstm_layer = BiLSTM(hidden_size, hidden_size)
dropout_layer = Dropout(keep_prob)
fc_layer = FullConnect(2*hidden_size, output_size)
crf = CRF(output_size)
# graph defination
X_emb = emb_layer(X)
scores = fc_layer(tensor.tanh(lstm_layer(dropout_layer(X_emb))))
loss, predict = crf(scores, Y, isTraining=True)
# loss, predict and accuracy
accuracy = tensor.sum(tensor.eq(predict, Y)) * 1.0 / Y.shape[0]
params = emb_layer.params + lstm_layer.params + fc_layer.params + crf.params
updates = MomentumSGD(loss, params, lr=learning_rate)
print("Compiling train function: ")
train = theano.function(inputs=[X, Y, keep_prob, learning_rate], outputs=[predict, accuracy, loss],
updates=updates, allow_input_downcast=True)
print("Compiling evaluate function: ")
evaluate = theano.function(inputs=[X_emb, Y, keep_prob], outputs=[predict, accuracy, loss],
allow_input_downcast=True)
self.embedding_tensor = emb_layer.params[0]
self.train = train
self.evaluate = evaluate
self.params = params
示例11: set_model
def set_model(args, init_w_emb, w_emb_dim, vocab_word, vocab_char, vocab_tag):
print '\nBuilding a neural model: %s\n' % args.model
""" neural architecture parameters """
c_emb_dim = args.c_emb_dim
w_hidden_dim = args.w_hidden_dim
c_hidden_dim = args.c_hidden_dim
output_dim = vocab_tag.size()
window = args.window
opt = args.opt
""" symbol definition """
x = T.ivector()
c = T.ivector()
b = T.ivector()
y = T.ivector()
lr = T.fscalar('lr')
if args.model == 'char':
return nn_char.Model(name=args.model, w=x, c=c, b=b, y=y, lr=lr,
init_w_emb=init_w_emb, vocab_w_size=vocab_word.size(), vocab_c_size=vocab_char.size(),
w_emb_dim=w_emb_dim, c_emb_dim=c_emb_dim, w_hidden_dim=w_hidden_dim,
c_hidden_dim=c_hidden_dim, output_dim=output_dim,
window=window, opt=opt)
else:
return nn_word.Model(name=args.model, x=x, y=y, lr=lr,
init_emb=init_w_emb, vocab_size=vocab_word.size(),
emb_dim=w_emb_dim, hidden_dim=w_hidden_dim, output_dim=output_dim,
window=window, opt=opt)
示例12: create_iter_funcs_train
def create_iter_funcs_train(l_out, lr, mntm, wd):
X = T.tensor4('X')
y = T.ivector('y')
X_batch = T.tensor4('X_batch')
y_batch = T.ivector('y_batch')
y_hat = layers.get_output(l_out, X, deterministic=False)
# softmax loss
train_loss = T.mean(
T.nnet.categorical_crossentropy(y_hat, y))
# L2 regularization
train_loss += wd * regularize_network_params(l_out, l2)
train_acc = T.mean(
T.eq(y_hat.argmax(axis=1), y))
all_params = layers.get_all_params(l_out, trainable=True)
updates = lasagne.updates.nesterov_momentum(
train_loss, all_params, lr, mntm)
train_iter = theano.function(
inputs=[theano.Param(X_batch), theano.Param(y_batch)],
outputs=[train_loss, train_acc],
updates=updates,
givens={
X: X_batch,
y: y_batch,
},
)
return train_iter
示例13: create_iter_funcs_valid
def create_iter_funcs_valid(l_out, bs=None, N=50, mc_dropout=False):
X = T.tensor4('X')
y = T.ivector('y')
X_batch = T.tensor4('X_batch')
y_batch = T.ivector('y_batch')
if not mc_dropout:
y_hat = layers.get_output(l_out, X, deterministic=True)
else:
if bs is None:
raise ValueError('a fixed batch size is required for mc dropout')
X_repeat = T.extra_ops.repeat(X, N, axis=0)
y_sample = layers.get_output(
l_out, X_repeat, deterministic=False)
sizes = [X_repeat.shape[0] / X.shape[0]] * bs
y_sample_split = T.as_tensor_variable(
T.split(y_sample, sizes, bs, axis=0))
y_hat = T.mean(y_sample_split, axis=1)
valid_loss = T.mean(
T.nnet.categorical_crossentropy(y_hat, y))
valid_acc = T.mean(
T.eq(y_hat.argmax(axis=1), y))
valid_iter = theano.function(
inputs=[theano.Param(X_batch), theano.Param(y_batch)],
outputs=[valid_loss, valid_acc],
givens={
X: X_batch,
y: y_batch,
},
)
return valid_iter
示例14: __init__
def __init__(self, input_params=None):
rng = numpy.random.RandomState(23455)
self._corpusWithEmbeddings = T.matrix("wordIndeices")
self._dialogSentenceCount = T.ivector("dialogSentenceCount")
self._sentenceWordCount = T.ivector("sentenceWordCount")
# for list-type data
self._layer0 = SentenceEmbeddingNN(self._corpusWithEmbeddings, self._dialogSentenceCount, self._sentenceWordCount, rng, wordEmbeddingDim=200, \
sentenceLayerNodesNum=1000, \
sentenceLayerNodesSize=[5, 200])
self._average_layer = sentenceEmbeddingAverage(self._corpusWithEmbeddings, self._dialogSentenceCount, self._sentenceWordCount, rng, wordEmbeddingDim=200)
# Get sentence layer W
semanicTransformW = theano.shared(
numpy.asarray(
rng.uniform(low=-0.2, high=0.2, size=(self._layer0.outputDimension, 200)),
dtype=config.globalFloatType()
),
borrow=True
)
self._nextSentence = T.dot(self._layer0.output, semanicTransformW)
# construct the parameter array.
self._params = [semanicTransformW] + self._layer0.params
self._setParameters(input_params)
示例15: test_unwrapper
def test_unwrapper():
emb_size = 5
y_time = tt.ivector()
y_seq_id = tt.ivector()
x = tt.tensor3()
emb = IdentityInput(x, size=5)
sequn = SeqUnwrapper(20)
sequn.connect(emb, y_time, y_seq_id)
rng = np.random.RandomState(23455)
conv = LeNetConvPoolLayer()
conv.connect(sequn, rng, (3, 1, 5, emb_size), (1, 1, ))
#prev_layer = conv
f = theano.function([x, y_time, y_seq_id], conv.output())
xx = np.random.randn(20, 4, emb_size)
y_time = [3, 7, 10, 12]
y_seq_id = [0, 0, 0, 0]
res = f(xx, y_time, y_seq_id)
print res.shape
print res
import ipdb; ipdb.set_trace()