i have a project which use the same data, which in my c++ code it need 17sec to train 100 data, meanwhile in javascript code from this project
https://github.com/CodingTrain/Toy-Neural-Network-JS
it running only about 10sec to train 2400data
please someone help me whats wrong, and i need to complete my project for my undergraduate thesis.
ive already build 2 project, which one of them(this) is the same neuralnetwork in c++ from that javascript code(kinda), but still giving the same results
NeuralNetwork::NeuralNetwork(int a,int b,int c)
{
this->numInput = a;
this->numHidden = b;
this->numOutput = c;
std::vector<double> vec(a, 0.1);
for (int i = 0; i < b; ++i) {
this->weightIH.push_back(vec);
}
std::vector<double> vec2(b, 0.1);
for (int i = 0; i < c; ++i) {
this->weightHO.push_back(vec2);
}
}
NeuralNetwork::~NeuralNetwork()
{
}
std::vector<double> NeuralNetwork::tambahbias(std::vector<double> a) {
int size = a.size();
for (int i = 0; i < size; ++i) {
a[i] = a[i] + 1;
}
return a;
}
std::vector<double> NeuralNetwork::activate(std::vector<double> a) {
int size = a.size();
for (int i = 0; i < size; ++i) {
a[i] = a[i] / (1 + abs(a[i]));
}
return a;
}
std::vector<double> NeuralNetwork::derivation(std::vector<double> a) {
int size = a.size();
for (int i = 0; i < size; ++i) {
a[i] = a[i] * (1 - a[i]);
}
return a;
}
std::vector<double> NeuralNetwork::hitungError(std::vector<double> a, std::vector<double> b) {
int size = a.size();
for (int i = 0; i < size; ++i) {
a[i] = b[i] - a[i];
}
return a;
}
void NeuralNetwork::train(std::vector<double> a, std::vector<double> target) {
std::vector<double> hidden(numHidden);
for (int i = 0; i < numHidden; ++i) {
for (int j = 0; j < numInput; ++j) {
hidden[i] += a[j] * weightIH[i][j];
}
}
hidden = tambahbias(hidden);
hidden = activate(hidden);
std::vector<double> output(numOutput);
for (int i = 0; i < numOutput; ++i) {
for (int j = 0; j < numHidden; ++j) {
output[i] += hidden[j] * weightHO[i][j];
}
}
output = tambahbias(output);
output = activate(output);
std::vector<double> errorO(numOutput);
errorO = hitungError(output, target);
std::vector<double> gradO(numOutput);
gradO = derivation(output);
for (int i = 0; i < numOutput; ++i) {
gradO[i] = gradO[i] * errorO[i] * 0.1;
}
for (int i = 0; i < numOutput; ++i) {
for (int j = 0; j < numHidden; ++j) {
weightHO[i][j] += (gradO[i] * hidden[j]);
}
}
std::vector<double> gradH(numHidden);
std::vector<double> derH(numHidden);
derH = derivation(hidden);
for (int i = 0; i < numHidden; ++i) {
for (int j = 0; j < numOutput; ++j) {
gradH[i] = gradO[j] * weightHO[j][i];
}
gradH[i] = gradH[i] * derH[i] * 0.1;
}
for (int i = 0; i < numHidden; ++i) {
for (int j = 0; j < numInput; ++j) {
weightIH[i][j] += (gradH[i] * a[j]);
}
}
}
You're copying all your std::vectors into functions:
void NeuralNetwork::train(std::vector<double> a, std::vector<double> target)
use references instead:
void NeuralNetwork::train(const std::vector<double>& a, const std::vector<double>& target)
Copying a vector is an O(n) operation in both space and time, using a reference is O(1) in both.
A const std::vector reference can't be modified, when you're copying the vector in and out again after modifying it:
std::vector<double> NeuralNetwork::derivation(std::vector<double> a)
use a non-const reference instead:
void NeuralNetwork::derivation(std::vector<double>& a)
it turns out im just an idiot, who doesnt know about debug / release, make this program to release just solve the problem, thank you everyone for your help
Related
I am trying to make Pascal's Triangle in JavaScript but there are lots of errors. I have know idea why errors are happening but there are some.
Code:
function triangle() {
this.rows = [[1]];
this.genRow = function() {
this.rows.push([]);
this.rows[this.rows.length-1].push(1);
for (var i = 0; i < this.rows[this.rows.length-1].length; i++){
var u = [this.rows[this.rows.length-1][i-1], this.rows[this.rows.length-1][i], this.rows[this.rows.length-1][i+1]];
var f = function(e) {
return e != undefined;
};
function s() {
var sum=0;
for (var index = 0; i < this.legnth; i++){
sum =+ this[i];
}
return sum;
}
u = u.filter(f).s();
this.rows[this.rows.length-1].push(u);
}
this.rows[this.rows.length-1].push(1);
}
}
var t = new triangle();
t.genRow();
console.log(t.rows);
Thanks.
Please try this code,To Pasqual's triangle gone wrong
#include <stdio.h>
int binomialCoeff(int n, int k);
void printPascal(int n)
{
for (int line = 0; line < n; line++)
{
for (int i = 0; i <= line; i++)
printf("%d ",
binomialCoeff(line, i));
printf("\n");
}
}
int binomialCoeff(int n, int k)
{
int res = 1;
if (k > n - k)
k = n - k;
for (int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
int main()
{
int n = 7;
printPascal(n);
return 0;
}
I hope this code will be useful.
Thank you.
const pascalsTriangle = (rows = 1) => {
let res = [];
for (let i = 1; i <= rows; i++) {
if (i == 1) {
res.push([1]);
}
else if (i == 2) {
res.push([1, 1]);
}
else {
let arr = [1];
let lastArr = res[i - 2];
for (let index=0; index<lastArr.length-1; index++) {
arr.push(lastArr[index] + lastArr[index + 1]);
}
arr.push(1);
res.push(arr);
}
}
return res;
};
This will work perfectly. You can refer it here. https://github.com/omkarsk98/Exercism/blob/master/javascript/pascals-triangle/pascals-triangle.js
Here is an approach in JS.
function getNextLevel(previous) {
const current = [1];
for (let i = 1; i < previous.length; i++) {
current.push(previous[i] + previous[i - 1]);
}
current.push(1);
return current;
}
function pascalTriangle(levels = 1) {
let currentRow = [1];
while (levels--) {
console.log(currentRow.join(" "));
currentRow = getNextLevel(currentRow);
}
}
pascalTriangle(10);
i require matrixo function that in random.js, but in server.js but program cant find this function. Where do i need require random .js for fixing?
server.js
var matrix = matrixo(40, 40);
let random = require('./modules/random.js');
random.js
function matrixo(m) {
var matrix = [];
for (var i = 0; i < m; i++) {
matrix.push([]);
for (var j = 0; j < m; j++) {
matrix[i][j] = Math.floor(Math.random() * 3);
}
for (var j = 0; j < m; j++) {
matrix[i][j] = Math.floor(Math.random() * 4);
}
for (var j = 0; j < m + 3; j++) {
matrix[i][j] = Math.floor(Math.random() * 5);
}
for (var j = 0; j < m; j++) {
matrix[i][j] = Math.floor(Math.random() * 6);
}
for (var j = 0; j < m; j++) {
matrix[i][j] = Math.floor(Math.random() * 7);
}
}
return matrix;
}
module.exports = matrixo;
error - ReferenceError: matrixo is not defined
You need to assign the return value (which is the exported value) to the variable you are trying to use, and you need to do it before you use that variable.
let matrixo = require('./modules/random.js');
var matrix = matrixo(40, 40);
Here's my code with cycles and I want to make it shorter (in one cycle if possible).
function plan(piece) {
for (i = 0; i < 10; i++) {
piece.addStep('right');
}
for (i = 0; i < 9; i++) {
piece.addStep('down');
}
for (i = 0; i < 8; i++) {
piece.addStep('left');
}
for (i = 0; i < 7; i++) {
piece.addStep('up');
}
}
etc... to i < 1
I thought about it that case,
function plan(piece) {
for (i=10; i>1; i--){
piece.addStep('right');
piece.addStep('down');
piece.addStep('left');
piece.addStep('up');
}
but it's was wrong. Help pls!
here's look of task(maze)
You can add function for the repeating logic :
function addSteps(piece, n) {
while (n--) {
piece.addStep(piece);
}
}
addSteps('right', 10);
addSteps('down', 9);
addSteps('left', 8);
addSteps('up', 7);
Simply combine all of them by introducing some if Checks.
For Example :
function plan(piece) {
for (i = 0; i < 10; i++) {
piece.addStep('right');
if(i < 9)
piece.addStep('down');
if(i < 8)
piece.addStep('left');
if(i < 7)
piece.addStep('up');
}
}
One option is:
function plan(piece) {
['right', 'down', 'left', 'up'].forEach((dir, ind) => {
for (let i = 0; i < 10 - ind; i++) {
piece.addStep(dir);
}
});
}
Doubt it is efficient, but you can be Array fill, concat, and forEach
var steps = [].concat(Array(10).fill("right"), Array(9).fill("down"), Array(8).fill("left"), Array(7).fill("up"))
steps.forEach(dir => piece.addStep(dir));
You could take nested loops and increment the index for getting the right direction.
var sequence = ['right', 'down', 'left', 'up'],
i, j,
k = 0,
l = sequence.length;
for (i = 0; i < 10; i++) {
for (j = i; j < 10; j++) {
document.getElementById('out').innerHTML += sequence[k] + '\n';
// or piece.addStep(sequence[k]);
}
++k;
k %= l;
}
<pre id="out"></pre>
I am trying to adapt an established dynamic programming Matrix Chain Multiplication algorithm to compare Javascript's performance to other languages.
This is the Java version found on Wikipedia from which I am adapting to Javascript:
void matrixChainOrder(int[] p) {
int n = p.length - 1;
m = new int[n][n];
s = new int[n][n];
for (int L = 1; L < n; L++) {
for (int i = 0; i < n - L; i++) {
int j = i + L;
m[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
int q = m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
}
Below is the version I am adapting and it is the same as far as I can see, but the console logs the following error message: TypeError: m[(k + 1)] is undefined
var matrixChainOrder = function(p) {
var n = p.length - 1;
var m = [[,]];
var s = [[,]];
for (var L = 1; L < n; L++) {
for (var i = 0; i < n - L; i++) {
var j = i + L;
m[i][j] = Number.MAX_SAFE_INTEGER;
for (var k = i; k < j; k++) {
var q = m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
}
var array = [15, 12, 16, 17, 19];
var d = new Date();
var start = d.getMilliseconds();
matrixChainOrder(array);
var end = d.getMilliseconds();
console.log(end - start);
I don't understand why a TypeError is being thrown... hoping someone else can spot the problem and help me out.
function search(pattern, text) {
var M = pattern.length;
var N = text.length;
for (var i = 0; i < N - M; i++) {
var j =0;
while (j < M) {
if (text.charAt(i + j) != pattern.charAt(j)) {break;}
}
if (j == M) {return i;}
}
return -1;
}
console.log(search("rf", "jdsrfan"));
I want to make an brute-force string matching algorithm in JavaScript. Can anyone tell me whats wrong with above code?
I did fixed it myself fixed code as follows:
// return offset of first match or -1 if no match
function bruteForcePatternSearch(sPattern, sText) {
var M = sPattern.length,
N = sText.length;
for (var i = 0; i <= N - M; i++) {
var j=0;
while (j < M) {
if (sText.charAt(i+j) !=sPattern.charAt(j)){
break;
}
j++;
}
if (j == M) {return i;} // found at offset i
}
return -1; // not found
}
bruteForcePatternSearch("abracadabra","abacadabrabracabracadabrabrabracad");
You're never incrementing j to start with. Hence the infinite loop.
Then, as Claudio commented, i < N - M is wrong. Should be i <= N - M.
Spoiler: here the fixed function. But I advise you not to take it as-is, but to try doing it yourself instead.
function search(pattern, text) {
var M = pattern.length;
var N = text.length;
for (var i = 0; i <= N - M; ++i) {
var matched = true;
for (var j = 0; j < M; ++j) {
if (text.charAt(i + j) != pattern.charAt(j)) {
matched = false;
break;
}
}
if (matched) {
return i;
}
}
return -1;
}
i guess this will work
for (var i = 0; i < M; i++) {
var j =0;
while (j < N) {
if (text.charAt(j) != pattern.charAt(i)) {
break;
}
j++
}
if (j == M) {return i;}
}
here is the explanation
on each pattern
match each character of text