How to find pattern with brain.js - javascript

Disclaimer: I am new to machine learning. Please forgive me if I am asking a super dumb question.
Hi I am trying to find pattern in a set of numbers using brain.js.
I have expanded on the example in brain.js github page.
const net = new brain.recurrent.LSTMTimeStep({
inputSize: 3,
hiddenLayers: [10],
outputSize: 3
});
net.train([
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8]
]);
const output = net.run([[7, 7, 7], [8, 8, 8]]);
I was trying to get an output of [9, 9, 9], but I am mostly getting [8, 8, 8].
But if I try running const output = net.run([[5, 5, 5], [6, 6, 6]]); I am easily getting [7, 7, 7] & consecutive output with other numbers in the training data sequence.
Is there anyway to train it so that I can get the desired output and the use it for other patters?

You are using a LSTM time step recurrent neural network trained using "supervised learning": https://en.wikipedia.org/wiki/Supervised_learning, which provides similar output values it has been given previously to the .run() call.
What it seems you want is a LSTM time step recurrent neural network trained using "reinforcement learning": https://en.wikipedia.org/wiki/Reinforcement_learning, which can output values it has seen before, but also new ones, using values it has never been given previously to the .run() call, based off exploitation and exploratory (random) findings.
Brain.js doesn't yet have a reinforcement learning API, although that will be the focus after v2 stabilizes.
Ty for providing a clear question and reproduction steps. I've included your sample, working in the browser so others can play with it: https://jsfiddle.net/robertleeplummerjr/4o2vcajt/1/

Related

Is there any way of converting OpenCV.Mat to array in JavaScript?

