This function should build the model which takes the input variables
        and return cost at the end
2974
def build_graph(self, image, label):
        """"""

        # In tensorflow, inputs to convolution function are assumed to be
        # NHWC. Add a single channel here.
        image = tf.expand_dims(image, 3)

        image = image * 2 - 1   # center the pixels values at zero
        # The context manager `argscope` sets the default option for all the layers under
        # this context. Here we use 32 channel convolution with shape 3x3
        with argscope(Conv2D, kernel_size=3, activation=tf.nn.relu, filters=32):
            logits = (LinearWrap(image)
                      .Conv2D('conv0')
                      .MaxPooling('pool0', 2)
                      .Conv2D('conv1')
                      .Conv2D('conv2')
                      .MaxPooling('pool1', 2)
                      .Conv2D('conv3')
                      .FullyConnected('fc0', 512, activation=tf.nn.relu)
                      .Dropout('dropout', rate=0.5)
                      .FullyConnected('fc1', 10, activation=tf.identity)())

        # a vector of length B with loss of each sample
        cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label)
        cost = tf.reduce_mean(cost, name='cross_entropy_loss')  # the average cross-entropy loss

        correct = tf.cast(tf.nn.in_top_k(predictions=logits, targets=label, k=1), tf.float32, name='correct')
        accuracy = tf.reduce_mean(correct, name='accuracy')

        # This will monitor training error & accuracy (in a moving average fashion). The value will be automatically
        # 1. written to tensosrboard
        # 2. written to stat.json
        # 3. printed after each epoch
        train_error = tf.reduce_mean(1 - correct, name='train_error')
        summary.add_moving_summary(train_error, accuracy)

        # Use a regex to find parameters to apply weight decay.
        # Here we apply a weight decay on all W (weight matrix) of all fc layers
        # If you don't like regex, you can certainly define the cost in any other methods.
        wd_cost = tf.multiply(1e-5,
                              regularize_cost('fc.*/W', tf.nn.l2_loss),
                              name='regularize_loss')
        total_cost = tf.add_n([wd_cost, cost], name='total_cost')
        summary.add_moving_summary(cost, wd_cost, total_cost)

        # monitor histogram of all weight (of conv and fc layers) in tensorboard
        summary.add_param_summary(('.*/W', ['histogram', 'rms']))
        # the function should return the total cost to be optimized
        return total_cost
1023880
def build_train_op_from_cost(self, cost):
    """"""
    self.lrn_rate = tf.constant(self.hps.lrn_rate, tf.float32,
                                name='learning_rate')
    self.momentum = tf.constant(self.hps.momentum, tf.float32,
                                name='momentum')

    trainable_variables = tf.trainable_variables()
    grads = tf.gradients(cost, trainable_variables)
    devs = {v.device for v in trainable_variables}
    assert len(devs) == 1, ('There should be no trainable variables'
                            ' on any device other than the last GPU.')

    optimizer = tf.train.MomentumOptimizer(self.lrn_rate, self.momentum)

    gv_pairs = zip(grads, trainable_variables)
    gv_pairs = [gv for gv in gv_pairs if gv[0] is not None]
    devs = {gv[1].device for gv in gv_pairs}
    assert len(devs) == 1, ('There should be no gradients wrt'
                            ' vars on other GPUs.')

    apply_op = optimizer.apply_gradients(
        gv_pairs,
        global_step=self.global_step, name='train_step')

    train_ops = [apply_op]
    train_op = tf.group(*train_ops)
    return train_op
1095263
def build_model(self, n_features, n_classes):
        
        self._create_placeholders(n_features, n_classes)
        self._create_variables(n_features)

        next_train = self._create_encoding_layers()
        self.mod_y, _, _ = Layers.linear(next_train, n_classes)
        self.layer_nodes.append(self.mod_y)

        self.cost = self.loss.compile(self.mod_y, self.input_labels)
        self.train_step = self.trainer.compile(self.cost)
        self.accuracy = Evaluation.accuracy(self.mod_y, self.input_labels)
1070268
def _get_cost_function(self):
        
        self.weights = tf.placeholder(
            tf.float32, shape=[self.n_words, self.n_words])
        self.log_coincidence = tf.placeholder(
            tf.float32, shape=[self.n_words, self.n_words])
        self.diffs = tf.subtract(self.model, self.log_coincidence)
        cost = tf.reduce_sum(
            0.5 * tf.multiply(self.weights, tf.square(self.diffs)))
        if self.mittens > 0:
            self.mittens = tf.constant(self.mittens, tf.float32)
            cost += self.mittens * tf.reduce_sum(
                tf.multiply(
                    self.has_embedding,
                    self._tf_squared_euclidean(
                        tf.add(self.W, self.C),
                        self.original_embedding)))
        tf.summary.scalar("cost", cost)
        return cost
