How to use the numjs.multiply 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
// # terminal node
      return -this.Es[s];
    }

    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;
github MindExMachina / smartgeometry / services / sketch-rnn / lib / sketch_rnn.js View on Github external
function encode_from_mu_sigma(mu, sigma, temperature) {
        // returns random z given mu, sigma, temperature (2nd part of encode function)
        // sometimes we want to create a large list of random z's given the same encoded image
        // this is an optimization.
        var temp = 1.0;
        if (typeof(temperature) === "number") {
            temp = temperature;
        };
        var eps = nj.multiply(nj.array(random_normal_vector(), 'float32'), temp);
        var z = nj.add(mu, nj.multiply(eps, sigma));
        return z.tolist();
    };
github MindExMachina / smartgeometry / services / sketch-rnn / lib / sketch_rnn.js View on Github external
s = [sequence[i][0] / scale_factor, sequence[i][1] / scale_factor, sequence[i][2], sequence[i][3], sequence[i][4]];
            forward_sequence.push(s);
        }
        for (i = N - 1; i >= 0; i--) {
            s = [forward_sequence[i][0], forward_sequence[i][1], forward_sequence[i][2], forward_sequence[i][3], forward_sequence[i][4]];
            reverse_sequence.push(s);
        }
        var output_fw = enc_fw_lstm.encode(forward_sequence);
        var output_bw = enc_bw_lstm.encode(reverse_sequence);
        var output = nj.concatenate([output_fw, output_bw]);
        var mu = nj.add(nj.dot(output, enc_mu_w), enc_mu_b);
        // optimization:
        if (temp > 0) {
            var presig = nj.add(nj.dot(output, enc_sigma_w), enc_sigma_b);
            var sigma = nj.sqrt(nj.exp(presig));
            var eps = nj.multiply(nj.array(random_normal_vector(), 'float32'), temp);
            var z = nj.add(mu, nj.multiply(eps, sigma));
        } else {
            var z = mu;
        }
        return z.tolist();
    };
github MindExMachina / smartgeometry / services / sketch-rnn / lib / sketch_rnn.js View on Github external
forward_sequence.push(s);
        }
        for (i = N - 1; i >= 0; i--) {
            s = [forward_sequence[i][0], forward_sequence[i][1], forward_sequence[i][2], forward_sequence[i][3], forward_sequence[i][4]];
            reverse_sequence.push(s);
        }
        var output_fw = enc_fw_lstm.encode(forward_sequence);
        var output_bw = enc_bw_lstm.encode(reverse_sequence);
        var output = nj.concatenate([output_fw, output_bw]);
        var mu = nj.add(nj.dot(output, enc_mu_w), enc_mu_b);
        // optimization:
        if (temp > 0) {
            var presig = nj.add(nj.dot(output, enc_sigma_w), enc_sigma_b);
            var sigma = nj.sqrt(nj.exp(presig));
            var eps = nj.multiply(nj.array(random_normal_vector(), 'float32'), temp);
            var z = nj.add(mu, nj.multiply(eps, sigma));
        } else {
            var z = mu;
        }
        return z.tolist();
    };
github grimmer0125 / alphago-zero-tictactoe-js / src / tictactoe / TicTacToeGame.js View on Github external
getCanonicalForm(boardNdArray, player) {
    // Python:
    // # return state if player==1, else return -state if player==-1
    // return player*board
    return nj.multiply(boardNdArray, player);
  }
github MindExMachina / smartgeometry / services / sketch-rnn / lib / sketch_rnn.js View on Github external
LSTMCell.prototype.forward = function(x, h, c) {
    var concat = nj.concatenate([x, h]);
    var hidden = nj.add(nj.dot(concat, this.Wfull), this.bias);
    var num_units = this.num_units;
    var forget_bias = this.forget_bias;

    var i = nj.sigmoid(hidden.slice([0 * num_units, 1 * num_units]));
    var g = nj.tanh(hidden.slice([1 * num_units, 2 * num_units]));
    var f = nj.sigmoid(nj.add(hidden.slice([2 * num_units, 3 * num_units]), forget_bias));
    var o = nj.sigmoid(hidden.slice([3 * num_units, 4 * num_units]));

    var new_c = nj.add(nj.multiply(c, f), nj.multiply(g, i));
    var new_h = nj.multiply(nj.tanh(new_c), o);

    return [new_h, new_c];
};
LSTMCell.prototype.encode = function(sequence) {