-
Notifications
You must be signed in to change notification settings - Fork 3
/
NeuralNetworkLayer.swift
111 lines (88 loc) · 4 KB
/
NeuralNetworkLayer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// NeuralNetworkLayer.swift
// DataFlowTests
//
// Created by Michael Rudolf on 24.10.23.
//
import Foundation
class NeuralNetworkLayer{
var newNeurons: [Neuron]
var oldNeurons: [Float]
var connectionStrengths: [Float]
private var lastConnectionVariation = [Float]()
static func makeNetworkWithNeurons(_ neurons: [Neuron], oldNeuronNumber: Int) -> NeuralNetworkLayer{
let connectionsCount = oldNeuronNumber*neurons.count//total number of connections in layer
let neuralNetworkLayer = NeuralNetworkLayer(newNeurons: neurons, oldNeurons: [], connectionStrengths: [Float](repeating: 1.0, count: connectionsCount))
return neuralNetworkLayer
}
func startProccessWithPrevious(_ neurons: [Float]){
oldNeurons = neurons
print("Starting with:")
print(oldNeurons)
chargeNeurons()
fireNeurons()
print("Current connection strengths: \(connectionStrengths)")
}
func wasRight(_ wasRight: Bool){/*
for connectionStrengthEn in connectionStrengths.enumerated() {
var connectionStrength = connectionStrengths[connectionStrengthEn.offset]
var connectionVariation = lastConnectionVariation[connectionStrengthEn.offset]
if wasRight{
connectionStrength += (connectionStrength / connectionVariation)*2
}else{
connectionStrength -= (connectionStrength / connectionVariation)*2
}
}
print("Updated connection strenghts: \(connectionStrengths)")
*/
guard !wasRight else {return}
for connectionStrength in connectionStrengths.enumerated() {
guard connectionStrength.offset > lastConnectionVariation.count && !lastConnectionVariation.isEmpty else {continue}
let connectionVariation = lastConnectionVariation[connectionStrength.offset] * 0.9
connectionStrengths[connectionStrength.offset] -= connectionStrength.element/connectionVariation
//resetted nearly
}
randomVariation()
}
private func chargeNeurons(){
for neuronEn in newNeurons.enumerated() {
let neuron = neuronEn.element
for connection in connectionStrengths.enumerated(){
let connectionIsIt = ((connection.offset+2) % (neuronEn.offset+2)) == 0
//neuron.precharchedConnections.append((oldNeurons[Int(neuronEn.offset / newNeurons.count)]) * connection.element)
if connectionIsIt{
print("Charging connection \(connection)")
let corrispondingOldNeuronNumer = ceil(Float(connection.offset + 1)/Float(newNeurons.count))
let oldNeuronNumber = oldNeurons[Int(corrispondingOldNeuronNumer)-1]
neuron.precharchedConnections.append(connection.element*oldNeuronNumber)
}
newNeurons[neuronEn.offset] = neuron
}
}
}
private func fireNeurons(){
for neuron in newNeurons {
_ = neuron.fire()
}
}
private func randomVariation(){
lastConnectionVariation = []
for connectionStrength in connectionStrengths.enumerated() {
var variation = Float.random(in: 0...0.1)
let variation2 = Float.random(in: 0...0.1)
let shallBeNegativ = Bool.random()
variation = variation + variation2
if shallBeNegativ{
variation = -variation
print("madeVariationNegative")
}
connectionStrengths[connectionStrength.offset] += variation
lastConnectionVariation.append(variation)
}
}
init(newNeurons: [Neuron], oldNeurons: [Float], connectionStrengths: [Float]) {
self.newNeurons = newNeurons
self.oldNeurons = oldNeurons
self.connectionStrengths = connectionStrengths
}
}