778699
def custom_build(inputs, is_training, keep_prob):
  """"""
  outputs = snt.Conv2D(output_channels=32, kernel_shape=4, stride=2)(inputs)
  outputs = snt.BatchNorm()(outputs, is_training=is_training)
  outputs = tf.nn.relu(outputs)
  outputs = snt.Conv2D(output_channels=64, kernel_shape=4, stride=2)(outputs)
  outputs = snt.BatchNorm()(outputs, is_training=is_training)
  outputs = tf.nn.relu(outputs)
  outputs = snt.BatchFlatten()(outputs)
  outputs = tf.nn.dropout(outputs, keep_prob=keep_prob)
  outputs = snt.Linear(output_size=10)(outputs)
  return outputs
1066614
def create(input_length, hidden_layers, activation='tanh', normalization=None):
    
    def instantiate(**_):
        return MLP(
            input_length=input_length,
            hidden_layers=hidden_layers,
            activation=activation,
            normalization=normalization
        )

    return ModelFactory.generic(instantiate)
123930
def make_var(self, op_name, name, shape):
        ''''''
        if op_name in self.weights and name in self.weights[op_name]:
            if self.verbose:
                print("Using: {} {}".format(op_name, name))
            initializer = tf.constant(self.weights[op_name][name], shape=shape)
            return tf.get_variable(name, initializer=initializer, trainable=self.trainable)
        return tf.get_variable(name, shape, trainable=self.trainable)
778329
def total_cost(self, p=None, p_cost=None, pcost_model=None):
        
        p = self.p if p is None else p
        p_cost = self.p_cost if p_cost is None else p_cost
        pcost_model = self.pcost_model if pcost_model is None else pcost_model

        p = 0.0 if not self.online else p

        if pcost_model == PW_LINEAR:
            n_segments = len(p_cost) - 1
            # Iterate over the piece-wise linear segments.
            for i in range(n_segments):
                x1, y1 = p_cost[i]
                x2, y2 = p_cost[i + 1]
                m = (y2 - y1) / (x2 - x1)
                c = y1 - m * x1
                if x1 <= p <= x2:
                    result = m*p + c
                    break
            else:
#                print "TOTC:", self.name, p, self.p_max, p_cost

#                raise ValueError, "Value [%f] outwith pwl cost curve." % p

                # Use the last segment for values outwith the cost curve.
                logger.error("Value [%f] outside pwl cost curve [%s]." %
                             (p, p_cost[-1][0]))
                result = m*p + c
        elif pcost_model == POLYNOMIAL:
#            result = p_cost[-1]
#            for i in range(1, len(p_cost)):
#                result += p_cost[-(i + 1)] * p**i
            result = polyval(p_cost, p)
        else:
            raise ValueError

        if self.is_load:
            return -result
        else:
            return result
114939
function(left, right, cost, identifier, allVars, joinVars) {

    this.left =  left;
    this.right = right;
    this.cost = cost;
    this.i = identifier;
    this.vars = allVars;
    this.join = joinVars;

}
686348
def __make_var(self, name: str, shape: list):
        
        return tf.get_variable(name, shape, trainable=self.__network.is_trainable())
743955
def _costfcn(self, x):
        
        f = self._f(x)
        df = self._df(x)
        d2f = self._d2f(x)

        return f, df, d2f
549109
def _cost_method(self, *args, **kwargs):
        

        cost_val = self.thresh * nuclear_norm(cube2matrix(args[0]))

        if 'verbose' in kwargs and kwargs['verbose']:
            print(' - NUCLEAR NORM (X):', cost_val)

        return cost_val
1183342
def make_nn_funs(layer_sizes, L2_reg, noise_variance, nonlinearity=np.tanh):
    """"""
    shapes = list(zip(layer_sizes[:-1], layer_sizes[1:]))
    num_weights = sum((m+1)*n for m, n in shapes)

    def unpack_layers(weights):
        num_weight_sets = len(weights)
        for m, n in shapes:
            yield weights[:, :m*n]     .reshape((num_weight_sets, m, n)),\
                  weights[:, m*n:m*n+n].reshape((num_weight_sets, 1, n))
            weights = weights[:, (m+1)*n:]

    def predictions(weights, inputs):
        """weights is shape (num_weight_samples x num_weights)
           inputs  is shape (num_datapoints x D)"""
        inputs = np.expand_dims(inputs, 0)
        for W, b in unpack_layers(weights):
            outputs = np.einsum('mnd,mdo->mno', inputs, W) + b
            inputs = nonlinearity(outputs)
        return outputs

    def logprob(weights, inputs, targets):
        log_prior = -L2_reg * np.sum(weights**2, axis=1)
        preds = predictions(weights, inputs)
        log_lik = -np.sum((preds - targets)**2, axis=1)[:, 0] / noise_variance
        return log_prior + log_lik

    return num_weights, predictions, logprob
