paint-brush
Creating a Racist AI for my Browser with JavaScript and Brain JSby@samchowdhury
572 reads
572 reads

Creating a Racist AI for my Browser with JavaScript and Brain JS

by Samanja CartagenaAugust 21st, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A Neural Network mimics human brain functioning and can be trained to predict outputs based on inputs. Brain JS is a JavaScript library making neural network creation easy; it trains on input data and predicts outcomes. Brain JS creates and trains a neural network with minimal code. GPU.JS accelerates JavaScript using GPUs, enhancing Brain.js. Building a neural network from scratch involves setting up Brain JS, GPU JS, and creating input/output examples. The article demonstrates training AI to predict emotions and experiments with teaching AI biased beliefs, highlighting the current stage of supervised learning in neural networks.

People Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Creating a Racist AI for my Browser with JavaScript and Brain JS
Samanja Cartagena HackerNoon profile picture

First, let’s discuss Neural Networks

What is a Neural Network?

A neural network is a computer model that mimics how a human brain works. A neural network is made up of connected nodes just like neurons in a human brain. Also, just like the human brain, a neural network needs to be trained with inputs so that it can predict an output. For example, if we don’t walk on the sidewalk on a busy road we can have an accident. We have been warned by our parents, teachers, etc. to not walk in the middle of the road but to take the sidewalk for safety. Hence, our brains have been trained to take the sidewalk. In the same way, neural networks can be taught to make the right decision based on inputs.


Neural Network is like our brain !!!


What is Brain JS?

Brain JS is a fantastic way to build neural networks using JavaScript. It is a JavaScript library used for neural networking. It can be used for the browser and for Node JS backends. Brain JS always hides mathematical complexity and makes it very easy to train Neural Networks.


Like all machine learning models, we have to feed our neural network some kind of input. Train it on the input and run it so the machine can predict the output based on the inputs.


To use Brain JS in a Node application we would have to install Brain JS first

npm i brain.js


You can train a neural network with only four lines of code 😎


A neural network is created with :

new brain.NeuralNetwork()

Next, we can train the network with :

network.train([examples])

Then, we can find the corresponding output values with

network.run([examples])

The process is very simple and easy and here is an example to understand how it all comes together. In this article, I am trying to design a racist AI. I will feed my browser racist inputs so that it predicts racist outcomes.


First of all, I think it is a good idea to make the browser as fast as we can because our program would be training our browser to spit out data and hence we would need to make the browser faster so it can compute the outcome quicker.


For this, we would need to install gpu.js

npm i gpu.js


What is GPU.JS?

GPU.JS is a JavaScript library designed to accelerate JavaScript, meaning it can be used for general-purpose computations on GPUs using JavaScript. Hence, it makes JavaScript faster. It supports all browsers and can work with Node JS and TypeScript. Brain.js performs computations using GPU and would fall back to JavaScript when GPU is not available. In case GPU is not available, all the functions still run on regular JavaScript.


Building a Neural Network, with BrainJS, from Scratch

First, let’s create a folder called brainjs

Let us cd into this empty folder brainjs

cd brainjs


Now let’s create a package.json by installing:

npm init -y


Next, it’s time to install brain.js and gpu.js:

npm in brain.js

npm i gpu.js


Now, let’s create two files index.html and index.js. In index.js we will have all our JavaScript codes and all the html would be in index.js.


Let’s connect our JavaScript with our index.html files. I would also suggest adding some brain.js and gpu.js CDN files to our index.html file.


<!DOCTYPE html>

<html>

<head> <meta charset="UTF-8"> <script src="index.js" defer></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/brain.js/2.0.0-beta.1/brain-browser.js" integrity="sha512-bI0AOBSlsQUR/VlZhVj6PuYqjQFO4YTD6Fpw2UBEGC+QkZWLiDgiFZpDefJFnS+ptTjiwZS30VsaF5vJtkQCGw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <script src="//unpkg.com/brain.js"></script>`

<script src="https://cdnjs.cloudflare.com/ajax/libs/gpu.js/1.0.2/gpu.min.js" integrity="sha512-cr2nuynSuSV6MGtWlympE0qd1g1TKBuEhv9lcfbW8HrE9UbPPc8zMwcje1fb9w2kzxqRnsizh6c+YbE6Ab7wpg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

</head>

<body>

<h1>Hello World</h1>

</body>

</html>


Now, let’s start programming our AI in index.js.


Let’s teach our browser how to predict if a user is happy or sad by what they say.

const net = new brain.recurrent.LSTM()

net.train([

{input:'I am happy', output:'happy'},

{input : 'I feel great', output:'happy'},

{input:'happy yeah', output:'happy'},

{input:'sad', output:'sad'} ])

const output= net.run('I am happy')

alert(output)


This will output in the browser, of course after it takes its sweet time.

The browser has learned that the user is happy


Now let’s teach our browser some form of racism, just for fun of course. Please I am not giving anyone any ideas. Just experimenting and seeing if browsers can be taught to be racist. I am about to teach my browser that all Asians are good at math. Let’s input some racist data into our machine.


const net = new brain.recurrent.LSTM()

net.train([

{input:'Jack is Asian', output:'he is good at math'},

{input : 'Ming is Asian', output:'she is good at math'},

{input:'Lee is Asian', output:'he is good at math'},

{input:'Andrea is Irish', output:'she is bad at math'},

{input:'Jackie Chan is Asian',output:'he is good at math'},

{input:'Bobby is Puerto Rican', output:'he is bad at math'} ])

const output= net.run('Emily is Asian')

alert(output)


Look’s like I have successfully taught my browser that all Asians are good at Math.

Yikes, my browser is biased


This is all supervised machine learning. At this stage, Brain JS can only support supervised learning. Many experts think supervised learning would be more beneficial and less detrimental to society because it produces a more accurate model. But I think supervised learning largely depends on the person inputting the data. Faulty data can teach the machine wrong things. Right now, the neural network is still in its infancy and we can leave our awes and gasps for the coming future which will be filled with AI.


Spread the love like a spread operator

[…❤]

Samanja

www.samanja.dev