DEV Community

Cover image for Flatiron Project 4 JavaScript
Damon Marc Rocha II
Damon Marc Rocha II

Posted on

Flatiron Project 4 JavaScript

For my fourth project here at flatiron, I had a little trouble deciding what to build. The first few days I spent on my project was solely dedicated to trying to come up with a viable project idea. After much planning and inner conflict, I found myself starting my project 'Synthetic Ai'.
I found the inspiration for this idea through the article: https://waitbutwhy.com/2015/01/artificial-intelligence-revolution-1.html
About the advancement of tech and the impact Ai will have on our future. Along with an idea given to me by my cohort lead using Ai to recognize objects and such. So with Ai in mind, I found brain.js which is a customizable neural network that works in browser and with node.js. Luckily in the README was a link to a short course covering the basic usage of this neural network:
https://scrimba.com/course/gneuralnetworks/

With all this in hand, I was able to start my project. Using Rails as an API and javascript as the main content controller of my front end, I flew through the initial setup. I found this separation of front and back to be refreshing. Not only did it make moving my data around a lot easier but I felt that building my app this way made a lot more sense than jumbling everything together.
Everything went smoothly until I started building the AI's.
I decided to build two user-customizable AI's. One that takes in likes and dislikes and outputs how much a user likes whatever object they choose. This AI is meant to be the start to building a recommendation engine. And for the other a user inputs various sentences and moods and the AI will try to return the mood of any sentence input. What I wanted to do here was allow people who don't know much about AI to get a foot in the door using a control panel that does most of the setup for them.
This was all fine and good until I started brain learning and output. Since all my data was a string, but had to be an object with strings and numbers for the brain. I spent a lot of time with console.logs and testing different data types. After much research I found something I was already doing that would fix my problem:

    let learningData = [];
    let tmpData = this.data.split(",\n");
    for(let i = 0; i < tmpData.length; i++){
        let obj = JSON.parse(`${tmpData[i]}`);
        console.log(obj);
        learningData.push(obj);
    }
    console.log(learningData);
    this.net.train(learningData);
    alert('Done Training');
Enter fullscreen mode Exit fullscreen mode

I had to split the data on every new line since the data looked like "{"input": { "val": 1}, "output":[1]},\n", and then parse it to JSON so that it would become an object with the corresponding values like so {input: {val: 1}, output: [1]}. This worked very well with both of my AIs, and after this, I really did not have any problems.
I enjoyed using Javascript as my front end for this project and messing with brain.js. I used to work with neural networks in the past, but have not kept up with it. I hope to build onto this project in the future and eventually add more functionality with Javascript and brain.js.

Top comments (0)