Jump to content

Perceptron: Difference between revisions

Added 11l
(→‎{{header|FreeBASIC}}: added google translate)
(Added 11l)
Line 18:
* [https://youtu.be/dXuNAkHsos4?t=16m44s Machine Learning - Perceptrons (youtube)]
<br><br>
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>V TRAINING_LENGTH = 2000
 
T Perceptron
c = .01
[Float] weights
 
F (n)
.weights = (0 .< n).map(_ -> random:(-1.0 .. 1.0))
 
F feed_forward(inputs)
[Float] vars
L(i) 0 .< inputs.len
vars.append(inputs[i] * .weights[i])
R .activate(sum(vars))
 
F activate(value)
R I value > 0 {1} E -1
 
F train(inputs, desired)
V guess = .feed_forward(inputs)
V error = desired - guess
L(i) 0 .< inputs.len
.weights[i] += .c * error * inputs[i]
 
T Trainer
[Float] inputs
Int answer
 
F (x, y, a)
.inputs = [x, y, 1.0]
.answer = a
 
F f(x)
R 2 * x + 1
 
V ptron = Perceptron(3)
[Trainer] training
L(i) 0 .< TRAINING_LENGTH
V x = random:(-10.0 .. 10.0)
V y = random:(-10.0 .. 10.0)
V answer = 1
I y < f(x)
answer = -1
training.append(Trainer(x, y, answer))
[[Char]] result
L(y) -10 .< 10
[Char] temp
L(x) -10 .< 10
I ptron.feed_forward([x, y, 1]) == 1
temp.append(Char(‘^’))
E
temp.append(Char(‘.’))
result.append(temp)
 
print(‘Untrained’)
L(row) result
print(row.join(‘’))
 
L(t) training
ptron.train(t.inputs, t.answer)
 
result.clear()
L(y) -10 .< 10
[Char] temp
L(x) -10 .< 10
I ptron.feed_forward([x, y, 1]) == 1
temp.append(Char(‘^’))
E
temp.append(Char(‘.’))
result.append(temp)
 
print(‘Trained’)
L(row) result
print(row.join(‘’))</lang>
 
{{out}}
<pre>
Untrained
^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^.
^^^^^^^^^^^^^^^^^^..
^^^^^^^^^^^^^^^^^^..
^^^^^^^^^^^^^^^^^...
^^^^^^^^^^^^^^^^....
^^^^^^^^^^^^^^^.....
^^^^^^^^^^^^^^......
^^^^^^^^^^^^^.......
^^^^^^^^^^^^^.......
^^^^^^^^^^^^........
^^^^^^^^^^^.........
^^^^^^^^^^..........
^^^^^^^^^...........
^^^^^^^^............
^^^^^^^^............
^^^^^^^.............
^^^^^^..............
^^^^^...............
^^^^................
Trained
^^^.................
^^^^................
^^^^^...............
^^^^^...............
^^^^^^..............
^^^^^^^.............
^^^^^^^.............
^^^^^^^^............
^^^^^^^^^...........
^^^^^^^^^...........
^^^^^^^^^^..........
^^^^^^^^^^^.........
^^^^^^^^^^^.........
^^^^^^^^^^^^........
^^^^^^^^^^^^^.......
^^^^^^^^^^^^^^......
^^^^^^^^^^^^^^......
^^^^^^^^^^^^^^^.....
^^^^^^^^^^^^^^^^....
^^^^^^^^^^^^^^^^....
</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
1,481

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.