985715
def _f(self, x, user_data=None):
        
        p_gen = x[self._Pg.i1:self._Pg.iN + 1] # Active generation in p.u.
        q_gen = x[self._Qg.i1:self._Qg.iN + 1] # Reactive generation in p.u.

        # Polynomial cost of P and Q.
        xx = r_[p_gen, q_gen] * self._base_mva
        if len(self._ipol) > 0:
            f = sum([g.total_cost(xx[i]) for i,g in enumerate(self._gn)])
        else:
            f = 0

        # Piecewise linear cost of P and Q.
        if self._ny:
            y = self.om.get_var("y")
            self._ccost = csr_matrix((ones(self._ny),
                (range(y.i1, y.iN + 1), zeros(self._ny))),
                shape=(self._nxyz, 1)).T
            f = f + self._ccost * x
        else:
            self._ccost = zeros((1, self._nxyz))
        # TODO: Generalised cost term.

        return f
315222
function() {
      var input_depth = this.data[0].w.length;
      var num_classes = this.unique_labels.length;

      // sample network topology and hyperparameters
      var layer_defs = [];
      layer_defs.push({type:'input', out_sx:1, out_sy:1, out_depth: input_depth});
      var nl = weightedSample([0,1,2,3], [0.2, 0.3, 0.3, 0.2]); // prefer nets with 1,2 hidden layers
      for(var q=0;q<nl;q++) {
        var ni = randi(this.neurons_min, this.neurons_max);
        var act = ['tanh','maxout','relu'][randi(0,3)];
        if(randf(0,1)<0.5) {
          var dp = Math.random();
          layer_defs.push({type:'fc', num_neurons: ni, activation: act, drop_prob: dp});
        } else {
          layer_defs.push({type:'fc', num_neurons: ni, activation: act});
        }
      }
      layer_defs.push({type:'softmax', num_classes: num_classes});
      var net = new Net();
      net.makeLayers(layer_defs);

      // sample training hyperparameters
      var bs = randi(this.batch_size_min, this.batch_size_max); // batch size
      var l2 = Math.pow(10, randf(this.l2_decay_min, this.l2_decay_max)); // l2 weight decay
      var lr = Math.pow(10, randf(this.learning_rate_min, this.learning_rate_max)); // learning rate
      var mom = randf(this.momentum_min, this.momentum_max); // momentum. Lets just use 0.9, works okay usually ;p
      var tp = randf(0,1); // trainer type
      var trainer_def;
      if(tp<0.33) {
        trainer_def = {method:'adadelta', batch_size:bs, l2_decay:l2};
      } else if(tp<0.66) {
        trainer_def = {method:'adagrad', learning_rate: lr, batch_size:bs, l2_decay:l2};
      } else {
        trainer_def = {method:'sgd', learning_rate: lr, momentum: mom, batch_size:bs, l2_decay:l2};
      }
      
      var trainer = new Trainer(net, trainer_def);

      var cand = {};
      cand.acc = [];
      cand.accv = 0; // this will maintained as sum(acc) for convenience
      cand.layer_defs = layer_defs;
      cand.trainer_def = trainer_def;
      cand.net = net;
      cand.trainer = trainer;
      return cand;
    }
910583
def _quadratic_costs(self, generators, ipol, nxyz, base_mva):
        
        npol = len(ipol)
        rnpol = range(npol)
        gpol = [g for g in generators if g.pcost_model == POLYNOMIAL]

        if [g for g in gpol if len(g.p_cost) > 3]:
            logger.error("Order of polynomial cost greater than quadratic.")

        iqdr = [i for i, g in enumerate(generators)
                if g.pcost_model == POLYNOMIAL and len(g.p_cost) == 3]
        ilin = [i for i, g in enumerate(generators)
                if g.pcost_model == POLYNOMIAL and len(g.p_cost) == 2]

        polycf = zeros((npol, 3))
        if npol > 0:
            if len(iqdr) > 0:
                polycf[iqdr, :] = array([list(g.p_cost)
                                         for g in generators])#[iqdr, :].T
            if len(ilin) > 0:
                polycf[ilin, 1:] = array([list(g.p_cost[:2])
                                          for g in generators])#[ilin, :].T
            # Convert to per-unit.
            polycf = polycf * array([base_mva**2, base_mva, 1])
            Pg = self.om.get_var("Pg")
            Npol = csr_matrix((ones(npol), (rnpol, Pg.i1 + array(ipol))),
                              (npol, nxyz))
            Hpol = csr_matrix((2 * polycf[:, 0], (rnpol, rnpol)), (npol, npol))
            Cpol = polycf[:, 1]
            fparm_pol = (ones(npol) * array([[1], [0], [0], [1]])).T
        else:
            Npol = Hpol = None
            Cpol = array([])
            fparm_pol = zeros((0, 4))

        return Npol, Hpol, Cpol, fparm_pol, polycf, npol
392031
def create(input_width, input_height, input_channels=1, output_dim=512):
    
    def instantiate(**_):
        return NatureCnn(
            input_width=input_width, input_height=input_height, input_channels=input_channels,
            output_dim=output_dim
        )

    return ModelFactory.generic(instantiate)