def train(self, x, y_r, a): if not hasattr(self, '_train'): self._compute = theano.function(inputs=[self.x, self.y_r, self.action_index], outputs=self.cost_all) self._train = theano.function(updater=self.opt) global mutex mutex.acquire() loss = self._compute(x, y_r, a) mutex.release() self._train() return loss
def predict_p_and_v(self, x): if not hasattr(self, '_predict_p_and_v'): self._predict_p_and_v = theano.function(inputs=self.x, outputs=[self.softmax_p, self.logits_v]) global mutex mutex.acquire() p, v = self._predict_p_and_v(x) mutex.release() return p, v
def run(self, feed_dict=None): # objective function if not hasattr(self, '_objective_func'): # find minimum solving targets targets = set() for t in self.objs: targets.add(t) if feed_dict is not None: self._objective_func = theano.function(inputs=feed_dict.keys(), outputs=list(targets)) else: self._objective_func = theano.function(outputs=list(targets)) if feed_dict is not None: self._objective_func(*feed_dict.values()) else: self._objective_func() # update function if not hasattr(self, '_update_func'): self._update_func = theano.function(updater=self.updater) self._update_func()
def BuildOptimizer(self): """Build the optimizer. Returns ------- None """ # collect for layer, blobs in self._net.params.items(): self._layer_blobs.extend(blobs) # push for idx, blob in enumerate(self._layer_blobs): if self._net._lr_mults[idx] > 0: if blob.diff is None: continue self._optimizer.append((blob.data, blob.diff), self._net._lr_mults[idx], self._net._decay_mults[idx]) self.update = theano.function(updater=self._optimizer)
def function(self, givens=None): """Returns the function the ``ForwardBackward``. Parameters ---------- givens : None or dict The givens to replace existing blobs. Returns ------- lambda The function. See Also -------- `theano.function(*args, **kwargs)`_ - How to make a graph. [**Theano Style**] References ---------- The implementation of `ForwardBackward(net.cpp, L85)`_. """ if hasattr(self, '_function'): return self._function for cost in self._costs: for wrt in self._wrts: T.grad(cost, wrt) if givens is not None: if not isinstance(givens, dict): raise TypeError('The givens should be a dict.') for k, v in givens.items(): if not isinstance(v, Tensor): raise ValueError('The value of givens should be a Tensor.') self._swap_tensors[k] = v self._function = \ theano.function(outputs=[self._blobs[name]['data'] for name in self._net_outputs], givens=self._swap_tensors) if hasattr(self, '_model'): ws.Restore(self._model, format='caffe') return self._function
def run(self, inputs, outputs): """ Run implement(i.e. forward-pass). Parameters ---------- inputs : sequence of strs Indicating the operator's inputs outputs : sequence of strs Indicating the operator's outputs Returns ------- None """ ws.FeedTensor(outputs[0], self._queue.get()) if __name__ == '__main__': # def y = ops.Run([], module=__name__, op='DataProcess', nout=1) foo = theano.function(outputs=y) # run foo() # fetch logger.info('y \n-------------- \n', y.get_value(), '\n')
def run(self): if not hasattr(self, '_init_func'): self._init_func = theano.function(outputs=self.var_list) self._init_func()
def _run(self, fetches, feed_dict): if self._closed: raise RuntimeError('Attempted to use a closed Session.') # unpack opts and tensors opts = []; tensors = [] for target in fetches: if isinstance(target, Optimizer): opts.append(target) elif isinstance(target, VariablesInitializer): tensors.extend(target.var_list) elif isinstance(target, Tensor): tensors.append(target) # find minimum solving targets targets = set() for t in tensors: targets.add(t) for opt in opts: for t in opt.objs: targets.add(t) targets = list(targets) # if existing a transaction before? global _TRANSACTIONS t_key = tuple(fetches + feed_dict.keys()) \ if feed_dict is not None else tuple(fetches) transaction = None if not t_key in _TRANSACTIONS else _TRANSACTIONS[t_key] # cond.1: run by feeding if feed_dict is not None: # checking for key, value in feed_dict.items(): if not isinstance(key, Tensor): raise TypeError('The key of feed_dict key should be a Tensor.') if key.shape is not None: if len(key.shape) != len(value.shape): raise RuntimeError('The Tensor({}) was limited to {} dimensions, \ while feed a value with {} dimensions.'. format(key.name, len(key.shape), len(value.shape))) for i in xrange(len(key.shape)): if key.shape[i] is None: continue if key.shape[i] != value.shape[i]: raise RuntimeError('The shape of Tensor({}) was limited as ('.format(key.name) + ','.join([str(dim) for dim in key.shape]) + '), ' + 'while feed a value with (' + ','.join([str(dim) for dim in value.shape]) + ').') # create a new transaction if transaction is None: functions = [] functions.append(theano.function(inputs=feed_dict.keys(), outputs=targets)) for opt in opts: functions.append(theano.function(updater=opt.updater)) _TRANSACTIONS[t_key] = transaction = Transaction(functions) transaction.run(feed_dict.values()) # cond.2: run without feeding else: # create a new transaction if transaction is None: functions = [] functions.append(theano.function(outputs=targets)) for opt in opts: functions.append(theano.function(updater=opt.updater)) _TRANSACTIONS[t_key] = transaction = Transaction(functions) transaction.run(None) # fetch after running returns = [] for target in fetches: if isinstance(target, Optimizer): returns.append(None) elif isinstance(target, VariablesInitializer): returns.append(None) else: np_target = target.get_value() # unpack the scalar if necessary if np_target.size == 1: returns.append(np_target.flatten()[0]) else: returns.append(np_target) # unpack the returns if necessary if len(returns) == 1: return returns[0] else: return returns
def predict_p(self, x): if not hasattr(self, '_predict_p'): self._predict_p = theano.function(inputs=self.x, outputs=self.softmax_p) return self._predict_p(x)
def predict_v(self, x): if not hasattr(self, '_predict_p'): self._predict_v = theano.function(inputs=self.x, outputs=self.logits_v) return self._predict_v(x)
""" x1 = ws.FetchTensor(inputs[0]) x2 = ws.FetchTensor(inputs[1]) dy = ws.FetchTensor(inputs[-1]) dx1 = dy * x2 dx2 = dy * x1 ws.FeedTensor(outputs[0], dx1) ws.FeedTensor(outputs[1], dx2) if __name__ == '__main__': # def x1 = Tensor('x1').Variable() x2 = Tensor('x2').Variable() y = ops.Template([x1, x2], module=__name__, op='VecMultOp', nout=1) dx1 = T.grad(y, x1) dx2 = T.grad(y, x2) foo = theano.function(outputs=y) # feed ws.FeedTensor(x1, np.ones((5, 3), dtype=np.float32)) ws.FeedTensor(x2, np.ones((5, 3), dtype=np.float32) * 5.0) # run foo() # fetch print('y \n-------------- \n', y.get_value(), '\n') print('dx1 \n-------------- \n', dx1.get_value(), '\n') print('dx2 \n-------------- \n', dx2.get_value(), '\n')