backward step for HMM, assuming both the hidden state and noise
            have 1-step dependence on the previous value.
518588
def _backward_step(self, deltaY, deltaY_sigma2inv_rho_weightT,
                       sigma2_e, weight, mu, mu_Gamma_inv, Gamma_inv,
                       Lambda_0, Lambda_1, H):
        
        n_T = len(Gamma_inv)
        # All the terms with hat before are parameters of posterior
        # distributions of X conditioned on data from all time points,
        # whereas the ones without hat calculated by _forward_step
        # are mean and covariance of posterior of X conditioned on
        # data up to the time point.
        Gamma_inv_hat = [None] * n_T
        mu_Gamma_inv_hat = [None] * n_T
        mu_hat = [None] * n_T

        mu_hat[-1] = mu[-1].copy()
        mu_Gamma_inv_hat[-1] = mu_Gamma_inv[-1].copy()
        Gamma_inv_hat[-1] = Gamma_inv[-1].copy()

        for t in np.arange(n_T - 2, -1, -1):
            tmp = np.linalg.solve(Gamma_inv_hat[t + 1] - Gamma_inv[t + 1]
                                  + Lambda_1, H)
            Gamma_inv_hat[t] = Gamma_inv[t] + Lambda_0 - np.dot(H.T, tmp)
            mu_Gamma_inv_hat[t] = mu_Gamma_inv[t] \
                - deltaY_sigma2inv_rho_weightT[t, :] + np.dot(
                    mu_Gamma_inv_hat[t + 1] - mu_Gamma_inv[t + 1]
                    + np.dot(deltaY[t, :] / sigma2_e, weight.T), tmp)
            mu_hat[t] = np.linalg.solve(Gamma_inv_hat[t],
                                        mu_Gamma_inv_hat[t])
        return mu_hat, mu_Gamma_inv_hat, Gamma_inv_hat
1076489
public void backward() {
        VarTensor[] varBeliefsAdj = bAdj.varBeliefs;
        VarTensor[] facBeliefsAdj = bAdj.facBeliefs;
        
        // Initialize the adjoints.
    
        // We are given the adjoints of the normalized beleifs. Compute
        // the adjoints of the unnormalized beliefs and store them in the original
        // adjoint arrays.
        for (int v=0; v<varBeliefsAdj.length; v++) {
            unnormalizeAdjInPlace(varBeliefs[v], varBeliefsAdj[v], varBeliefsUnSum[v]);
        }
        for (int a=0; a<facBeliefsAdj.length; a++) {
            if (facBeliefs[a] != null) {
                unnormalizeAdjInPlace(facBeliefs[a], facBeliefsAdj[a], facBeliefsUnSum[a]);
            }
        }
        
        // Initialize the message and potential adjoints by running the variable / factor belief computation in reverse.
        backwardVarFacBeliefs(varBeliefsAdj, facBeliefsAdj);
        
        // Process each tape entry in reverse order.
        for (int t = tape.size() - 1; t >= 0; t--) {
            // Dequeue from tape.
            TapeEntry te = tape.get(t);
            List<Integer> edges = CachingBpSchedule.toEdgeList(fg, te.item);
            List<?> elems = CachingBpSchedule.toFactorEdgeList(te.item);
            
            for (int j = edges.size() - 1; j >= 0; j--) {
                backwardSendMessage(edges.get(j), te.msgs.get(j));
            }
            for (int j = edges.size() - 1; j >= 0; j--) {
                backwardNormalize(edges.get(j), te.msgSums.get(j));
            }
            for (int j = elems.size() - 1; j >= 0; j--) {
                Object elem = elems.get(j);
                if (elem instanceof Integer) {
                    backwardCreateMessage((Integer) elem);
                } else if (elem instanceof AutodiffGlobalFactor) {
                    backwardGlobalFactorToVar((AutodiffGlobalFactor) elem, te);
                } else {
                    throw new RuntimeException("Unsupported type in schedule: " + elem.getClass());
                }
            }
        }
    }
193206
def backward(self, y):
        
        if self.squeeze:
            y = y[None, :, :]
        ind = np.tril_indices(self.N)
        return np.vstack([y_i[ind] for y_i in y])
