Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var forward_sequence = [];
var reverse_sequence = [];
var i;
var s;
var N = sequence.length;
for (i = 0; i < N; i++) {
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();
};
var z = nj.array(y);
var init_state = nj.tanh(nj.add(nj.dot(z, enc_w), enc_b));
var c = init_state.slice([0, dec_num_units]).clone();
var h = init_state.slice([dec_num_units, 2 * dec_num_units]).clone();
var rnn_state;
var dx, dy, pen_down, pen_up, pen_end;
var pdf;
var x = nj.array([0, 0, 0, 0, 0]);
var result = [];
var lstm_input;
for (var i = 0; i < max_seq_len; i++) {
lstm_input = nj.concatenate([x, z]);
rnn_state = dec_lstm.forward(lstm_input, h, c);
pdf = get_pdf(rnn_state);
[dx, dy, pen_down, pen_up, pen_end] = sample(pdf, temp, softmax_temp);
result.push([dx, dy, pen_down, pen_up, pen_end]);
if (pen_end === 1) {
return result;
}
x = nj.array([dx / scale_factor, dy / scale_factor, pen_down, pen_up, pen_end]);
h = rnn_state[0];
c = rnn_state[1];
}
result.push([0, 0, 0, 0, 1]);
return result;
};
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) {
function LSTMCell(num_units, input_size, Wxh, Whh, bias) {
this.num_units = num_units;
this.input_size = input_size;
this.Wxh = Wxh;
this.Whh = Whh;
this.bias = bias;
this.forget_bias = 1.0;
this.Wfull = nj.concatenate([Wxh.T, Whh.T]).T;
}
LSTMCell.prototype.zero_state = function() {