本文整理汇总了Python中theano.Param方法的典型用法代码示例。如果您正苦于以下问题:Python theano.Param方法的具体用法?Python theano.Param怎么用?Python theano.Param使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano
的用法示例。
在下文中一共展示了theano.Param方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_SGD_trainer
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
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
gparams = T.grad(self.mean_cost, self.params) # all the gradients
updates = OrderedDict()
for param, gparam in zip(self.params, gparams):
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
示例2: get_adagrad_trainer
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
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
gparams = T.grad(self.mean_cost, self.params) # all the gradients
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
示例3: get_adadelta_trainer
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def get_adadelta_trainer(self):
""" Returns an Adadelta (Zeiler 2012) trainer using self._rho and
self._eps params. """
batch_x = T.fmatrix('batch_x')
batch_y = T.ivector('batch_y')
gparams = T.grad(self.mean_cost, self.params)
updates = OrderedDict()
for accugrad, accudelta, param, gparam in zip(self._accugrads,
self._accudeltas, self.params, gparams):
# c.f. Algorithm 1 in the Adadelta paper (Zeiler 2012)
agrad = self._rho * accugrad + (1 - self._rho) * gparam * gparam
dx = - T.sqrt((accudelta + self._eps)
/ (agrad + self._eps)) * gparam
updates[accudelta] = (self._rho * accudelta
+ (1 - self._rho) * dx * dx)
updates[param] = param + dx
updates[accugrad] = agrad
train_fn = theano.function(inputs=[theano.Param(batch_x),
theano.Param(batch_y)],
outputs=self.mean_cost,
updates=updates,
givens={self.x: batch_x, self.y: batch_y})
return train_fn
示例4: transform
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def transform(self, X, target_layer_name, y=None):
target_layer = self.layers_[target_layer_name]
layers = self.layers_
input_layers = [
layer for layer in layers.values()
if isinstance(layer, nn.layers.InputLayer)
]
X_inputs = [
theano.Param(input_layer.input_var, name=input_layer.name)
for input_layer in input_layers
]
target_layer_output = nn.layers.get_output(
target_layer, None, deterministic=True
)
transform_iter = theano.function(
inputs=X_inputs,
outputs=target_layer_output,
allow_input_downcast=True,
)
outputs = []
for Xb, yb in self.batch_iterator_test(X):
outputs.append(self.apply_batch_func(transform_iter, Xb))
return np.vstack(outputs)
示例5: get_rmsprop_trainer
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def get_rmsprop_trainer(self, with_step_adapt=True, nesterov=False): # TODO Nesterov momentum
""" Returns an RmsProp (possibly Nesterov) (Sutskever 2013) trainer
using self._rho, self._eps and self._momentum params. """
# TODO CHECK
batch_x = T.fmatrix('batch_x')
batch_y = T.ivector('batch_y')
learning_rate = T.fscalar('lr') # learning rate
gparams = T.grad(self.mean_cost, self.params)
updates = OrderedDict()
for accugrad, avggrad, accudelta, sa, param, gparam in zip(
self._accugrads, self._avggrads, self._accudeltas,
self._stepadapts, self.params, gparams):
acc_grad = self._rho * accugrad + (1 - self._rho) * gparam * gparam
avg_grad = self._rho * avggrad + (1 - self._rho) * gparam # this decay/discount (self._rho) should differ from the one of the line above
###scaled_grad = gparam / T.sqrt(acc_grad + self._eps) # original RMSprop gradient scaling
scaled_grad = gparam / T.sqrt(acc_grad - avg_grad**2 + self._eps) # Alex Graves' RMSprop variant (divide by a "running stddev" of the updates)
if with_step_adapt:
incr = sa * (1. + self._stepadapt_alpha)
#decr = sa * (1. - self._stepadapt_alpha)
decr = sa * (1. - 2*self._stepadapt_alpha)
###steps = sa * T.switch(accudelta * -gparam >= 0, incr, decr)
steps = T.clip(T.switch(accudelta * -gparam >= 0, incr, decr), self._eps, 1./self._eps) # bad overloading of self._eps!
scaled_grad = steps * scaled_grad
updates[sa] = steps
dx = self._momentum * accudelta - learning_rate * scaled_grad
updates[param] = param + dx
updates[accugrad] = acc_grad
updates[avggrad] = avg_grad
updates[accudelta] = dx
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
示例6: score_classif
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def score_classif(self, given_set):
""" Returns functions to get current classification errors. """
batch_x = T.fmatrix('batch_x')
batch_y = T.ivector('batch_y')
score = theano.function(inputs=[theano.Param(batch_x),
theano.Param(batch_y)],
outputs=self.errors,
givens={self.x: batch_x, self.y: batch_y})
def scoref():
""" returned function that scans the entire set given as input """
return [score(batch_x, batch_y) for batch_x, batch_y in given_set]
return scoref
示例7: pretraining_functions
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def pretraining_functions(self, pretrain_x, batch_size):
index = T.lscalar('index') # index to a minibatch
corruption_level = T.scalar('corruption') # % of corruption
learning_rate = T.scalar('lr') # learning rate
if batch_size:
# begining of a batch, given `index`
batch_begin = index * batch_size
# ending of a batch given `index`
batch_end = batch_begin + batch_size
pretrain_x = pretrain_x[batch_begin: batch_end]
pretrain_fns = []
is_train = numpy.cast['int32'](0) # value does not matter
for dA_layer in self.dA_layers:
# get the cost and the updates list
cost, updates = dA_layer.get_cost_updates(corruption_level,
learning_rate)
# compile the theano function
fn = theano.function(
on_unused_input='ignore',
inputs=[
index,
theano.Param(corruption_level, default=0.2),
theano.Param(learning_rate, default=0.1)
],
outputs=cost,
updates=updates,
givens={
self.x: pretrain_x,
self.is_train: is_train
}
)
pretrain_fns.append(fn)
return pretrain_fns
示例8: build_finetune_functions_kaldi
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def build_finetune_functions_kaldi(self, train_shared_xy, valid_shared_xy):
(train_set_x, train_set_y) = train_shared_xy
(valid_set_x, valid_set_y) = valid_shared_xy
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=[theano.Param(learning_rate, default = 0.0001),
theano.Param(momentum, default = 0.5)],
outputs=self.errors,
updates=updates,
givens={self.x: train_set_x, self.y: train_set_y})
valid_fn = theano.function(inputs=[],
outputs=self.errors,
givens={self.x: valid_set_x, self.y: valid_set_y})
return train_fn, valid_fn
示例9: build_finetune_functions
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size):
(train_set_x, train_set_y) = train_shared_xy
(valid_set_x, valid_set_y) = valid_shared_xy
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]
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
示例10: pretraining_functions
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def pretraining_functions(self, train_set_x, batch_size, k , weight_cost):
index = T.lscalar('index')
momentum = T.scalar('momentum')
learning_rate = T.scalar('lr')
# number of mini-batches
n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
# start and end index of this mini-batch
batch_begin = index * batch_size
batch_end = batch_begin + batch_size
pretrain_fns = []
for rbm in self.rbm_layers:
r_cost, fe_cost, updates = rbm.get_cost_updates(batch_size, learning_rate,
momentum, weight_cost,
persistent=None, k = k)
# compile the theano function
fn = theano.function(inputs=[index,
theano.Param(learning_rate, default=0.0001),
theano.Param(momentum, default=0.5)],
outputs= [r_cost, fe_cost],
updates=updates,
givens={self.x: train_set_x[batch_begin:batch_end]})
# append function to the list of functions
pretrain_fns.append(fn)
return pretrain_fns
示例11: build_finetune_functions
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size):
(train_set_x, train_set_y) = train_shared_xy
(valid_set_x, valid_set_y) = valid_shared_xy
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]
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
示例12: pretraining_functions
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def pretraining_functions(self, train_set_x, batch_size):
# index to a [mini]batch
index = T.lscalar('index') # index to a minibatch
corruption_level = T.scalar('corruption') # % of corruption to use
learning_rate = T.scalar('lr') # learning rate to use
# number of batches
n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
# begining of a batch, given `index`
batch_begin = index * batch_size
# ending of a batch given `index`
batch_end = batch_begin + batch_size
pretrain_fns = []
for dA in self.dA_layers:
# get the cost and the updates list
cost, updates = dA.get_cost_updates(corruption_level,
learning_rate)
# compile the theano function
fn = theano.function(inputs=[index,
theano.Param(corruption_level, default=0.2),
theano.Param(learning_rate, default=0.1)],
outputs=cost,
updates=updates,
givens={self.x: train_set_x[batch_begin:
batch_end]})
# append `fn` to the list of functions
pretrain_fns.append(fn)
return pretrain_fns
示例13: plot_features
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
def plot_features(subject, data_path, model_path, test_labels, dataset='test'):
with open(model_path + '/' + subject + '.pickle', 'rb') as f:
state_dict = cPickle.load(f)
cnn = ConvNet(state_dict['params'])
cnn.set_weights(state_dict['weights'])
scalers = state_dict['scalers']
if dataset == 'test':
d = load_test_data(data_path, subject)
x = d['x']
y = test_labels['preictal']
elif dataset == 'train':
d = load_train_data(data_path, subject)
x, y = d['x'], d['y']
else:
raise ValueError('dataset')
x, _ = scale_across_time(x, x_test=None, scalers=scalers) if state_dict['params']['scale_time'] \
else scale_across_features(x, x_test=None, scalers=scalers)
cnn.batch_size.set_value(x.shape[0])
get_features = theano.function([cnn.x, Param(cnn.training_mode, default=0)], cnn.feature_extractor.output,
allow_input_downcast=True)
logits_test = get_features(x)
model = TSNE(n_components=2, random_state=0)
z = model.fit_transform(np.float64(logits_test))
plt.scatter(z[:, 0], z[:, 1], s=60, c=y)
plt.show()
示例14: get_SGD_trainer
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
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
示例15: get_adagrad_trainer
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Param [as 别名]
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
if self.max_norm:
W = param + dx
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 + 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