1184594
def lstm(num_hidden, indata, prev_state, param, seqidx, layeridx, dropout=0., num_hidden_proj=0, is_batchnorm=False,
         gamma=None, beta=None, name=None):
    """"""
    # dropout input
    if dropout > 0.:
        indata = mx.sym.Dropout(data=indata, p=dropout)

    i2h = mx.sym.FullyConnected(data=indata,
                                weight=param.i2h_weight,
                                bias=param.i2h_bias,
                                num_hidden=num_hidden * 4,
                                name="t%d_l%d_i2h" % (seqidx, layeridx))
    if is_batchnorm:
        if name is not None:
            i2h = batchnorm(net=i2h, gamma=gamma, beta=beta, name="%s_batchnorm" % name)
        else:
            i2h = batchnorm(net=i2h, gamma=gamma, beta=beta)

    h2h = mx.sym.FullyConnected(data=prev_state.h,
                                weight=param.h2h_weight,
                                # bias=param.h2h_bias,
                                no_bias=True,
                                num_hidden=num_hidden * 4,
                                name="t%d_l%d_h2h" % (seqidx, layeridx))

    gates = i2h + h2h
    slice_gates = mx.sym.SliceChannel(gates, num_outputs=4,
                                      name="t%d_l%d_slice" % (seqidx, layeridx))

    Wcidc = mx.sym.broadcast_mul(param.c2i_bias, prev_state.c) + slice_gates[0]
    in_gate = mx.sym.Activation(Wcidc, act_type="sigmoid")

    in_transform = mx.sym.Activation(slice_gates[1], act_type="tanh")

    Wcfdc = mx.sym.broadcast_mul(param.c2f_bias, prev_state.c) + slice_gates[2]
    forget_gate = mx.sym.Activation(Wcfdc, act_type="sigmoid")

    next_c = (forget_gate * prev_state.c) + (in_gate * in_transform)

    Wcoct = mx.sym.broadcast_mul(param.c2o_bias, next_c) + slice_gates[3]
    out_gate = mx.sym.Activation(Wcoct, act_type="sigmoid")

    next_h = out_gate * mx.sym.Activation(next_c, act_type="tanh")

    if num_hidden_proj > 0:
        proj_next_h = mx.sym.FullyConnected(data=next_h,
                                            weight=param.ph2h_weight,
                                            no_bias=True,
                                            num_hidden=num_hidden_proj,
                                            name="t%d_l%d_ph2h" % (seqidx, layeridx))

        return LSTMState(c=next_c, h=proj_next_h)
    else:
        return LSTMState(c=next_c, h=next_h)
476324
def main():
    """"""
    model = load_model('../data/hmmdefs')
    hmm = model.hmms['r-We']
    for state_name in hmm.state_names:
        print(state_name)
        state = model.states[state_name]
        print(state.means_)
    print(model)
    model2 = load_model('../data/prior.hmm1mixSI.rate32')
    print(model2)
385370
def _backward_sampling_ON2(self, M, idx):
        
        for m in range(M):
            for t in reversed(range(self.T - 1)):
                lwm = (self.wgt[t].lw + self.model.logpt(t + 1, self.X[t],
                                                     self.X[t + 1][idx[t + 1, m]]))
                idx[t, m] = rs.multinomial_once(rs.exp_and_normalise(lwm))
50658
public void rnnSetPreviousStates(Map<String, Map<String, INDArray>> previousStates) {
        for (Map.Entry<String, Map<String, INDArray>> entry : previousStates.entrySet()) {
            rnnSetPreviousState(entry.getKey(), entry.getValue());
        }
    }
406614
def denoise_v1_m15():
  """"""
  hparams = xmoe2_v1()
  # no local attention
  # TODO(noam): non-masked version of local-attention
  hparams.decoder_layers = [
      "att" if l == "local_att" else l for l in hparams.decoder_layers]
  hparams.decoder_type = "denoising"
  hparams.noising_spec_train = {"type": "mask", "prob": 0.15}
  return hparams
336146
def on_backward_begin(self, last_loss:Rank0Tensor, **kwargs:Any) -> Rank0Tensor:
        ""
        #To avoid gradient underflow, we scale the gradients
        ret_loss = last_loss * self.loss_scale
        return {'last_loss': ret_loss}
