我们从Python开源项目中,提取了以下44个代码示例,用于说明如何使用theano.tensor.fscalar()。
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
def sample_old(self, x, sigma, n_steps): # Enable on-the-fly graph computations # theano.config.compute_test_value = "raise" # in_val = T.fmatrix('input_values") # in_val.tag.test_value = np.asarray( # np.random.rand(1, 784), dtype=theano.config.floatX) # s_sigma = T.fscalar("sigma_value") # s_sigma = np.asarray( # np.random.rand(1), dtype=theano.config.floatX) # mode = "FAST_RUN" samples = [] sample = x samples.append(x) for i in xrange(n_steps): print "Sample %d ..." % i sampler = self.sample_one_step(sample, sigma) sample = sampler.eval() samples.append(sample) return samples
def __init__(self, data_manager, t_layer_sizes, p_layer_sizes, dropout=0): print('{:25}'.format("Initializing Model"), end='', flush=True) self.t_layer_sizes = t_layer_sizes self.p_layer_sizes = p_layer_sizes self.dropout = dropout self.data_manager = data_manager self.t_input_size = self.data_manager.f.feature_count self.output_size = self.data_manager.s.information_count self.time_model = StackedCells(self.t_input_size, celltype=LSTM, layers = t_layer_sizes) self.time_model.layers.append(PassthroughLayer()) p_input_size = t_layer_sizes[-1] + self.output_size self.pitch_model = StackedCells( p_input_size, celltype=LSTM, layers = p_layer_sizes) self.pitch_model.layers.append(Layer(p_layer_sizes[-1], self.output_size, activation = T.nnet.sigmoid)) self.conservativity = T.fscalar() self.srng = T.shared_randomstreams.RandomStreams(np.random.randint(0, 1024)) self.epsilon = np.spacing(np.float32(1.0)) print("Done")
def setUp(self): super(TestPdbBreakpoint, self).setUp() # Sample computation that involves tensors with different numbers # of dimensions self.input1 = T.fmatrix() self.input2 = T.fscalar() self.output = T.dot((self.input1 - self.input2), (self.input1 - self.input2).transpose()) # Declare the conditional breakpoint self.breakpointOp = PdbBreakpoint("Sum of output too high") self.condition = T.gt(self.output.sum(), 1000) (self.monitored_input1, self.monitored_input2, self.monitored_output) = self.breakpointOp(self.condition, self.input1, self.input2, self.output)
def setUp(self): self.iv = T.tensor(dtype='int32', broadcastable=(False,)) self.fv = T.tensor(dtype='float32', broadcastable=(False,)) self.fv1 = T.tensor(dtype='float32', broadcastable=(True,)) self.dv = T.tensor(dtype='float64', broadcastable=(False,)) self.dv1 = T.tensor(dtype='float64', broadcastable=(True,)) self.cv = T.tensor(dtype='complex64', broadcastable=(False,)) self.zv = T.tensor(dtype='complex128', broadcastable=(False,)) self.fv_2 = T.tensor(dtype='float32', broadcastable=(False,)) self.fv1_2 = T.tensor(dtype='float32', broadcastable=(True,)) self.dv_2 = T.tensor(dtype='float64', broadcastable=(False,)) self.dv1_2 = T.tensor(dtype='float64', broadcastable=(True,)) self.cv_2 = T.tensor(dtype='complex64', broadcastable=(False,)) self.zv_2 = T.tensor(dtype='complex128', broadcastable=(False,)) self.fm = T.fmatrix() self.dm = T.dmatrix() self.cm = T.cmatrix() self.zm = T.zmatrix() self.fa = T.fscalar() self.da = T.dscalar() self.ca = T.cscalar() self.za = T.zscalar()
def test_param_allow_downcast_floatX(self): a = tensor.fscalar('a') b = tensor.fscalar('b') c = tensor.fscalar('c') f = pfunc([In(a, allow_downcast=True), In(b, allow_downcast=False), In(c, allow_downcast=None)], (a + b + c)) # If the values can be accurately represented, everything is OK assert numpy.all(f(0, 0, 0) == 0) # If allow_downcast is True, idem assert numpy.allclose(f(0.1, 0, 0), 0.1) # If allow_downcast is False, nope self.assertRaises(TypeError, f, 0, 0.1, 0) # If allow_downcast is None, it should work iff floatX=float32 if config.floatX == 'float32': assert numpy.allclose(f(0, 0, 0.1), 0.1) else: self.assertRaises(TypeError, f, 0, 0, 0.1)
def test_grad_dtype_change(self): x = tensor.fscalar('x') y = tensor.fscalar('y') c = tensor.iscalar('c') def inner_fn(cond, x, y): new_cond = tensor.cast(tensor.switch(cond, x, y), 'int32') new_x = tensor.switch(cond, tensor.nnet.sigmoid(y * x), x) new_y = tensor.switch(cond, y, tensor.nnet.sigmoid(x)) return new_cond, new_x, new_y values, _ = theano.scan( inner_fn, outputs_info=[c, x, y], n_steps=10, truncate_gradient=-1, go_backwards=False) gX, gY = tensor.grad(values[1].sum(), [x, y]) f = theano.function([c, x, y], [gX, gY], allow_input_downcast=True) # Check for runtime errors f(numpy.int32(0), numpy.float32(1.), numpy.float32(.5))
def test_gpualloc_output_to_gpu(): a_val = numpy.asarray(numpy.random.rand(4, 5), dtype='float32') a = tcn.shared_constructor(a_val) b = T.fscalar() f = theano.function([b], T.ones_like(a) + b, mode=mode_without_gpu) f_gpu = theano.function([b], B.gpu_from_host(T.ones_like(a)) + b, mode=mode_with_gpu) f(2) f_gpu(2) assert sum([node.op == T.alloc for node in f.maker.fgraph.toposort()]) == 1 assert sum([node.op == B.gpu_alloc for node in f_gpu.maker.fgraph.toposort()]) == 1 assert numpy.allclose(numpy.ones(a.get_value(borrow=True).shape) + 9, f_gpu(9)) assert numpy.allclose(f(5), f_gpu(5))
def get_tile_coder(min_val, max_val, num_tiles, num_tilings, num_features, learning_rate): # x.shape: (num_features), y.shape: () x = T.fvector('x') y = T.fscalar('y') tile_coding_layer = TileCodingLayer( min_val=min_val, max_val=max_val, num_tiles=num_tiles, num_tilings=num_tilings, num_features=num_features) # quantized_x q_x = tile_coding_layer.quantize(x) y_hat = tile_coding_layer.approximate(q_x) updates = tile_coding_layer.update_rule(y, y_hat, 0.1) train = theano.function([x, y], y_hat, updates=updates, allow_input_downcast=True) eval_ = theano.function([x], y_hat, allow_input_downcast=True) return train, eval_
def optimize_function(model, solver_params, config=None): """ Create a optimizing function receives gradients. Parameters: params - parameters config - training configuration Returns: updating function receives gradients """ gradients_ = [dim_to_var(p.ndim) for p in model.params] lr_ = tt.fscalar('lr_') updates = optimizers.optimizer(lr=lr_, model=model, gradients=gradients_, solver_params=solver_params) return t.function(inputs=[lr_]+ gradients_, outputs=[], updates=updates)
def __init__(self, game_params, arch_params, solver_params, trained_model, sn_dir): params=None if trained_model: params = common.load_params(trained_model) self.lr_func = create_learning_rate_func(solver_params) self.v_h_0 = tt.fvector('v_h_0') self.x_h_0 = tt.fvector('x_h_0') self.v_t_0 = tt.fmatrix('v_t_0') self.x_t_0 = tt.fmatrix('x_t_0') self.a_t_0 = tt.fmatrix('a_t_0') self.is_aggressive = tt.fmatrix('is_aggressive') self.lr_ = tt.fscalar('lr') self.n_steps_ = tt.iscalar('n_steps') self.sn_dir = sn_dir self.game_params = game_params self.arch_params = arch_params self.solver_params = solver_params self.model = CONTROLLER(self.v_h_0, self.x_h_0, self.v_t_0, self.x_t_0, self.a_t_0, self.is_aggressive, self.lr_, self.n_steps_, self.game_params, self.arch_params, self.solver_params, params)
def setUp(self): self.initial = np.array([0.1, 0.1], dtype='float32') x = theano.shared(self.initial) self.params = [x] left_bound = T.fscalar('left bound') right_bound = T.fscalar('right bound') self.inputs = [left_bound, right_bound] y = T.sum(x) loss = -T.log(y - left_bound) - T.log(right_bound - y) + 1.0e-3 * T.sum(x ** 2) self.loss = loss x0 = (0.01 + 0.011 + 2.0 + 2.1) / 4.0 self.approx_solution = np.array([x0 / 2, x0 / 2], dtype='float32') self.get_inputs = lambda : [ np.float32(np.random.uniform(0.01, 0.011)), np.float32(np.random.uniform(2.0, 2.1)), ] x_sub = T.fvector('x sub') self.get_loss = theano.function([x_sub] + self.inputs, self.loss, givens=[(self.params[0], x_sub)])
def genLossAndGradient(self): # establish loss kl_div = lasagne.layers.get_output(self.kl_loss_layer, deterministic=False) kl_loss = lasagne.objectives.aggregate(kl_div, mode='sum') # assume the reconstructed all with standard Gaussian distribution recons_loss = lasagne.objectives.squared_error(self.recons_var, self.pose_input_var) recons_loss = recons_loss*0.5 recons_loss = lasagne.objectives.aggregate(recons_loss, mode='sum') # calculate gradient loss = kl_loss + recons_loss # loss = recons_loss lr_var = T.fscalar('lr') update_params = self.encoder_params + self.decoder_params update_vars = lasagne.updates.adam(loss, update_params, lr_var, self.b1) # compile the function self.train_fn = theano.function( [self.pose_input_var, self.noise_input_var, lr_var], loss, updates = update_vars) self.recons_fn = theano.function( [self.pose_input_var, self.noise_input_var], self.recons_tvar ) self.encode_fn = theano.function( [self.pose_input_var, self.noise_input_var], self.z_tvar ) print '[PoseVAE]function compiled'
def __init__(self, classifier, criterion, learning_rate, trainset, clip_threshold=0): self.eta = learning_rate self.is_weighted = trainset.is_weighted self.trainset = trainset if clip_threshold > 0: gparams = [T.clip(T.grad(criterion.cost, param), -clip_threshold, clip_threshold) for param in classifier.params] else: gparams = [T.grad(criterion.cost, param) for param in classifier.params] lr = T.fscalar() updates = [ (param, param - lr * gparam) for param, gparam in zip(classifier.params, gparams) ] x = classifier.input y = criterion.y if self.is_weighted: w = criterion.w self.step_func = theano.function( inputs=[x, y, w, lr], outputs=[criterion.cost] + gparams, updates=updates, ) else: self.step_func = theano.function( inputs=[x, y, lr], outputs=[criterion.cost] + gparams, updates=updates, )
def train_linreg(X_train, y_train, eta, epochs): costs = [] eta0 = T.fscalar('eta0') y = T.fvector(name='y') X = T.fmatrix(name='X') w = theano.shared( np.zeros(shape=(X_train.shape[1] + 1), dtype=theano.config.floatX), name='w', ) net_input = T.dot(X, w[1:]) + w[0] errors = y - net_input cost = T.sum(T.pow(errors, 2)) gradient = T.grad(cost, wrt=w) update = [(w, w - (eta0 * gradient))] train = theano.function( inputs=[eta0], outputs=cost, updates=update, givens={X: X_train, y: y_train}, ) for _ in range(epochs): costs.append(train(eta)) return costs, w
def __build_free_phase(self): n_iterations = T.iscalar('n_iterations') epsilon = T.fscalar('epsilon') def step(*layers): E_sum = T.sum(self.__energy(layers)) layers_dot = T.grad(-E_sum, list(layers)) # temporal derivative of the state (free trajectory) layers_new = [layers[0]]+[T.clip(layer+epsilon*dot,0.,1.) for layer,dot in zip(layers,layers_dot)][1:] return layers_new ( layers, updates ) = theano.scan( step, outputs_info=self.layers, n_steps=n_iterations ) layers_end = [layer[-1] for layer in layers] for particles,layer,layer_end in zip(self.persistent_particles,self.layers[1:],layers_end[1:]): updates[particles] = T.set_subtensor(layer,layer_end) free_phase = theano.function( inputs=[n_iterations,epsilon], outputs=[], updates=updates ) return free_phase
def __build_positive_phase(self): n_iterations = T.iscalar('n_iterations') alphas = [T.fscalar("alpha_W"+str(r+1)) for r in range(len(self.weights))] def backprop(*layers): layers_new = [layers[-1]] for k in range(len(self.layers)-2,0,-1): layers_new += [pi(T.dot(layers[k-1], self.weights[k-1]) + T.dot(layers_new[-1], self.weights[k].T) + self.biases[k])] layers_new += [layers[0]] layers_new.reverse() return layers_new ( layers, updates ) = theano.scan( backprop, outputs_info=self.layers[:-1]+[self.y_data_one_hot], n_steps=n_iterations ) layers_new = [layer[-1] for layer in layers] Delta_layers = [(layer_new-layer) for layer_new,layer in zip(layers_new[1:],self.layers[1:])] biases_new = [b + alpha * T.mean(Delta, axis=0) for b,alpha,Delta in zip(self.biases[1:],alphas,Delta_layers)] weights_new = [W + alpha * T.dot(layer.T, Delta) / T.cast(self.layers[0].shape[0], dtype=theano.config.floatX) for W, alpha, layer, Delta in zip(self.weights, alphas, self.layers[:-1], Delta_layers)] for bias, bias_new in zip(self.biases[1:],biases_new): updates[bias]=bias_new for weight, weight_new in zip(self.weights,weights_new): updates[weight]=weight_new positive_phase = theano.function( inputs=[n_iterations]+alphas, outputs=[], updates=updates ) return positive_phase
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)
def test_copy_share_memory(self): x = T.fscalar('x') # SharedVariable for tests, one of them has update y = theano.shared(value=1) z = theano.shared(value=2) out = T.tanh((x + y + 2) / (x + z - 0.2)**2) # Test for different linkers for mode in ["FAST_RUN", "FAST_COMPILE"]: ori = theano.function([x], [out], mode=mode, updates={z: z + 1}) cpy = ori.copy(share_memory=True) # Test if memories shared storage_map_ori = ori.fn.storage_map storage_map_cpy = cpy.fn.storage_map fgraph_cpy = cpy.maker.fgraph # Assert intermediate and Constants storages are shared. # and output stoarges are not shared i_o_variables = fgraph_cpy.inputs + fgraph_cpy.outputs ori_storages = storage_map_ori.values() l = [val for key, val in storage_map_cpy.items() if key not in i_o_variables or isinstance(key, theano.tensor.Constant)] for storage in l: self.assertTrue(any([storage is s for s in ori_storages])) # Assert storages of SharedVariable without updates are shared for (input, _1, _2), here, there in zip(ori.indices, ori.input_storage, cpy.input_storage): self.assertTrue(here.data is there.data)
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)
def test_allow_downcast_floatX(self): a = tensor.fscalar('a') b = tensor.fvector('b') f = pfunc([a, b], (a + b), allow_input_downcast=True) g = pfunc([a, b], (a + b), allow_input_downcast=False) h = pfunc([a, b], (a + b), allow_input_downcast=None) # If the values can be accurately represented, OK assert numpy.all(f(0, [0]) == 0) assert numpy.all(g(0, [0]) == 0) assert numpy.all(h(0, [0]) == 0) # For the vector: OK iff allow_input_downcast is True assert numpy.allclose(f(0, [0.1]), 0.1) self.assertRaises(TypeError, g, 0, [0.1]) self.assertRaises(TypeError, h, 0, [0.1]) # For the scalar: OK if allow_input_downcast is True, # or None and floatX==float32 assert numpy.allclose(f(0.1, [0]), 0.1) self.assertRaises(TypeError, g, 0.1, [0]) if config.floatX == 'float32': assert numpy.allclose(h(0.1, [0]), 0.1) else: self.assertRaises(TypeError, h, 0.1, [0])
def test_scalar(self): x = cuda.fscalar() y = numpy.array(7, dtype='float32') assert y.size == theano.function([x], x.size)(y)
def setUp(self): self.initial = -2.0 self.approx_solution = 0.0 x = theano.shared(np.array(self.initial, dtype='float32')) self.params = [x] self.inputs = [] loss = -T.nnet.sigmoid(10.0 * x) * T.nnet.sigmoid(-10.0 * x) self.loss = loss x_sub = T.fscalar('x sub') self.get_loss = theano.function([x_sub] + self.inputs, self.loss, givens=[(self.params[0], x_sub)])
def _train_procedures(self): self.learning_rate = T.fscalar('learning rate') self.grads_generator = theano.grad(self.loss_generator, self.params_generator) self.grads_generator_clipped = updates.total_norm_constraint( self.grads_generator, max_norm=self.grad_clip_norm ) upd_generator = updates.sgd( self.grads_generator_clipped, self.params_generator, learning_rate=self.learning_rate ) self.train_generator = theano.function( [self.X_geant_raw, self.learning_rate], self.loss_pseudo, updates=upd_generator ) self.grads_discriminator = theano.grad(self.loss_discriminator, self.params_discriminator) self.grads_discriminator_clipped = updates.total_norm_constraint( self.grads_discriminator, max_norm=self.grad_clip_norm ) upd_discriminator = updates.sgd( self.grads_discriminator_clipped, self.params_discriminator, learning_rate=self.learning_rate ) self.train_discriminator = theano.function( [self.X_geant_raw, self.X_real_raw, self.learning_rate], [self.loss_pseudo, self.loss_real], updates=upd_discriminator ) self.anneal_discriminator = nn.updates.sa( [self.X_geant_raw, self.X_real_raw], self.loss_discriminator, params=self.params_discriminator, **self.annealing_args )
def adastep( inputs, loss, params, outputs=(), max_iter=8, rho = 0.9, momentum=None, initial_learning_rate = 1.0e-3, max_learning_rate=1.0, max_delta = 1.0e-1, eps=1.0e-6): cache_inputs, cache_grads, get_loss, set_params = grad_base( inputs, loss, params, outputs, norm_gradients=False, momentum=momentum ) one = T.constant(1.0, dtype='float32') v = theano.shared( np.float32(initial_learning_rate), name = 'v' ) new_v = T.fscalar() upd_v = OrderedDict() upd_v[v] = v * rho + new_v * (one - rho) update_v = theano.function([new_v], v, updates=upd_v, no_default_updates=True) get_v = theano.function([], v, no_default_updates=True) return _adastep( cache_inputs, cache_grads, get_loss, set_params, get_v, update_v, max_iter=max_iter, max_learning_rate=max_learning_rate, max_delta=max_delta, eps=eps )
def optimize_gan_hkl(self, model, lam1=0.00001): """ optimizer for hkl packaged dataset. Returns the updates for discirminator & generator and computed costs for the model. """ i = T.iscalar('i'); lr = T.fscalar('lr'); Xu = T.fmatrix('X'); cost_disc = model.cost_dis(Xu, self.batch_sz) \ + lam1 * model.dis_network.weight_decay_l2() gparams_dis = T.grad(cost_disc, model.dis_network.params) cost_gen = model.cost_gen(self.batch_sz) gparams_gen = T.grad(cost_gen, model.gen_network.params) updates_dis = self.ADAM(model.dis_network.params, gparams_dis, lr) updates_gen = self.ADAM(model.gen_network.params, gparams_gen, lr) discriminator_update = theano.function([Xu, theano.Param(lr,default=self.epsilon_dis)],\ outputs=cost_disc, updates=updates_dis) generator_update = theano.function([theano.Param(lr,default=self.epsilon_gen)],\ outputs=cost_gen, updates=updates_gen) get_valid_cost = theano.function([Xu], outputs=[cost_disc, cost_gen]) get_test_cost = theano.function([Xu], outputs=[cost_disc, cost_gen]) return discriminator_update, generator_update, get_valid_cost, get_test_cost
def optimize_gan(self, model, train_set, valid_set, test_set, lam1=0.00001): """ optimizer for non packaged dataset, returning updates for discriminator & generator, as well as the computed costs. """ i = T.iscalar('i'); lr = T.fscalar('lr'); Xu = T.matrix('X'); cost_disc = model.cost_dis(Xu, self.batch_sz) \ + lam1 * model.dis_network.weight_decay_l2() gparams_dis = T.grad(cost_disc, model.dis_network.params) cost_gen = model.cost_gen(self.batch_sz) gparams_gen = T.grad(cost_gen, model.gen_network.params) updates_dis = self.ADAM(model.dis_network.params, gparams_dis, lr) updates_gen = self.ADAM(model.gen_network.params, gparams_gen, lr) discriminator_update = theano.function([i, theano.Param(lr,default=self.epsilon_dis)],\ outputs=cost_disc, updates=updates_dis,\ givens={Xu:train_set[0][i*self.batch_sz:(i+1)*self.batch_sz]}) generator_update = theano.function([theano.Param(lr,default=self.epsilon_gen)],\ outputs=cost_gen, updates=updates_gen) get_valid_cost = theano.function([i], outputs=[cost_disc, cost_gen],\ givens={Xu:valid_set[0][i*self.batch_sz:(i+1)*self.batch_sz]}) get_test_cost = theano.function([i], outputs=[cost_disc, cost_gen],\ givens={Xu:test_set[0][i*self.batch_sz:(i+1)*self.batch_sz]}) return discriminator_update, generator_update, get_valid_cost, get_test_cost
def get_train_model(data, inputs, loss, params, batch_size=32): """ trainer: Function to define the trainer of the model on the data set that bassed as the parameters of the function parameters: contexts: List of the contexts (the input of the trainer) targets: List of the targets. return: Theano function represents the train model """ data_contexts = data[0] data_targets = data[1] context = inputs[0] target = inputs[1] learning_rate = T.fscalar('learning_rate') # theano input: the learning rate, the value of this input # can be constant like 0.1 or #it can be come from a function like a decay learning rate function index = T.lscalar('index') # the index of minibatch g_params = T.grad(cost=loss, wrt=params) updates = [ (param, param - learning_rate * gparam) for param, gparam in zip(params, g_params) ] train_fun = theano.function( [index, learning_rate], loss, updates=updates, givens={ context: data_contexts[index * args.batch_size: (index + 1) * args.batch_size], target: data_targets[index * args.batch_size: (index + 1) * args.batch_size] } ) return train_fun
def __init__(self, classifier, criterion, learning_rate, trainset, clip_threshold=0): self.eta = learning_rate self.is_weighted = trainset.is_weighted if clip_threshold > 0: gparams = [T.clip(T.grad(criterion.cost, param), -clip_threshold, clip_threshold) for param in classifier.params] else: gparams = [T.grad(criterion.cost, param) for param in classifier.params] lr = T.fscalar() updates = [ (param, param - lr * gparam) for param, gparam in zip(classifier.params, gparams) ] index = T.lscalar() # index to a [mini]batch x = classifier.input y = criterion.y if self.is_weighted: w = criterion.w self.step_func = theano.function( inputs=[index, lr], outputs=[criterion.cost] + gparams, updates=updates, givens={ x: trainset.get_x(index), y: trainset.get_y(index), w: trainset.get_w(index) } ) else: self.step_func = theano.function( inputs=[index, lr], outputs=[criterion.cost] + gparams, updates=updates, givens={ x: trainset.get_x(index), y: trainset.get_y(index) } )
def __init__(self): # Define inputs input_var = T.ftensor4('input_var') # input images (batchx3x64x64) labels_classifier_var = T.ivector('labels_classifier_var') # labels for images labels_domain_var = T.ivector('labels_domain_var') # labels for domain (1 for source, 0 for target) learning_rate = T.fscalar('learning_rate') # Define classifier networks network_classifier = self.network_classifier(input_var) network_discriminator = self.network_discriminator(network_classifier['classifier/pool1']) # Define outputs prediction_classifier = get_output(network_classifier['classifier/output']) # prob image classification prediction_discriminator = get_output(network_discriminator['discriminator/output']) # prob image domain (should be 1 for source) # Define losses (objectives) loss_classifier_only = T.mean(categorical_crossentropy(prediction_classifier, labels_classifier_var) * labels_domain_var) loss_discriminator = T.mean(categorical_crossentropy(prediction_discriminator, labels_domain_var)) loss_classifier = loss_classifier_only - loss_discriminator # Define performance perf_classifier_only = categorical_accuracy(prediction_classifier, labels_classifier_var).mean() perf_discriminator = categorical_accuracy(prediction_discriminator, labels_domain_var).mean() # Define params params_classifier = lasagne.layers.get_all_params(network_classifier['classifier/output'], trainable=True) params_discriminator = lasagne.layers.get_all_params(network_discriminator['discriminator/output'], trainable=True) params_discriminator = [param for param in params_discriminator if 'discriminator' in param.name] # Define updates updates_classifier = lasagne.updates.adam(loss_classifier, params_classifier, learning_rate=learning_rate) updates_classifier_only = lasagne.updates.adam(loss_classifier_only, params_classifier, learning_rate=learning_rate) updates_discriminator = lasagne.updates.adam(loss_discriminator, params_discriminator, learning_rate=learning_rate) # Define training functions self.train_fn_classifier = theano.function( [input_var, labels_classifier_var, labels_domain_var, learning_rate], [loss_classifier, loss_classifier_only, prediction_classifier], updates=updates_classifier) self.train_fn_classifier_only = theano.function( [input_var, labels_classifier_var, labels_domain_var, learning_rate], [loss_classifier, loss_classifier_only, prediction_classifier], updates=updates_classifier_only) self.train_fn_discriminator = theano.function( [input_var, labels_domain_var, learning_rate], [loss_discriminator, prediction_discriminator], updates=updates_discriminator) # Define validation functions self.valid_fn_classifier = theano.function( [input_var, labels_classifier_var], [perf_classifier_only, prediction_classifier]) self.valid_fn_discriminator = theano.function( [input_var, labels_domain_var], [perf_discriminator, prediction_discriminator])
def __init__(self, layer_sizes, n_samples, alpha, learning_rate, v_prior, batch_size, X_train, y_train, N_train): layer_sizes = copy.copy(layer_sizes) layer_sizes[ 0 ] = layer_sizes[ 0 ] + 1 print layer_sizes self.batch_size = batch_size self.N_train = N_train self.X_train = X_train self.y_train = y_train self.rate = learning_rate # We create the network self.network = network.Network(layer_sizes, n_samples, v_prior, N_train) # index to a batch index = T.lscalar() self.indexes = T.vector('index', dtype = 'int32') indexes_train = theano.shared(value = np.array(range(0, N_train), dtype = np.int32), borrow = True) self.x = T.tensor3('x',dtype=theano.config.floatX) self.y = T.matrix('y', dtype =theano.config.floatX) self.lr = T.fscalar() # The logarithm of the values for the likelihood factors sampl = T.bscalar() self.fwpass = theano.function(outputs=self.network.output(self.x,False,samples=sampl,use_indices=False), inputs=[self.x,sampl],allow_input_downcast=True) ll_train = self.network.log_likelihood_values(self.x, self.y, self.indexes, 0.0, 1.0) self.estimate_marginal_ll = (-1.0 * N_train / (self.x.shape[ 1 ] * alpha) * \ T.sum(LogSumExp(alpha * (T.sum(ll_train, 2) - self.network.log_f_hat() - self.network.log_f_hat_z()), 0)+ \ T.log(1.0 / n_samples)) - self.network.log_normalizer_q() - 1.0 * N_train / self.x.shape[ 1 ] * self.network.log_normalizer_q_z() + \ self.network.log_Z_prior()) # We create a theano function for updating q upd = adam(self.estimate_marginal_ll, self.network.params,indexes_train[index*batch_size:(index+1)*batch_size],self.rate,rescale_local=np.float32(N_train/batch_size)) self.process_minibatch = theano.function([ index], self.estimate_marginal_ll, \ updates = upd, \ givens = { self.x: T.tile(self.X_train[ index * batch_size: (index + 1) * batch_size] , [ n_samples, 1, 1 ]), self.y: self.y_train[ index * batch_size: (index + 1) * batch_size ], self.indexes: indexes_train[ index * batch_size : (index + 1) * batch_size ] }) # We create a theano function for making predictions self.error_minibatch_train = theano.function([ index ], T.sum((T.mean(self.network.output(self.x,self.indexes), 0, keepdims = True)[ 0, :, : ] - self.y)**2) / layer_sizes[ -1 ], givens = { self.x: T.tile(self.X_train[ index * batch_size: (index + 1) * batch_size ], [ n_samples, 1, 1 ]), self.y: self.y_train[ index * batch_size: (index + 1) * batch_size ], self.indexes: indexes_train[ index * batch_size : (index + 1) * batch_size ] }) self.ll_minibatch_train = theano.function([ index ], T.sum(LogSumExp(T.sum(ll_train, 2), 0) + T.log(1.0 / n_samples)), \ givens = { self.x: T.tile(self.X_train[ index * batch_size: (index + 1) * batch_size ], [ n_samples, 1, 1 ]), self.y: self.y_train[ index * batch_size: (index + 1) * batch_size ], self.indexes: indexes_train[ index * batch_size : (index + 1) * batch_size ] })
def __build_weakly_clamped_phase(self): n_iterations = T.iscalar('n_iterations') epsilon = T.fscalar('epsilon') beta = T.fscalar('beta') alphas = [T.fscalar("alpha_W"+str(r+1)) for r in range(len(self.weights))] def step(*layers): F_sum = T.sum(self.__total_energy(layers, beta)) layers_dot = T.grad(-F_sum, list(layers)) # temporal derivative of the state (weakly clamped trajectory) layers_new = [layers[0]]+[T.clip(layer+epsilon*dot,0.,1.) for layer,dot in zip(layers,layers_dot)][1:] return layers_new ( layers, updates ) = theano.scan( step, outputs_info=self.layers, n_steps=n_iterations ) layers_weakly_clamped = [layer[-1] for layer in layers] E_mean_free = T.mean(self.__energy(self.layers)) E_mean_weakly_clamped = T.mean(self.__energy(layers_weakly_clamped)) biases_dot = T.grad( (E_mean_weakly_clamped-E_mean_free) / beta, self.biases, consider_constant=layers_weakly_clamped) weights_dot = T.grad( (E_mean_weakly_clamped-E_mean_free) / beta, self.weights, consider_constant=layers_weakly_clamped) biases_new = [b - alpha * dot for b,alpha,dot in zip(self.biases[1:],alphas,biases_dot[1:])] weights_new = [W - alpha * dot for W,alpha,dot in zip(self.weights, alphas,weights_dot)] Delta_log = [T.sqrt( ((W_new - W) ** 2).mean() ) / T.sqrt( (W ** 2).mean() ) for W,W_new in zip(self.weights,weights_new)] for bias, bias_new in zip(self.biases[1:],biases_new): updates[bias]=bias_new for weight, weight_new in zip(self.weights,weights_new): updates[weight]=weight_new weakly_clamped_phase = theano.function( inputs=[n_iterations, epsilon, beta]+alphas, outputs=Delta_log, updates=updates ) return weakly_clamped_phase
def test_local_merge_alloc(): # Add this opt to the default mode, # otherwise, FAST_COMPILE fails. default_mode = theano.compile.mode.get_default_mode() opt_mode = default_mode.including("local_merge_alloc") x = T.iscalar('x') y = T.iscalar('y') y2 = T.iscalar('y2') z = T.iscalar('z') w = T.iscalar('w') m = T.fscalar('m') # case 1 # Alloc(Alloc(m, x, 1, 1, 1), x, y, z, w) -> Alloc(m, x, y, z, w) output = T.alloc(T.alloc(m, 1, y, 1, 1), x, y, z, w) f = theano.function([m, x, y, z, w], output, mode=opt_mode) topo = f.maker.fgraph.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, T.Alloc) o = f(0., 1, 2, 3, 4) assert o.shape == (1, 2, 3, 4) # case 2 # Alloc(Alloc(m, y, 1, 1), x, y, z, w) -> Alloc(m, x, y, z, w) output = T.alloc(T.alloc(m, y, 1, 1), x, y, z, w) f = theano.function([m, x, y, z, w], output, mode=opt_mode) topo = f.maker.fgraph.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, T.Alloc) o = f(0., 1, 2, 3, 4) assert o.shape == (1, 2, 3, 4) # case 3 # Alloc(Alloc(m, y1, 1, 1), x, y2, z, w) -> # Alloc(m, x, assert(y1, y1==y2), z, w) output = T.alloc(T.alloc(m, y, 1, 1), x, y2, z, w) f = theano.function([m, x, y, y2, z, w], output, mode=opt_mode) topo = f.maker.fgraph.toposort() assert len(topo) == 3 assert isinstance(topo[-2].op, T.opt.Assert) assert isinstance(topo[-1].op, T.Alloc) o = f(0., 1, 2, 2, 3, 4) assert o.shape == (1, 2, 3, 4) assert_raises((AssertionError, ValueError), f, 0., 1, 2, 5, 3, 4)
def test_local_useless_alloc(): useless_alloc = out2in(local_useless_alloc) merge_alloc = out2in(local_merge_alloc) x = T.iscalar('x') y = T.iscalar('y') y2 = T.iscalar('y2') z = T.iscalar('z') w = T.iscalar('w') m = T.fscalar('m') # case 1 # Alloc(Alloc(m, x, 1, 1, 1), x, y, z, w) -> Alloc(m, x, y, z, w) output = T.alloc(T.alloc(m, 1, y, 1, 1), x, y, z, w) g = FunctionGraph([m, x, y, z, w], [output]) useless_alloc.optimize(g) merge_alloc.optimize(g) useless_alloc.optimize(g) topo = g.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, T.Alloc) # case 2 # Alloc(Alloc(m, y, 1, 1), x, y, z, w) -> Alloc(m, x, y, z, w) output = T.alloc(T.alloc(m, y, 1, 1), x, y, z, w) g = FunctionGraph([m, x, y, z, w], [output]) useless_alloc.optimize(g) merge_alloc.optimize(g) useless_alloc.optimize(g) topo = g.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, T.Alloc) # case 3 # Alloc(Alloc(m, y1, 1, 1), x, y2, z, w) -> # Alloc(m, x, assert(y1, y1==y2), z, w) output = T.alloc(T.alloc(m, y, 1, 1), x, y2, z, w) g = FunctionGraph([m, x, y, y2, z, w], [output]) useless_alloc.optimize(g) merge_alloc.optimize(g) useless_alloc.optimize(g) topo = g.toposort() assert len(topo) == 3 assert isinstance(topo[-2].op, T.opt.Assert) assert isinstance(topo[-1].op, T.Alloc)
def test_one_sequence_one_output_weights_gpu2(self): def f_rnn(u_t, x_tm1, W_in, W): return u_t * W_in + x_tm1 * W u = theano.tensor.fvector('u') x0 = theano.tensor.fscalar('x0') W_in = theano.tensor.fscalar('win') W = theano.tensor.fscalar('w') output, updates = theano.scan(f_rnn, u, x0, [W_in, W], n_steps=None, truncate_gradient=-1, go_backwards=False, mode=self.mode_with_gpu) f2 = theano.function([u, x0, W_in, W], output, updates=updates, allow_input_downcast=True, mode=self.mode_with_gpu) # get random initial values rng = numpy.random.RandomState(utt.fetch_seed()) v_u = rng.uniform(size=(4,), low=-5., high=5.) v_x0 = rng.uniform() W = rng.uniform() W_in = rng.uniform() # compute the output in numpy v_out = numpy.zeros((4,)) v_out[0] = v_u[0] * W_in + v_x0 * W for step in xrange(1, 4): v_out[step] = v_u[step] * W_in + v_out[step - 1] * W theano_values = f2(v_u, v_x0, W_in, W) utt.assert_allclose(theano_values, v_out) topo = f2.maker.fgraph.toposort() assert sum([isinstance(node.op, self.gpu_backend.HostFromGpu) for node in topo]) == 1 assert sum([isinstance(node.op, self.gpu_backend.GpuFromHost) for node in topo]) == 4 scan_node = [node for node in topo if isinstance(node.op, theano.scan_module.scan_op.Scan)] assert len(scan_node) == 1 scan_node = scan_node[0] scan_node_topo = scan_node.op.fn.maker.fgraph.toposort() # check that there is no gpu transfer in the inner loop. assert any([isinstance(node.op, self.gpu_backend.GpuElemwise) for node in scan_node_topo]) assert not any([isinstance(node.op, self.gpu_backend.HostFromGpu) for node in scan_node_topo]) assert not any([isinstance(node.op, self.gpu_backend.GpuFromHost) for node in scan_node_topo]) # This third test checks that scan can deal with a mixture of dtypes as # outputs when is running on GPU
def test_gpu3_mixture_dtype_outputs(self): def f_rnn(u_t, x_tm1, W_in, W): return (u_t * W_in + x_tm1 * W, tensor.cast(u_t + x_tm1, 'int64')) u = theano.tensor.fvector('u') x0 = theano.tensor.fscalar('x0') W_in = theano.tensor.fscalar('win') W = theano.tensor.fscalar('w') output, updates = theano.scan(f_rnn, u, [x0, None], [W_in, W], n_steps=None, truncate_gradient=-1, go_backwards=False, mode=self.mode_with_gpu) f2 = theano.function([u, x0, W_in, W], output, updates=updates, allow_input_downcast=True, mode=self.mode_with_gpu) # get random initial values rng = numpy.random.RandomState(utt.fetch_seed()) v_u = rng.uniform(size=(4,), low=-5., high=5.) v_x0 = rng.uniform() W = rng.uniform() W_in = rng.uniform() # compute the output in numpy v_out1 = numpy.zeros((4,)) v_out2 = numpy.zeros((4,), dtype='int64') v_out1[0] = v_u[0] * W_in + v_x0 * W v_out2[0] = v_u[0] + v_x0 for step in xrange(1, 4): v_out1[step] = v_u[step] * W_in + v_out1[step - 1] * W v_out2[step] = numpy.int64(v_u[step] + v_out1[step - 1]) theano_out1, theano_out2 = f2(v_u, v_x0, W_in, W) utt.assert_allclose(theano_out1, v_out1) utt.assert_allclose(theano_out2, v_out2) topo = f2.maker.fgraph.toposort() scan_node = [node for node in topo if isinstance(node.op, theano.scan_module.scan_op.Scan)] assert len(scan_node) == 1 scan_node = scan_node[0] assert self.is_scan_on_gpu(scan_node)
def test_dot22scalar(): 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 any([isinstance(n.op, tcn.blas.GpuDot22Scalar) for n in t]) # assert any([isinstance(n.op, tcn.basic_ops.GpuAllocEmpty) # for n in t]) 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 any([isinstance(n.op, tcn.blas.GpuDot22Scalar) for n in t]) # assert any([isinstance(n.op, tcn.basic_ops.GpuAllocEmpty) # for n in t]) assert numpy.allclose(f(av, bv, 0.5), f2(av, bv, 0.5)) f = theano.function([a, b, scalar], tensor.blas._dot22scalar(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)) cmp((3, 4), (4, 5)) cmp((0, 4), (4, 5)) cmp((3, 4), (4, 0)) cmp((3, 0), (0, 5)) cmp((0, 4), (4, 0)) cmp((0, 0), (0, 0))
def run_nnet(use_gpu, n_batch=60, n_in=1024, n_hid=2048, n_out=10, n_train=100): if config.mode == 'DEBUG_MODE': n_train = 1 if use_gpu: w = tcn.shared_constructor(0.01 * (my_rand(n_in, n_hid) - 0.5), 'w') b = tcn.shared_constructor(my_zeros(n_hid), 'b') v = tcn.shared_constructor(my_zeros((n_hid, n_out)), 'c') c = tcn.shared_constructor(my_zeros(n_out), 'c') else: w = shared(0.01 * (my_rand(n_in, n_hid) - 0.5), 'w') b = shared(my_zeros(n_hid), 'b') v = shared(my_zeros((n_hid, n_out)), 'c') c = shared(my_zeros(n_out), 'c') x = tensor.fmatrix('x') y = tensor.fmatrix('y') lr = tensor.fscalar('lr') hid = tensor.tanh(tensor.dot(x, w) + b) out = tensor.tanh(tensor.dot(hid, v) + c) loss = tensor.sum(0.5 * (out - y) ** 2 * lr) if 0: print('loss type', loss.type) params = [w, b, v, c] gparams = tensor.grad(loss, params) mode = get_mode(use_gpu) # print 'building pfunc ...' train = pfunc([x, y, lr], [loss], mode=mode, updates=[(p, p - g) for p, g in izip(params, gparams)]) if 0: for i, n in enumerate(train.maker.fgraph.toposort()): print(i, n) xval = my_rand(n_batch, n_in) yval = my_rand(n_batch, n_out) lr = theano._asarray(0.01, dtype='float32') t0 = time.time() rval = [] for i in xrange(n_train): rval.append(train(xval, yval, lr)) dt = time.time() - t0 return numpy.asarray(rval), dt
def run_conv_nnet1(use_gpu): if use_gpu: shared_fn = tcn.shared_constructor else: shared_fn = shared n_batch = 16 n_kern = 20 shape_img = (n_batch, 1, 32, 32) shape_kern = (n_kern, 1, 5, 5) n_train = 10 if config.mode == 'DEBUG_MODE': n_train = 1 logical_hid_shape = tcn.blas.GpuConv.logical_output_shape_2d( shape_img[2:], shape_kern[2:], 'valid') n_hid = n_kern * logical_hid_shape[0] * logical_hid_shape[1] n_out = 10 w = shared_fn(0.01 * (my_rand(*shape_kern) - 0.5), 'w') b = shared_fn(my_zeros((n_kern,)), 'b') v = shared_fn(my_zeros((n_hid, n_out)), 'c') c = shared_fn(my_zeros(n_out), 'c') x = tensor.Tensor(dtype='float32', broadcastable=(0, 1, 0, 0))('x') y = tensor.fmatrix('y') lr = tensor.fscalar('lr') conv_op = conv.ConvOp(shape_img[2:], shape_kern[2:], n_kern, n_batch, 1, 1) hid = tensor.tanh(conv_op(x, w) + b.dimshuffle((0, 'x', 'x'))) hid_flat = hid.reshape((n_batch, n_hid)) out = tensor.tanh(tensor.dot(hid_flat, v) + c) loss = tensor.sum(0.5 * (out - y) ** 2 * lr) # print 'loss type', loss.type params = [w, b, v, c] gparams = tensor.grad(loss, params) mode = get_mode(use_gpu) # print 'building pfunc ...' train = pfunc( [x, y, lr], [loss], mode=mode, updates=[(p, p - g) for p, g in zip(params, gparams)]) # for i, n in enumerate(train.maker.fgraph.toposort()): # print i, n xval = my_rand(*shape_img) yval = my_rand(n_batch, n_out) lr = theano._asarray(0.01, dtype='float32') for i in xrange(n_train): rval = train(xval, yval, lr) # print 'training done' return rval
def __init__(self, ob_space, action_space, **usercfg): """ Initialize your agent's parameters """ nO = ob_space.shape[0] nA = action_space.n # Here are all the algorithm parameters. You can modify them by passing in keyword args self.config = dict(episode_max_length=100, timesteps_per_batch=10000, n_iter=100, gamma=1.0, stepsize=0.05, nhid=20) self.config.update(usercfg) # Symbolic variables for observation, action, and advantage # These variables stack the results from many timesteps--the first dimension is the timestep ob_no = T.fmatrix() # Observation a_n = T.ivector() # Discrete action adv_n = T.fvector() # Advantage def shared(arr): return theano.shared(arr.astype('float64')) # Create weights of neural network with one hidden layer W0 = shared(np.random.randn(nO,self.config['nhid'])/np.sqrt(nO)) b0 = shared(np.zeros(self.config['nhid'])) W1 = shared(1e-4*np.random.randn(self.config['nhid'],nA)) b1 = shared(np.zeros(nA)) params = [W0, b0, W1, b1] # Action probabilities prob_na = T.nnet.softmax(T.tanh(ob_no.dot(W0)+b0[None,:]).dot(W1) + b1[None,:]) N = ob_no.shape[0] # Loss function that we'll differentiate to get the policy gradient # Note that we've divided by the total number of timesteps loss = T.log(prob_na[T.arange(N), a_n]).dot(adv_n) / N stepsize = T.fscalar() grads = T.grad(loss, params) # Perform parameter updates. # I find that sgd doesn't work well # updates = sgd_updates(grads, params, stepsize) updates = rmsprop_updates(grads, params, stepsize) self.pg_update = theano.function([ob_no, a_n, adv_n, stepsize], [], updates=updates, allow_input_downcast=True) self.compute_prob = theano.function([ob_no], prob_na, allow_input_downcast=True)
def __init__(self, game_params, arch_params, solver_params, trained_model, sn_dir): params=None if trained_model: params = common.load_params(trained_model) self.lr_func = create_learning_rate_func(solver_params) self.x_h_0 = tt.fvector('x_h_0') self.v_h_0 = tt.fvector('v_h_0') self.t_h_0 = tt.fvector('t_h_0') self.x_t_0 = tt.fmatrix('x_t_0') self.v_t_0 = tt.fmatrix('v_t_0') self.a_t_0 = tt.fmatrix('a_t_0') self.t_t_0 = tt.fvector('t_t_0') self.time_steps = tt.fvector('t_0') self.exist = tt.bvector('exist') self.is_leader = tt.fvector('is_leader') self.x_goal = tt.fvector('x_goal') self.turn_vec_h = tt.fvector('turn_vec_h') self.turn_vec_t = tt.fvector('turn_vec_t') self.n_steps = tt.iscalar('n_steps') self.lr = tt.fscalar('lr') self.sn_dir = sn_dir self.game_params = game_params self.arch_params = arch_params self.solver_params = solver_params self.model = CONTROLLER(self.x_h_0, self.v_h_0, self.t_h_0, self.x_t_0, self.v_t_0, self.a_t_0, self.t_t_0, self.time_steps, self.exist, self.is_leader, self.x_goal, self.turn_vec_h, self.turn_vec_t, self.n_steps, self.lr, self.game_params, self.arch_params, self.solver_params, params)
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)
def sa(inputs, loss, params, outputs = (), srng=None, seed=1122334455, iters=32, initial_temperature = 1.0e-1, learning_rate=1.0e-2): if srng is None: # from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams as RandomStreams from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams srng = srng or RandomStreams(seed=seed) inputs_cached = [ to_shared(i) for i in inputs ] input_setter = OrderedDict() for inpc, inp in zip(inputs_cached, inputs): input_setter[inpc] = inp memorize_inputs = theano.function(inputs, [], updates=input_setter, no_default_updates=True) inputs_givens = [ (inp, inpc) for inp, inpc in zip(inputs, inputs_cached) ] deltas = [ make_copy(param) for param in params ] alpha = T.fscalar('learning rate') delta_setter = OrderedDict([ (delta, make_uniform(delta, -alpha, alpha, srng)) for delta in deltas ]) generate_deltas = theano.function([alpha], [], updates=delta_setter, no_default_updates=False) probe_givens = [ (param, param + delta) for param, delta in zip(params, deltas) ] probe = theano.function( [], [loss] + list(outputs), givens=probe_givens + inputs_givens, no_default_updates=True ) params_setter = OrderedDict(probe_givens) set_params = theano.function( [], [], updates=params_setter, no_default_updates=True ) return simulated_annealing( probe, memorize_inputs, set_params, generate_deltas, iters, initial_temperature, learning_rate )