Machine Learning
These are Apas Cloud services: they run on our servers for a signed-in member. The examples below are shown already made; sign in to edit and run your own.
nn Apas Cloud
Creates a neural network (fully connected, feed-forward). The sizes list the neurons of each layer, the input first: [2, 4, 1] has 2 inputs, a hidden layer of 4 and 1 output. The activations are one name for every layer ("sigmoid", "tanh", "relu", "linear"; "softmax" only on the last) or a list with one for each layer after the input. The starting weights come from the seed (He before relu, Xavier otherwise), so the same seed always gives the same network. Shows as a picture: a circle a neuron, a line a weight (blue positive, red negative, thicker when larger). Its numbers are approximate: training is a numerical method.
sizes (number[]) -- neurons in each layer, input first (each 1 to 4096)
activations (string | string[]) -- optional; default "sigmoid"
seed (number) -- optional; default from the script's random engine
nnSetWeights Apas Cloud
Sets the weights and biases of one layer, numbered as in textbooks: layer 1 is the first after the input, so its weights W1 join the input to it. W is one row a neuron of that layer, each row as long as the layer before; b has one bias a neuron. Use it to work a network by hand.
net (neuralnet) --
layer (number) -- 1 to the output layer
W (number[][]) -- one row a neuron
b (number[]) -- one bias a neuron
nnWeights Apas Cloud
The weights W (one row a neuron) and biases b of one layer, layer 1 being the first after the input.
net (neuralnet) --
layer (number) -- 1 to the output layer
nnPredict Apas Cloud
The network's outputs for one input [x1, x2, ...], or for each input of a list of inputs. Approximate numbers.
net (neuralnet) --
x (number[] | number[][]) -- one input or a list of inputs
nnLoss Apas Cloud
The mean loss of the network over examples X (inputs) with targets Y. The loss is "mse" (the mean of (output - target)^2 over the outputs) or "cross_entropy" (after a sigmoid or softmax output); by default cross-entropy after sigmoid or softmax, mse otherwise. A flat list is one number an example.
net (neuralnet) --
X (number[][]) -- the inputs
Y (number[][]) -- the targets
loss (string) -- optional: "mse" or "cross_entropy"
nnTrain Apas Cloud
Trains the network by backpropagation and gradient descent for the given epochs at the learning rate, changing the network in place, and returns the loss before training and after each epoch (plot it to see learning). Optional: the loss (default as nnLoss), the batch size (0, the default, uses every example in each step; 1 is one example at a time) and the way the weights move: a momentum from 0 up to 1, or "adam" (Adam: each weight steps by about the learning rate, scaled by running averages of its own gradients; a rate near 0.01 to 0.05 suits it). The same network, data and options always give the same result.
net (neuralnet) --
X (number[][]) -- the inputs
Y (number[][]) -- the targets
epochs (number) -- 1 to 100000
rate (number) -- the learning rate, positive
loss (string) -- optional
batch (number) -- optional; 0 = all examples
momentum (number | string) -- optional; 0 to below 1, or "adam"
nnDraw Apas Cloud
The network's picture; with an input, each neuron shows its value and is shaded by it (a forward pass at a glance).
net (neuralnet) --
x (number[]) -- optional input
nnForward Apas Cloud
Animates a forward pass layer by layer: the input, then for each layer its weighted sums z = w·a + b (each neuron adds its weighted inputs and its bias, the weights into that layer drawn strongly) and its activations a, then the answer. Each step is captioned with its numbers.
net (neuralnet) --
x (number[]) -- the input
nnBoundary Apas Cloud
For a network with two inputs: the plane coloured by the network's answer (one output: blue toward 0, orange toward 1, pale where it is unsure; several outputs: the colour of the largest), with the examples on top in the colour of their class.
net (neuralnet) -- two inputs
X (number[][]) -- the points
Y (number[][]) -- their targets
nnAnimate Apas Cloud
Trains the network like nnTrain and returns an animation from start to end (31 frames of the network, 21 of the decision regions), each captioned with its epoch and loss: the network with its weights changing ("network", the default) or, for two inputs, its decision regions moving into place ("boundary"). Optional last: a momentum or "adam", as in nnTrain.
net (neuralnet) --
X (number[][]) -- the inputs
Y (number[][]) -- the targets
epochs (number) -- 1 to 100000
rate (number) -- the learning rate
view (string) -- optional: "network" or "boundary"
loss (string) -- optional
momentum (number | string) -- optional; 0 to below 1, or "adam"
neuron Apas Cloud
One neuron worked step by step: z = w·x + b computed exactly, then its output a = f(z) for the activation (default "sigmoid"; exact for "relu" and "linear", approximate for "sigmoid" and "tanh"). The fields z and a can be used further.
w (number[]) -- the weights
x (number[]) -- the inputs
b (number) -- the bias
activation (string) -- optional
gradientDescent Apas Cloud
Gradient descent on f: from the start, steps of x - rate·f'(x) (the slope estimated by central differences), until the slope is 0 or the steps run out. One variable (f, x, x0) or several ([x, y] and [x0, y0]). Returns the point reached, f there, the steps taken, whether the slope reached 0, and the whole path. Approximate.
f (expression) -- the function to minimise
var (name | name[]) -- x, or [x, y]
start (number | number[]) -- where to start
rate (number) -- the learning rate
steps (number) -- at most this many steps
descentPlot Apas Cloud
Gradient descent as gradientDescent, animated step by step: for one variable the ball rolling down the curve y = f(x), for two variables the path across the level curves of f. At most 150 frames.
f (expression) --
var (name | name[]) --
start (number | number[]) --
rate (number) --
steps (number) --
kmeans Apas Cloud
k-means clustering: k starting centres chosen far apart (k-means++, from the seed), then each point joins its nearest centre and each centre moves to the mean of its points, until nothing changes. Returns the centres, each point's cluster (labels, from 0), the total squared distance (inertia) and the rounds taken.
points (number[][]) -- the points
k (number) -- 1 to the number of points
seed (number) -- optional
kmeansPlot Apas Cloud
k-means as kmeans, animated for points in the plane: the starting centres, then each round's two steps (points join their nearest centre; centres move to the mean).
points (number[][]) -- points [x, y]
k (number) --
seed (number) -- optional
perceptron Apas Cloud
Rosenblatt's perceptron for two classes (labels 1 and -1): for each example in turn, if it is on the wrong side of w·x + b = 0 then w = w + rate·y·x and b = b + rate·y, until an epoch makes no mistake or the epochs run out (default rate 1, 100 epochs). Returns w, b, whether it separated every example, and the updates made.
X (number[][]) -- the inputs
labels (number[]) -- 1 or -1 each
rate (number) -- optional
epochs (number) -- optional
perceptronPlot Apas Cloud
The perceptron as perceptron, animated for points in the plane: after each update the line w·x + b = 0 turns, the point that caused it marked; the last frame says whether the classes are separated.
X (number[][]) -- points [x, y]
labels (number[]) -- 1 or -1 each
rate (number) -- optional
epochs (number) -- optional