838518
private ForwardStepResult<S, O, D> forwardStep(O observation, Collection<S> prevCandidates,
            Collection<S> curCandidates, Map<S, Double> message,
            Map<S, Double> emissionLogProbabilities,
            Map<Transition<S>, Double> transitionLogProbabilities,
            Map<Transition<S>,D> transitionDescriptors) {
        final ForwardStepResult<S, O, D> result = new ForwardStepResult<>(curCandidates.size());
        assert !prevCandidates.isEmpty();

        for (S curState : curCandidates) {
            double maxLogProbability = Double.NEGATIVE_INFINITY;
            S maxPrevState = null;
            for (S prevState : prevCandidates) {
                final double logProbability = message.get(prevState) + transitionLogProbability(
                        prevState, curState, transitionLogProbabilities);
                if (logProbability > maxLogProbability) {
                    maxLogProbability = logProbability;
                    maxPrevState = prevState;
                }
            }
            // Throws NullPointerException if curState is not stored in the map.
            result.newMessage.put(curState, maxLogProbability
                    + emissionLogProbabilities.get(curState));

            // Note that maxPrevState == null if there is no transition with non-zero probability.
            // In this case curState has zero probability and will not be part of the most likely
            // sequence, so we don't need an ExtendedState.
            if (maxPrevState != null) {
                final Transition<S> transition = new Transition<>(maxPrevState, curState);
                final ExtendedState<S, O, D> extendedState = new ExtendedState<>(curState,
                        lastExtendedStates.get(maxPrevState), observation,
                        transitionDescriptors.get(transition));
                result.newExtendedStates.put(curState, extendedState);
            }
        }
        return result;
    }
702664
def fit(self):
        
        logger().info("=================================================================")
        logger().info("Running Baum-Welch:")
        logger().info("  input observations: "+str(self.nobservations)+" of lengths "+str(self.observation_lengths))
        logger().info("  initial HMM guess:"+str(self._hmm))

        initial_time = time.time()

        it = 0
        self._likelihoods = np.zeros(self.maxit)
        loglik = 0.0
        # flag if connectivity has changed (e.g. state lost) - in that case the likelihood
        # is discontinuous and can't be used as a convergence criterion in that iteration.
        tmatrix_nonzeros = self.hmm.transition_matrix.nonzero()
        converged = False

        while not converged and it < self.maxit:
            # self._fbtimings = np.zeros(5)
            t1 = time.time()
            loglik = 0.0
            for k in range(self._nobs):
                loglik += self._forward_backward(k)
                assert np.isfinite(loglik), it
            t2 = time.time()

            # convergence check
            if it > 0:
                dL = loglik - self._likelihoods[it-1]
                # print 'dL ', dL, 'iter_P ', maxiter_P
                if dL < self._accuracy:
                    # print "CONVERGED! Likelihood change = ",(loglik - self.likelihoods[it-1])
                    converged = True

            # update model
            self._update_model(self._gammas, self._Cs, maxiter=self._maxit_P)
            t3 = time.time()

            # connectivity change check
            tmatrix_nonzeros_new = self.hmm.transition_matrix.nonzero()
            if not np.array_equal(tmatrix_nonzeros, tmatrix_nonzeros_new):
                converged = False  # unset converged
                tmatrix_nonzeros = tmatrix_nonzeros_new

            # print 't_fb: ', str(1000.0*(t2-t1)), 't_up: ', str(1000.0*(t3-t2)), 'L = ', loglik, 'dL = ', (loglik - self._likelihoods[it-1])
            # print '  fb timings (ms): pobs', (1000.0*self._fbtimings).astype(int)

            logger().info(str(it) + " ll = " + str(loglik))
            # print self.model.output_model
            # print "---------------------"

            # end of iteration
            self._likelihoods[it] = loglik
            it += 1

        # final update with high precision
        # self._update_model(self._gammas, self._Cs, maxiter=10000000)

        # truncate likelihood history
        self._likelihoods = self._likelihoods[:it]
        # set final likelihood
        self._hmm.likelihood = loglik
        # set final count matrix
        self.count_matrix = self._transition_counts(self._Cs)
        self.initial_count = self._init_counts(self._gammas)

        final_time = time.time()
        elapsed_time = final_time - initial_time

        logger().info("maximum likelihood HMM:"+str(self._hmm))
        logger().info("Elapsed time for Baum-Welch solution: %.3f s" % elapsed_time)
        logger().info("Computing Viterbi path:")

        initial_time = time.time()

        # Compute hidden state trajectories using the Viterbi algorithm.
        self._hmm.hidden_state_trajectories = self.compute_viterbi_paths()

        final_time = time.time()
        elapsed_time = final_time - initial_time

        logger().info("Elapsed time for Viterbi path computation: %.3f s" % elapsed_time)
        logger().info("=================================================================")

        return self._hmm