I'm working on Mat in OpenCV. However, I need to manually calculate the Mat by myself. Is there is a way of accessing Mat likes 2D array?
const myMat = cv.matFromArray(cv, 3, 3, cv.CV_64F, [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
const newMat = someProcessThatReturnMat(myMat)
/* OpenCV in JS cannot access Mat like this */
const result = someProcess(newMat[1][2], newMat[2][0])
Thank you in advance
Updated: The problem is cv.matFromArray cannot convert 2D array to Mat. You have to use it as 1D array. That's why it never return the correct values. For example:
const myMat = cv.matFromArray(3, 3, cv.CV_64F, [1,2,3,4,5,6,7,8,9])
And then, you can access the value
const value = myMat.doubleAt(1, 2) // row 1, col 2
You need to use the doubleAt(y,x) method.
It's double because the mat's content is CV_64F, not because you want doubles.
You can also use .data64F to access a flat Float64Array of the Mat's data.
OpenCV.js is... rough. It originated from someone's Google Summer of Code and hasn't received significant care since. Documentation amounts to some tutorials; API docs seem to be missing entirely. The "Mat" interface emulated the at() method from C++, badly, instead of looking at numpy (python) or making this access feel "native" to javascript. Overloading the [] operator is possible using a Proxy but that was not implemented.
Here's an example: https://docs.opencv.org/4.x/de/d06/tutorial_js_basic_ops.html
Feel free to browse OpenCV's issues and maybe suggest some improvements.

Why does passing a large dictionary from python to julia flatten contained multidimensional lists?

Okay so...I'm in the process of creating a python client for a coding game I play. This game is originally created in javascript and employs a json object of game data found at https://adventure.land/data.js. Within this data, each map has a list of doors, and each door within this list is a list of attributes. For example:
"doors":[[-965,-176,24,30,"woffice",0,1],[536,1665,64,32,"tunnel",0,2],[168,-149,32,40,"bank",0,3],[160,1370,24,32,"cave",0,4],[232,384,24,30,"arena",0,6],[-472,131,24,30,"tavern",0,8],[616,610,32,40,"mansion",0,10],[1936,-23,24,24,"level1",1,11],[169,-404,24,40,"hut",0,14],[1600,-547,60,40,"halloween",4,15],[312,-335,32,32,"mtunnel",0,16],[967,-584,32,32,"mtunnel",1,17],[1472,-434,32,32,"mtunnel",2,18]]
However, when sent from python to julia, it suddenly becomes:
"doors":[-965,-176,24,30,"woffice",0,1,536,1665,64,32,"tunnel",0,2,168,-149,32,40,"bank",0,3,160,1370,24,32,"cave",0,4,232,384,24,30,"arena",0,6,-472,131,24,30,"tavern",0,8,616,610,32,40,"mansion",0,10,1936,-23,24,24,"level1",1,11,169,-404,24,40,"hut",0,14,1600,-547,60,40,"halloween",4,15,312,-335,32,32,"mtunnel",0,16,967,-584,32,32,"mtunnel",1,17,1472,-434,32,32,"mtunnel",2,18]
Is there some way to prevent this from happening? If not, is there a possible workaround? Because my for door in doors code in python won't exactly have the same effect when given a direct translation to julia considering the random list flattening.
Edit: I've been asked to provide "minimum reproducible code" but I'm not too sure what the minimum is so I'll just say...
create a some function in julia that prints a dictionary
function printDict(d)
println(d)
end
create a dictionary in python that contains a nested list
nestedlistdict = {"data": [[0,1,2,3],[4,5,6,7]]}
call the printDict function from python while sending the nestedlistdict variable as an argument
from julia import Main
Main.include("path/to/your/julia/file")
Main.file.printDict(nestedlistdict)
result should be:
Dict{Any, Any}("data" => [[0, 1, 2, 3], [4, 5, 6, 7]])but instead my result is Dict{Any, Any}("data" => [0 1 2 3; 4 5 6 7])
Edit2: upon further testing, it seems to be having the issue because it converts a nested list into a Matrix object instead of a nested Vector object.
Julia uses the same interpretation of list of lists as numpy:
>>> np.array([[1,2,3],[4,5,6]])
array([[1, 2, 3],
[4, 5, 6]])
or in Julia:
julia> using PyCall
julia> py"[[1,2,3],[4,5,6]]"
2×3 Matrix{Int64}:
1 2 3
4 5 6
If you want to have an actual vector of vectors you need to do the same way as you need to do a vector of numpy arrays.
Python version:
>>> [np.array([1,2,3]),np.array([1,2,3])]
[np.array([1, 2, 3]), np.array([1, 2, 3])]
Julia version (this is what you are actually asking for):
julia> np = pyimport("numpy");
julia> py"[$np.array([1, 2, 3]), $np.array([1, 2, 3])]"
2-element Vector{Vector{Int32}}:
[1, 2, 3]
[1, 2, 3]
Another, perhaps simpler option could be packing that all to a tuple:
julia> py"tuple([[1,2,3],[4,5,6]])"
([1, 2, 3], [4, 5, 6])

Logging from Chrome console to Python

I'm looking for a way get a console output from Google Chrome into my python program. I have a script coded in JS that finishes in around 1 second, but my python implementation (exactly same logic, etc, the only diff is that it's in Python and not JS) takes about 15 seconds to run. Therefore I'm looking for a way to get the printout in Chrome console to my python program.
This is the current way I'm doing it:
Python program uses pyautogui to click and does what it needs to do inside to trigger the function running in JS.
JS completes the function in 1s and prints to console, something like:
(22) [6, 4, 4, 6, 0, 0, 2, 4, 4, 6, 4, 2, 4, 4, 6, 0, 0, 2, 4, 4, 6, 0]
I need to get that into my python program so it can does the rest of the stuff..

Grandient colors in each serie(line) Highcharts

Does someone know if i could make a 3 dimensions chart but by using a regular 2D line chart such as linear chart but the 3rd axis would be represented by a gradient color onto the serie's line:
If the 3rd dimension is high with a hot color (color of the line) and if it is low with a cold color (for example).
Thank everyone for the help. Excuse my lack of knowledge, I'm beginning...
In addition:
These two graphs are made from these points:
[x y z]
1st section
[1, 2, 10]
[6, 4, 6]
2nd section
[6, 4, 6]
[10, 3, 4]
3rd section
[3, 10, 13]
[6, 4, 6]
4th section
[10, 3, 4]
[13, 13, -6]
X&Y graph and X&Z graph
Well, what I'd like to do is represent just the first graph (X&Y) and the Z coordinate represented by a gradient color kind of THIS gradient but within the lines. Instead those default colors.

It's possible to have line charts with irregular data series (not time series) in javascript / html5? Are there libraries for that?

I need a javascript (HTML4/5) based library to draw a line chart that has irregular data (and one or more series).
Here is an example:
x-axis = [1, 2, 3, 4, 5, 6, 7, 8]
series1 (y-axis) = [6, 10, null, 8, null, 7, 6, 4]
series2 (y-axis) = [null, 8, 8, null, 1, 3, 5, 4]
In this chart the nulls are not taken in consideration; are in some way "skipped" and the line goes directly into the next value. Of course i want to highlight the points to know if a point is or isn't there at that x value.
There are some of this charts with irregular data based on time series (like this highcharts.com/demo/spline-irregular-time) but i need that defining my own (regular) x-axis.
What do you suggest?
Highcharts can accept irregular data without using times.
Basically, you take the example that you posted, and remove the type: 'datetime' option, and specify numbers instead of dates in the series.
I have posted an example here: http://jsfiddle.net/TRLhc/

Categories