How to use the numjs.divide function in numjs

To help you get started, we’ve selected a few numjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github grimmer0125 / alphago-zero-tictactoe-js / src / MCTS.js View on Github external
if (this.Ps.hasOwnProperty(s) == false) {
      // # leaf node
      // NOTE: Python ver.: v is ndarray type: [0.x]. qsa, too.
      // JavaScript: v is just a number value.
      const resp = this.nnet.predict(canonicalBoard);
      this.Ps[s] = resp.Ps;
      const v = resp.v;// .get(0);

      const valids = this.game.getValidMoves(canonicalBoard, 1);
      // NOTE: : Array multiplication is not matrix multiplication:
      // Python: self.Ps[s] = self.Ps[s]*valids
      this.Ps[s] = nj.multiply(this.Ps[s], valids); // # masking invalid moves
      const sum_Ps_s = nj.sum(this.Ps[s]);
      if (sum_Ps_s > 0) {
        this.Ps[s] = nj.divide(this.Ps[s], sum_Ps_s); // renormalize
      } else {
        // # if all valid moves were masked make all valid moves equally probable
        //
        // # NB! All valid moves may be masked if either your NNet architecture is insufficient or you've get overfitting or something else.
        // # If you have got dozens or hundreds of these messages you should pay attention to your NNet and/or training process.
        console.log('All valid moves were masked, do workaround.');
        this.Ps[s] = nj.add(this.Ps[s], valids);
        this.Ps[s] = nj.divide(this.Ps[s], nj.sum(this.Ps[s]));
      }

      this.Vs[s] = valids;
      this.Ns[s] = 0;
      return v;
    }

    const valids = this.Vs[s];
github MindExMachina / smartgeometry / services / sketch-rnn / lib / sketch_rnn.js View on Github external
function adjust_temp(z_old, temp) {
        var z = nj.array(z_old);
        var i;
        var x;
        //console.log("before="+z_old.get(0));
        for (i = z.shape[0] - 1; i >= 0; i--) {
            x = z.get(i);
            x = Math.log(x) / temp;
            z.set(i, x);
        }
        x = z.max();
        z = nj.subtract(z, x);
        z = nj.exp(z);
        x = z.sum();
        z = nj.divide(z, x);
        //console.log("after="+z.get(0));
        return z;
    };