825973
@Override
    public void backward() {
        Tensor x = modInX.getOutput();
        Tensor w = modInW.getOutput();
        {
            Tensor tmp = new Tensor(yAdj); // copy
            tmp.elemDivide(w);
            correctForZeros(tmp);
            modInX.getOutputAdj().elemAdd(tmp);
        }
        {
            Tensor tmp = new Tensor(w); // copy
            tmp.fill(s.one());
            tmp.elemDivide(w);
            tmp.elemDivide(w);
            tmp.multiply(s.fromReal(-1));
            tmp.elemMultiply(yAdj);
            tmp.elemMultiply(x); 
            correctForZeros(tmp);
            modInW.getOutputAdj().elemAdd(tmp);
        }
    }
1298069
private void backward(final CLClause c, final int ignore) {
        int minlit = 0;
        int minoccs = Integer.MAX_VALUE;
        int litoccs;
        this.stats.steps++;
        for (int i = 0; i < c.lits().size(); i++) {
            final int lit = c.lits().get(i);
            if (lit == ignore) { continue; }
            if (val(lit) < 0) { continue; }
            litoccs = occs(lit).count();
            if (minlit != 0 && minoccs >= litoccs) { continue; }
            minlit = lit;
            minoccs = litoccs;
        }
        if (minoccs >= this.config.bwocclim) { return; }
        assert minlit != 0;
        for (int i = 0; i < c.lits().size(); i++) { mark(c.lits().get(i)); }
        final CLOccs os = occs(minlit);
        for (final CLClause d : os) {
            if (d == c) { continue; }
            int lit;
            int count = this.seen.size();
            int negated = 0;
            this.stats.steps++;
            for (int p = 0; count != 0 && p < d.lits().size(); p++) {
                lit = d.lits().get(p);
                final int m = marked(lit);
                if (m == 0) { continue; }
                assert count > 0;
                count--;
                if (m > 0) { continue; }
                assert m < 0;
                if (negated != 0) {
                    count = Integer.MAX_VALUE;
                    break;
                }
                negated = lit;
            }
            if (count != 0) { continue; }
            if (negated != 0) {
                this.tostrengthen.push(new Pair<>(d, negated));
                this.stats.backwardStrengthened++;
            } else {
                this.stats.backwardSubsumed++;
                dumpClause(d);
            }
        }
        unmark();
    }
1212095
def expected_hmm_logprob(pi_0, trans_matrix, stats):
    
    E_z, sum_E_ztztp1T, _ = stats
    T, K = E_z.shape
    assert sum_E_ztztp1T.shape == (K, K)

    out = 0
    out += np.dot(E_z[0], np.log(pi_0))
    out += np.sum(sum_E_ztztp1T * np.log(trans_matrix))
    return out
316204
@Override
    public void backward() {
        Tensor x = modInX.getOutput();
        Tensor tmp = new Tensor(yAdj); // copy
        tmp.elemDivide(x);
        modInX.getOutputAdj().elemAdd(tmp);
    }
1048946
def decoherence_noise_with_asymmetric_ro(gates: Sequence[Gate], p00=0.975, p11=0.911):
    
    noise_model = _decoherence_noise_model(gates)
    aprobs = np.array([[p00, 1 - p00],
                       [1 - p11, p11]])
    aprobs = {q: aprobs for q in noise_model.assignment_probs.keys()}
    return NoiseModel(noise_model.gates, aprobs)
155819
def forward(data, model, mlm_loss, nsp_loss, vocab_size, dtype):
    """"""
    (input_id, masked_id, masked_position, masked_weight, \
     next_sentence_label, segment_id, valid_length) = data
    num_masks = masked_weight.sum() + 1e-8
    valid_length = valid_length.reshape(-1)
    masked_id = masked_id.reshape(-1)
    valid_length_typed = valid_length.astype(dtype, copy=False)
    _, _, classified, decoded = model(input_id, segment_id, valid_length_typed,
                                      masked_position)
    decoded = decoded.reshape((-1, vocab_size))
    ls1 = mlm_loss(decoded.astype('float32', copy=False),
                   masked_id, masked_weight.reshape((-1, 1)))
    ls2 = nsp_loss(classified.astype('float32', copy=False), next_sentence_label)
    ls1 = ls1.sum() / num_masks
    ls2 = ls2.mean()
    ls = ls1 + ls2
    return ls, next_sentence_label, classified, masked_id, decoded, \
           masked_weight, ls1, ls2, valid_length.astype('float32', copy=False)