本文整理汇总了Python中theano.tensor.fscalar函数的典型用法代码示例。如果您正苦于以下问题:Python fscalar函数的具体用法?Python fscalar怎么用?Python fscalar使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fscalar函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_no_complex
def test_no_complex():
width_var = tensor.cscalar()
freq_var = tensor.fscalar()
signal_var = tensor.fscalar()
stft_out = tensor.exp(width_var * freq_var) * signal_var
theano.function([width_var, freq_var, signal_var], stft_out,
mode=mode_with_gpu)
开发者ID:HapeMask,项目名称:Theano,代码行数:7,代码来源:test_opt.py
示例2: create_learning_rate_func
def create_learning_rate_func(solver_params):
base = tt.fscalar('base')
gamma = tt.fscalar('gamma')
power = tt.fscalar('power')
itrvl = tt.fscalar('itrvl')
iter = tt.scalar('iter')
if solver_params['lr_type']=='inv':
lr_ = base * tt.pow(1 + gamma * iter, -power)
lr = t.function(
inputs=[iter, t.Param(base, default=solver_params['base']), t.Param(gamma, default=solver_params['gamma']), t.Param(power, default=solver_params['power'])],
outputs=lr_)
elif solver_params['lr_type']=='fixed':
lr_ = base
lr = t.function(
inputs=[iter, t.Param(base, default=solver_params['base'])],
outputs=lr_,
on_unused_input='ignore')
elif solver_params['lr_type']=='episodic':
lr_ = base / (tt.floor(iter/itrvl) + 1)
lr = t.function(
inputs=[iter, t.Param(base, default=solver_params['base']), t.Param(itrvl, default=solver_params['interval'])],
outputs=lr_,
on_unused_input='ignore')
return lr
开发者ID:bentzinir,项目名称:Buffe,代码行数:30,代码来源:learning_rate.py
示例3: __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
开发者ID:dongx-duan,项目名称:crf,代码行数:34,代码来源:train_emb.py
示例4: __init__
def __init__(self, n_comp=10, verbose=False):
# Theano initialization
self.T_weights = shared(np.eye(n_comp, dtype=np.float32))
self.T_bias = shared(np.ones((n_comp, 1), dtype=np.float32))
T_p_x_white = T.fmatrix()
T_lrate = T.fscalar()
T_block = T.fscalar()
T_unmixed = T.dot(self.T_weights,T_p_x_white) + T.addbroadcast(self.T_bias,1)
T_logit = 1 - 2 / (1 + T.exp(-T_unmixed))
T_out = self.T_weights + T_lrate * T.dot(T_block * T.identity_like(self.T_weights) + T.dot(T_logit, T.transpose(T_unmixed)), self.T_weights)
T_bias_out = self.T_bias + T_lrate * T.reshape(T_logit.sum(axis=1), (-1,1))
T_max_w = T.max(self.T_weights)
T_isnan = T.any(T.isnan(self.T_weights))
self.w_up_fun = theano.function([T_p_x_white, T_lrate, T_block],
[T_max_w, T_isnan],
updates=[(self.T_weights, T_out),
(self.T_bias, T_bias_out)],
allow_input_downcast=True)
T_matrix = T.fmatrix()
T_cov = T.dot(T_matrix,T.transpose(T_matrix))/T_block
self.cov_fun = theano.function([T_matrix, T_block], T_cov, allow_input_downcast=True)
self.loading = None
self.sources = None
self.weights = None
self.n_comp = n_comp
self.verbose = verbose
开发者ID:edamaraju,项目名称:ica,代码行数:32,代码来源:ica_gpu.py
示例5: test_default_dtype
def test_default_dtype(self):
random = RandomStreams(utt.fetch_seed())
low = tensor.dscalar()
high = tensor.dscalar()
# Should not silently downcast from low and high
out0 = random.uniform(low=low, high=high, size=(42,))
assert out0.dtype == 'float64'
f0 = function([low, high], out0)
val0 = f0(-2.1, 3.1)
assert val0.dtype == 'float64'
# Should downcast, since asked explicitly
out1 = random.uniform(low=low, high=high, size=(42,), dtype='float32')
assert out1.dtype == 'float32'
f1 = function([low, high], out1)
val1 = f1(-1.1, 1.1)
assert val1.dtype == 'float32'
# Should use floatX
lowf = tensor.fscalar()
highf = tensor.fscalar()
outf = random.uniform(low=lowf, high=highf, size=(42,))
assert outf.dtype == config.floatX
ff = function([lowf, highf], outf)
valf = ff(numpy.float32(-0.1), numpy.float32(0.3))
assert valf.dtype == config.floatX
开发者ID:ChinaQuants,项目名称:Theano,代码行数:27,代码来源:test_shared_randomstreams.py
示例6: __build_iterative_functions
def __build_iterative_functions(self):
def states_dot(lambda_x, lambda_y, x_data, y_data):
[x_dot, h_dot, y_dot] = T.grad(-self.energy_sum, self.states)
x_dot_final = lambda_x * (x_data - self.x) + (1. - lambda_x) * x_dot
y_dot_final = lambda_y * (y_data - self.y) + (1. - lambda_y) * y_dot
return [x_dot_final, h_dot, y_dot_final]
lambda_x = T.fscalar('lambda_x')
lambda_y = T.fscalar('lambda_y')
x_data = self.outside_world.x_data
y_data = self.outside_world.y_data_one_hot
states_dot = [x_dot, h_dot, y_dot] = states_dot(lambda_x, lambda_y, x_data, y_data)
kinetic_energy = T.mean( sum( [(state_dot ** 2).sum(axis=1) for state_dot in states_dot] ) )
params_dot = T.grad(kinetic_energy, self.params)
# UPDATES
epsilon = T.fscalar('epsilon')
alpha_W1 = T.fscalar('alpha_W1')
alpha_W2 = T.fscalar('alpha_W2')
learning_rates = [alpha_W1,alpha_W1,alpha_W1,alpha_W2,alpha_W2]
Delta_states = [epsilon * state_dot for state_dot in states_dot]
Delta_params = [alpha * param_dot for alpha,param_dot in zip(learning_rates,params_dot)]
states_new = [state+Delta for state,Delta in zip(self.states,Delta_states)]
params_new = [param+Delta for param,Delta in zip(self.params,Delta_params)]
updates_states = zip(self.states,states_new)
updates_params = zip(self.params,params_new)
# OUTPUTS FOR MONITORING
error_rate = T.mean(T.neq(self.prediction, self.outside_world.y_data))
mse = T.mean(((self.y - self.outside_world.y_data_one_hot) ** 2).sum(axis=1))
norm_grad_hy = T.sqrt( (h_dot ** 2).mean(axis=0).sum() + (y_dot ** 2).mean(axis=0).sum() )
Delta_W1 = Delta_params[1]
Delta_W2 = Delta_params[3]
Delta_logW1 = T.sqrt( (Delta_W1 ** 2).mean() ) / T.sqrt( (self.W1 ** 2).mean() )
Delta_logW2 = T.sqrt( (Delta_W2 ** 2).mean() ) / T.sqrt( (self.W2 ** 2).mean() )
# THEANO FUNCTIONS
iterative_function = theano.function(
inputs=[lambda_x, lambda_y, epsilon, alpha_W1, alpha_W2],
outputs=[self.energy, norm_grad_hy, self.prediction, error_rate, mse, Delta_logW1, Delta_logW2],
updates=updates_params+updates_states
)
relaxation_function = theano.function(
inputs=[epsilon],
outputs=[self.energy, norm_grad_hy, self.prediction, error_rate, mse],
givens={
lambda_y: T.constant(0.)
},
updates=updates_states[1:3]
)
return iterative_function, relaxation_function
开发者ID:soroushmehr,项目名称:BiologicalNetwork,代码行数:58,代码来源:compact_model.py
示例7: generate_theano_functions
def generate_theano_functions(self, next_layer):
'''Compile necessary theano functions'''
exp = tensor.fmatrix('expected')
rate = tensor.fscalar('rate')
momentum = tensor.fscalar('momentum')
##Compute outputs given inputs
self.get_output = theano.function([],
updates = [(self.outputs,
tensor.nnet.sigmoid(
tensor.dot(
self.inputs,
self.weights)))],
name='get_output')
##Compute error values given errors of previous layer
if self.output:
self.find_errors = theano.function([exp],
updates = [(self.errors,
self.outputs *
(1 - self.outputs)
* exp)],
name='find_errors',
allow_input_downcast=True)
else:
self.find_errors = theano.function([],
updates = [(self.errors,
self.outputs *
(1 - self.outputs) *
tensor.dot(next_layer.errors,
next_layer.weights.T))],
name='find_errors')
##Compute the change to the weight vector using stochastic gradient
##descent with momentum
self.train_compute = theano.function([rate, momentum],
updates = [(self.delta_weights,
self.delta_weights *
momentum +
theano.tensor.dot(self.inputs.T,
(rate * self.errors)))],
name='train_compute',
allow_input_downcast=True)
##Adjust weights using the delta_w computed in train_compute
self.adjust = theano.function([], updates=[(self.weights, self.weights +
self.delta_weights)],
name='adjust')
##Drop a number of nodes roughly equal to rate/output_size
self.dropout = theano.function([rate], updates = [(self.outputs,
tensor.switch(
self.random.binomial(size=(1,
self.output_size),
p=rate), self.outputs /
rate, 0))],
name='dropout',
allow_input_downcast=True)
开发者ID:grant,项目名称:algo-rhythm,代码行数:58,代码来源:neural_net.py
示例8: find_Y
def find_Y(X_shared, Y_shared, sigma_shared, N, output_dims, n_epochs,
initial_lr, final_lr, lr_switch, init_stdev, initial_momentum,
final_momentum, momentum_switch, metric, verbose=0):
"""Optimize cost wrt Y"""
# Optimization hyperparameters
initial_lr = np.array(initial_lr, dtype=floath)
final_lr = np.array(final_lr, dtype=floath)
initial_momentum = np.array(initial_momentum, dtype=floath)
final_momentum = np.array(final_momentum, dtype=floath)
lr = T.fscalar('lr')
lr_shared = theano.shared(initial_lr)
momentum = T.fscalar('momentum')
momentum_shared = theano.shared(initial_momentum)
# Y velocities
Yv = T.fmatrix('Yv')
Yv_shared = theano.shared(np.zeros((N, output_dims), dtype=floath))
# Cost
X = T.fmatrix('X')
sigma = T.fvector('sigma')
Y = T.fmatrix('Y')
cost = cost_var(X, Y, sigma, metric)
# Setting update for Y velocities
grad_Y = T.grad(cost, Y)
updates = [(Yv_shared, momentum*Yv - lr*grad_Y)]
givens = {X: X_shared, sigma: sigma_shared, Y: Y_shared, Yv: Yv_shared,
lr: lr_shared, momentum: momentum_shared}
update_Yv = theano.function([], cost, givens=givens, updates=updates)
# Setting update for Y
givens = {Y: Y_shared, Yv: Yv_shared}
updates = [(Y_shared, Y + Yv)]
update_Y = theano.function([], [], givens=givens, updates=updates)
# Momentum-based gradient descent
for epoch in range(n_epochs):
if epoch == lr_switch:
lr_shared.set_value(final_lr)
if epoch == momentum_switch:
momentum_shared.set_value(final_momentum)
c = update_Yv()
update_Y()
if verbose:
print('Epoch: {0}. Cost: {1:.6f}.'.format(epoch + 1, float(c)))
return np.array(Y_shared.get_value())
开发者ID:paulorauber,项目名称:thesne,代码行数:55,代码来源:tsne.py
示例9: train
def train(self, data1, data2, similarities, miniBatchSize=20, epochs=200):
nrMiniBatches = len(data1) / miniBatchSize
miniBatchIndex = T.lscalar()
momentum = T.fscalar()
learningRate = T.fscalar()
learningRateMiniBatch = np.float32(self.learningRate / miniBatchSize)
print "learningRateMiniBatch in similarity net"
print learningRateMiniBatch
net = self._trainRBM(data1, data2)
data1 = theano.shared(np.asarray(data1,dtype=theanoFloat))
data2 = theano.shared(np.asarray(data2,dtype=theanoFloat))
similarities = theano.shared(np.asarray(similarities,dtype=theanoFloat))
# The mini-batch data is a matrix
x = T.matrix('x', dtype=theanoFloat)
y = T.matrix('y', dtype=theanoFloat)
self.x = x
self.y = y
z = T.vector('z', dtype=theanoFloat)
trainer = Trainer(x, y, net)
self.trainer = trainer
# error = T.sum(T.sqr(trainer.output-z))
error = T.sum(T.nnet.binary_crossentropy(trainer.output, z))
updates = self.buildUpdates(trainer, error, learningRate, momentum)
# Now you have to define the theano function
discriminativeTraining = theano.function(
inputs=[miniBatchIndex, learningRate, momentum],
outputs=[trainer.output, trainer.cos],
updates=updates,
givens={
x: data1[miniBatchIndex * miniBatchSize:(miniBatchIndex + 1) * miniBatchSize],
y: data2[miniBatchIndex * miniBatchSize:(miniBatchIndex + 1) * miniBatchSize],
z: similarities[miniBatchIndex * miniBatchSize:(miniBatchIndex + 1) * miniBatchSize],
})
for epoch in xrange(epochs):
print "epoch", epoch
momentum = np.float32(min(np.float32(0.5) + epoch * np.float32(0.1),
np.float32(0.95)))
for miniBatch in xrange(nrMiniBatches):
output, cos = discriminativeTraining(miniBatch, learningRateMiniBatch, momentum)
print trainer.w.get_value()
print trainer.b.get_value()
开发者ID:valadhi,项目名称:AttachmentDBN,代码行数:53,代码来源:similarity.py
示例10: build_finetune_functions
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size):
#print len(self.layers)
#print [T.shape(l.W)[0] for l in self.layers]
(train_set_x, train_set_y) = train_shared_xy
(valid_set_x, valid_set_y) = valid_shared_xy
#print T.shape(train_set_x), T.shape(train_set_y)
index = T.lscalar('index') # index to a [mini]batch
learning_rate = T.fscalar('learning_rate')
momentum = T.fscalar('momentum')
# compute the gradients with respect to the model parameters
gparams = T.grad(self.finetune_cost, self.params)
# compute list of fine-tuning updates
updates = collections.OrderedDict()
for dparam, gparam in zip(self.delta_params, gparams):
updates[dparam] = momentum * dparam - gparam*learning_rate
for dparam, param in zip(self.delta_params, self.params):
updates[param] = param + updates[dparam]
if self.max_col_norm is not None:
for i in xrange(self.hidden_layers_number):
W = self.layers[i].W
if W in updates:
updated_W = updates[W]
col_norms = T.sqrt(T.sum(T.sqr(updated_W), axis=0))
desired_norms = T.clip(col_norms, 0, self.max_col_norm)
updates[W] = updated_W * (desired_norms / (1e-7 + col_norms))
train_fn = theano.function(inputs=[index, theano.Param(learning_rate, default = 0.0001),
theano.Param(momentum, default = 0.5)],
outputs=self.errors,
updates=updates,
givens={
self.x: train_set_x[index * batch_size:
(index + 1) * batch_size],
self.y: train_set_y[index * batch_size:
(index + 1) * batch_size]})
valid_fn = theano.function(inputs=[index],
outputs=self.errors,
givens={
self.x: valid_set_x[index * batch_size:
(index + 1) * batch_size],
self.y: valid_set_y[index * batch_size:
(index + 1) * batch_size]})
return train_fn, valid_fn
开发者ID:mclaughlin6464,项目名称:pdnn,代码行数:52,代码来源:dnn.py
示例11: __init__
def __init__(self, input_dim, emb_dim, n_senses, W_w_f, lambdaH, lambdaL2, adjust, lambdaF):
super().__init__(input_dim, emb_dim, n_senses, W_w_f, lambdaF)
self.Wb = zeros((input_dim+1, n_senses), name="Wb") # sense- and word-specific bias
self.H = TT.fscalar() # entropy
self.L2 = TT.fscalar()
self.lambdaH = lambdaH # weight for entropy regularizer
self.lambdaL2 = lambdaL2 # weight for L2 regularizer
if lambdaL2 == 0.:
self.L2 = 0.
else:
self.L2 = TT.sum(TT.sqr(self.W_w)) + TT.sum(TT.sqr(self.W_c))
self.adjust = adjust
开发者ID:MorLong,项目名称:bimu,代码行数:13,代码来源:bimu_expectation.py
示例12: __init__
def __init__(self, game_params, arch_params, solver_params, trained_model, sn_dir):
params=[None, None]
if trained_model[0]:
params[0] = common.load_params(trained_model[0])
if trained_model[1]:
params[1] = common.load_params(trained_model[1])
self.lr_func = []
self.lr_func.append(create_learning_rate_func(solver_params['controler_0']))
self.lr_func.append(create_learning_rate_func(solver_params['controler_1']))
self.x_host_0 = tt.fvector('x_host_0')
self.v_host_0 = tt.fvector('v_host_0')
self.x_target_0 = tt.fvector('x_target_0')
self.v_target_0 = tt.fvector('v_target_0')
self.x_mines_0 = tt.fmatrix('x_mines_0')
self.mines_map = tt.fmatrix('mines_map')
self.time_steps = tt.fvector('time_steps')
self.force = tt.fmatrix('force')
self.n_steps_0 = tt.iscalar('n_steps_0')
self.n_steps_1 = tt.iscalar('n_steps_1')
self.lr = tt.fscalar('lr')
self.goal_1 = tt.fvector('goal_1')
self.trnsprnt = tt.fscalar('trnsprnt')
self.rand_goals = tt.fmatrix('rand_goals')
self.game_params = game_params
self.arch_params = arch_params
self.solver_params = solver_params
self.sn_dir = sn_dir
self.model = CONTROLLER(self.x_host_0,
self.v_host_0,
self.x_target_0,
self.v_target_0,
self.x_mines_0,
self.mines_map,
self.time_steps,
self.force,
self.n_steps_0,
self.n_steps_1,
self.lr,
self.goal_1,
self.trnsprnt,
self.rand_goals,
self.game_params,
self.arch_params,
self.solver_params,
params)
开发者ID:bentzinir,项目名称:Buffe,代码行数:51,代码来源:ctrlr_optimizer.py
示例13: compile
def compile(self):
""" compile theano functions
"""
self.t_L1_reg = T.fscalar('L1_reg')
self.t_L2_reg = T.fscalar('L2_reg')
self.t_learning_rate = T.fscalar('learning_rate')
cost = self.loss + self.t_L1_reg * self.L1 + self.t_L2_reg * self.L2_sqr
self.parameter_updates = [(param, param - self.t_learning_rate * T.grad(cost, param)) for param in self.params]
self._tf_train = theano.function(inputs=[self.input, self.true_output, self.t_L1_reg, self.t_L2_reg, self.t_learning_rate],
outputs=[self.loss], allow_input_downcast=True, updates=self.parameter_updates)
self._tf_infer = theano.function(inputs=[self.input], outputs=[self.output], allow_input_downcast=True)
self._tf_evaluate = theano.function(inputs=[self.input, self.true_output], outputs=[self.loss],
allow_input_downcast=True)
开发者ID:yueranyuan,项目名称:vector_edu,代码行数:15,代码来源:net.py
示例14: get_adagrad_trainer
def get_adagrad_trainer(self):
""" Returns an Adagrad (Duchi et al. 2010) trainer using a learning rate.
"""
batch_x = T.fmatrix('batch_x')
batch_y = T.ivector('batch_y')
learning_rate = T.fscalar('lr') # learning rate to use
# compute the gradients with respect to the model parameters
gparams = T.grad(self.mean_cost, self.params)
# compute list of weights updates
updates = OrderedDict()
for accugrad, param, gparam in zip(self._accugrads, self.params, gparams):
# c.f. Algorithm 1 in the Adadelta paper (Zeiler 2012)
agrad = accugrad + gparam * gparam
dx = - (learning_rate / T.sqrt(agrad + self._eps)) * gparam
updates[param] = param + dx
updates[accugrad] = agrad
train_fn = theano.function(inputs=[theano.Param(batch_x),
theano.Param(batch_y),
theano.Param(learning_rate)],
outputs=self.mean_cost,
updates=updates,
givens={self.x: batch_x, self.y: batch_y})
return train_fn
开发者ID:goelrhea1992,项目名称:AdaptiveLearning,代码行数:27,代码来源:AdaptiveLearningDNN.py
示例15: cmp
def cmp(a_shp, b_shp):
a = tensor.fmatrix()
b = tensor.fmatrix()
scalar = tensor.fscalar()
av = my_rand(*a_shp)
bv = my_rand(*b_shp)
f = theano.function(
[a, b],
tensor.dot(a, b) * numpy.asarray(4, 'float32'),
mode=mode_with_gpu)
f2 = theano.function(
[a, b],
tensor.dot(a, b) * numpy.asarray(4, 'float32'))
t = f.maker.fgraph.toposort()
assert len(t) == 4
assert isinstance(t[0].op, tcn.GpuFromHost)
assert isinstance(t[1].op, tcn.GpuFromHost)
assert isinstance(t[2].op, tcn.blas.GpuDot22Scalar)
assert isinstance(t[3].op, tcn.HostFromGpu)
assert numpy.allclose(f(av, bv), f2(av, bv))
f = theano.function([a, b, scalar], tensor.dot(a, b) * scalar,
mode=mode_with_gpu)
f2 = theano.function([a, b, scalar], tensor.dot(a, b) * scalar)
t = f.maker.fgraph.toposort()
assert len(t) == 4
assert isinstance(t[0].op, tcn.GpuFromHost)
assert isinstance(t[1].op, tcn.GpuFromHost)
assert isinstance(t[2].op, tcn.blas.GpuDot22Scalar)
assert isinstance(t[3].op, tcn.HostFromGpu)
assert numpy.allclose(f(av, bv, 0.5), f2(av, bv, 0.5))
开发者ID:gyenney,项目名称:Tools,代码行数:32,代码来源:test_blas.py
示例16: get_SGD_trainer
def get_SGD_trainer(self):
""" Returns a plain SGD minibatch trainer with learning rate as param.
"""
batch_x = T.fmatrix('batch_x')
batch_y = T.ivector('batch_y')
learning_rate = T.fscalar('lr') # learning rate to use
# compute the gradients with respect to the model parameters
# using mean_cost so that the learning rate is not too dependent
# on the batch size
gparams = T.grad(self.mean_cost, self.params)
# compute list of weights updates
updates = OrderedDict()
for param, gparam in zip(self.params, gparams):
if self.max_norm:
W = param - gparam * learning_rate
col_norms = W.norm(2, axis=0)
desired_norms = T.clip(col_norms, 0, self.max_norm)
updates[param] = W * (desired_norms / (1e-6 + col_norms))
else:
updates[param] = param - gparam * learning_rate
train_fn = theano.function(inputs=[theano.Param(batch_x),
theano.Param(batch_y),
theano.Param(learning_rate)],
outputs=self.mean_cost,
updates=updates,
givens={self.x: batch_x, self.y: batch_y})
return train_fn
开发者ID:jortizcs,项目名称:machine-learning,代码行数:30,代码来源:dnn.py
示例17: get_SGD_trainer
def get_SGD_trainer(self, debug=False):
""" Returns a plain SGD minibatch trainer with learning rate as param.
"""
batch_x1 = T.fmatrix('batch_x1')
batch_x2 = T.fmatrix('batch_x2')
batch_y = T.ivector('batch_y')
learning_rate = T.fscalar('lr') # learning rate to use
# compute the gradients with respect to the model parameters
# using mean_cost so that the learning rate is not too dependent on the batch size
cost = self.mean_cos_sim_cost
gparams = T.grad(cost, self.params)
# compute list of weights updates
updates = OrderedDict()
for param, gparam in zip(self.params, gparams):
updates[param] = param - gparam * learning_rate
outputs = cost
if debug:
outputs = [cost] + self.params + gparams +\
[updates[param] for param in self.params]
train_fn = theano.function(inputs=[theano.Param(batch_x1),
theano.Param(batch_x2), theano.Param(batch_y),
theano.Param(learning_rate)],
outputs=outputs,
updates=updates,
givens={self.x1: batch_x1, self.x2: batch_x2, self.y: batch_y})
return train_fn
开发者ID:Verderey,项目名称:timit_tools,代码行数:30,代码来源:nnet_archs.py
示例18: compile
def compile(self, log_pxz, log_qpz, cost, a_pxz):
batch_idx = T.iscalar()
learning_rate = T.fscalar()
updates, norm_grad = self.hp.optimizer(cost, self.params.values(), lr=learning_rate)
self.outidx = {'cost':0, 'cost_p':1, 'cost_q':2, 'norm_grad':3}
outputs = [cost, log_pxz, log_qpz]
self.train = theano.function(inputs=[batch_idx, learning_rate],
givens={self.X:self.data['tr_X'][batch_idx * self.hp.batch_size :
(batch_idx+1) * self.hp.batch_size]},
outputs=outputs + [norm_grad], updates=updates)
self.validate = theano.function(inputs=[batch_idx],
givens={self.X:self.data['tr_X'][batch_idx * self.hp.test_batch_size :
(batch_idx+1) * self.hp.test_batch_size]},
outputs=outputs)
self.test = theano.function(inputs=[batch_idx],
givens={self.X:self.data['te_X'][batch_idx * self.hp.test_batch_size :
(batch_idx+1) * self.hp.test_batch_size]},
outputs=outputs)
n_samples = T.iscalar()
if self.resample_z:
self.data['ge_Z'] = srnd.normal((self.max_gen_samples, self.n_z), dtype=theano.config.floatX)
else:
self.data['ge_Z'] = shared(np.random.randn(self.max_gen_samples, self.n_z))
self.decode = theano.function(inputs=[n_samples],
givens={self.Z:self.data['ge_Z'][:n_samples]},
outputs=a_pxz)
开发者ID:ronvohra,项目名称:Theano-Lights,代码行数:34,代码来源:modelbase.py
示例19: ADAMopt
def ADAMopt(self, tVars, loss, lr, momentum=0):
i = T.iscalar('i'); lr = T.fscalar('lr');
grads = T.grad(loss, tVars)
'''ADAM Code from
https://github.com/danfischetti/deep-recurrent-attentive-writer/blob/master/DRAW/adam.py
'''
self.m = [theano.shared(name = 'm', \
value = np.zeros(param.get_value().shape,dtype=theano.config.floatX)) for param in model.params]
self.v = [theano.shared(name = 'v', \
value = np.zeros(param.get_value().shape,dtype=theano.config.floatX)) for param in model.params]
self.t = theano.shared(name = 't',value = np.asarray(1).astype(theano.config.floatX))
updates = [(self.t,self.t+1)]
for param, gparam,m,v in zip(model.params, gparams, self.m, self.v):
b1_t = 1-(1-beta1)*(l**(self.t-1))
m_t = b1_t*gparam + (1-b1_t)*m
updates.append((m,m_t))
v_t = beta2*(gparam**2)+(1-beta2)*v
updates.append((v,v_t))
m_t_bias = m_t/(1-(1-beta1)**self.t)
v_t_bias = v_t/(1-(1-beta2)**self.t)
if param.get_value().ndim == 1:
updates.append((param,param - 5*lr*m_t_bias/(T.sqrt(v_t_bias)+epsilon)))
else:
updates.append((param,param - lr*m_t_bias/(T.sqrt(v_t_bias)+epsilon)))
return theano.function([], loss, updates=updates)
开发者ID:lebek,项目名称:reversible-raytracer,代码行数:29,代码来源:optimize.py
示例20: test_copy_delete_updates
def test_copy_delete_updates(self):
w = T.iscalar('w')
x = T.fscalar('x')
# SharedVariable for tests, one of them has update
y = theano.shared(value=1, name='y')
z = theano.shared(value=2, name='z')
out = x + y + z
# Test for different linkers
# for mode in ["FAST_RUN","FAST_COMPILE"]:
# second_time = False
for mode in ["FAST_RUN", "FAST_COMPILE"]:
ori = theano.function([x], out, mode=mode, updates={z: z * 2})
cpy = ori.copy(delete_updates=True)
assert cpy(1)[0] == 4
assert cpy(1)[0] == 4
assert cpy(1)[0] == 4
# Test if unused implicit and explicit inputs from delete_updates
# are ignored as intended.
for mode in ["FAST_RUN", "FAST_COMPILE"]:
ori = theano.function([x], x, mode=mode, updates={z: z * 2})
cpy = ori.copy(delete_updates=True)
ori = theano.function([x, w], x, mode=mode, updates={z: z + w})
cpy = ori.copy(delete_updates=True)
开发者ID:gvtulder,项目名称:Theano,代码行数:27,代码来源:test_function_module.py
注:本文中的theano.tensor.fscalar函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论