Neural Network Library 2.0

All this function does is determine if the given XY coordinates are above the cubic function. This is why it calculates the Y of the cubic at the given X and it then compares this Y to the given Y. Whether or not 0 or 1 is decided as ‘above’ or ‘below’ doesn’t matter, what matters is that it remains this way and there is a distinct difference between the 2 possible outputs.

3 Likes

The weights aren’t that simple to retrieve, especially in an OOP implementation where you need to grab them from every individual node. If you haven’t, you should check out the introduction page of the website to make sure you understand how weights work.

1 Like

It is indeed a typo, I changed it afterward and forgot to change the comment.

This is actually already a feature, I just completely forgot to document it on the website as I didn’t think people would really need it. I’ll put it up when I get around to it!

2 Likes

No like get your model forward propogate and back propogate functions and just constantly update those weights (which are a array of numbers ranging from 0 to 1, i dont know his model but thats how i do it)

local weights = {
 0,0,0,0,0,0,0,0,0,0
}
game:GetService("RunService").Heartbeat:Connect(function()
  forwardPropogate() --This tells us our outputs and how much they were off from the target
  backwordPropopgate() --This applies that delta change to the previous node layers
end)
1 Like

How would infinite node layers work? Oh nvm im stupid I thought you tried to create a sigma with no end which would f up the ratios lol

1 Like

If I pass a Vector X and Vector Z, I would expect a value for those vectors but it gives this random values like 0.472 and can you tell me what does the output actually give?

1 Like

How can I implement reinforcing learning using this? In the example it is supervised learning.

3 Likes

For this, you would use the GeneticAlgorithm class.
There is some example code for this here!

3 Likes

Hey Kironte do you ever plan to add on a greyscale mapper? (Asking cause I was wondering if you’ve already coded one cause I don’t want to make my own xd)

3 Likes

I’m not really sure what that is but I’m not planning to add anything to this library anytime soon.

2 Likes

Hey I just stumbled upon this module and I really want to create an ai with it and I was curious on how I should do it.
The bot will:

  • Attack with a 5 hit combo
  • Block
  • circle the player

I don’t need or want any code I just would like some advice/ help on how I should do it.

3 Likes

The roblox community likes to nickname pathfinding as AI, thats what you really want. Look into PathfindingService

3 Likes

Um, feeling kinda like an idiot asking this, but how do I start doing anything with this, and where do I start?

3 Likes

To start, you should visit the website and read through the introduction. From there, examine the example code and look at the documentation to get a good sense of what the library and you can do.

3 Likes

Sorry for the late response, but I thought I’d write a guide in case anyone is still interested in this resource and wants to try for themselves. A warning should be made that this stuff is hard and time consuming, and will result in lost brain cells no matter how good at scripting you are. I wouldn’t take it at first as anything more than a fun practice script to play around with.
I found the library pretty amazing and very easy compared to something like Tensorflow, but it wasn’t documented in a way I could understand well, meaning I had spend a lot of time figuring out what each thing did.
I’ve written a guide below based on my knowledge designed in a way so that if I were to see this before I tried out the network it would help me.
Please comment if anything I’ve said is not correct or could be improved.

Guide

What does a neural network do?
A neural network is given a function that takes in a value and returns a value and, after being trained upon many different values, tries to guess what it will output from a given value. For simplicity, we use a function that returns either 0 or 1 if the value entered is above or below a point in a cubic graph. If you want to do fancy things like text generation, then you will have to convert each word or letter into a number as neural networks work in numbers.

What is a single network?
This requires you to enter a learning rate, which is how fast the algorithm is willing to change. If you give it a low learning rate like 0.01, it will be very hesitant to change its outcome. This means you have to experiment with many different learning rates before you can get a desired result.

Why does a genetic algorithm help?
This works by finding the learning rate that works best by using different generations. It will evaluate how good the learning rate was by the success of the generation. At the end, it chooses the most successful generation and goes with that.

--This ensures that the math.random() function will return a properly random value.
--In my opinion, it would be better to use Random.new() or instead of this do
--math.randomseed(tick()+os.time()).
math.randomseed(os.clock()+os.time())

local Package = game:GetService("ReplicatedStorage").NNLibrary
local Base = require(Package.BaseRedirect)
local FeedforwardNetwork = require(Package.NeuralNetwork.FeedforwardNetwork)
local Momentum = require(Package.Optimizer.Momentum)

--This is used to prevent Roblox freezing and timing out.
local clock = os.clock()


----------------<<MAIN SETTINGS>>---------------------------------------------------------------

local setting = {
    --Apparently, this optimizer works best. Don't argue with the professional.
    Optimizer = Momentum.new();
    --Fun fact: LeakyReLU is an improved version of ReLU that stops the
    --"dying ReLU problem" where a neuron will always output 0, and cannot recover.
    --It's best not to change these for now.
    HiddenActivationName = "LeakyReLU";
    OutputActivationName = "Sigmoid";
    --Because you aren't using a genetic algorithm, you will need to change this.
    --0.3 is a very high learning rate, try experimenting with a lower value.
    LearningRate = 0.3;
}

--How long it trains for. Be warned, the longer you train does not necessarily
--equate to a higher quality output.
local generations = 200000
--This is a hacky way of making it faster.
local numOfGenerationsBeforeLearning = 1

----------------<<END OF MAIN SETTINGS>>---------------------------------------------------------------


--This has two coordinates: x and y. These are parameters to the function. You
--can name them what you want and have as many as you want.
--This has two layers of three neurons each. This will affect the quality of
--the output.
local net = FeedforwardNetwork.new({"x","y"},2,3,{"out"},setting)
local backProp = net:GetBackPropagator()

function isAboveFunction(x, y)
    --This is the function that it will try and guess what it will return (output).
    --If the input x in the cubic graph below is greater than y it returns 0.
    --0 should be returned if it is a "success".
    if x^3 + 2*x^2 < y then
        return 0
    end
    return 1
end

--Training phase
for generation = 1, generations do
    --This chooses some random values for x and y between -4 and 4.
    local coords = {x = math.random(-400,400)/100, y = math.random(-400,400)/100}
    --The neural network then tries them on the function and will magically learn
    --based on the response.
    local correctAnswer = {out = isAboveFunction(coords.x,coords.y)}

    --This calculates how far away it was from the answer.
    backProp:CalculateCost(coords,correctAnswer)

    --This is so Roblox doesn't freeze.
    if os.clock()-clock >= 0.1 then
        clock = os.clock()
        wait()
        print(generation/generations*(100).."% trained. Cost: "..backProp:GetTotalCost())
    end

    if generation % numOfGenerationsBeforeLearning == 0 then
        backProp:Learn()
    end
end

local totalRuns = 0
local wins = 0

--Testing phase
for x = -400, 399 do
    for y = -400, 399 do

        --Gets some 'random' coordinates for testing.
        local coords = {x = x/100, y = y/100}
        --The 'net' function asks the neural network to guess what the returned value (output)
        --of the function will be given these coordinates.
        local output = net(coords)

        --Gets what the actual output is.
        local correctAnswer = isAboveFunction(coords.x,coords.y)

	--Even if the function only returns 0 or 1, the neural network may still guess
        --a decimal value such as 0.1413409109385. If the difference is less than 0.3
        --then it's a win. Since this is a testing phase and it's not learning and
        --it only outputs 0 and 1, this may as well be 0.5 as it only has two outputs.
        if math.abs(output.out - correctAnswer) <= 0.3 then
            wins += 1
        end
        totalRuns += 1
    end

    if os.clock()-clock >= 0.1 then
        clock = os.clock()
        wait()
        print("Testing... "..(x+400)/(8).."%")
    end
end

print(wins/totalRuns*(100).."% correct!")
8 Likes

How can I make an AI Chat Bot with this?

4 Likes

bro thats kinda far fetched of u asking that

I mean just a basic idea of how I can even do it.

2 Likes

Very cool! I’ve never seen any kind of machine learning thing on roblox, so this is pretty new! :smiley:

The machine uprising is soon…

1 Like

Not all chat bots are alike, but I wouldn’t recommend trying to make a chat bot with machine learning on Roblox. Not because it is hard (which it is but that’s beside the point), but because Luau isn’t practical for it (especially when the server hosting a bunch of players has to handle it). Other than that, text recognition requires LSTM networks, which this library has, but I have no way of confirming if it works as it should.
You should try to do it for educational purposes (and I’m sure everyone would be curious about the results), but not for practical purposes.

3 Likes