-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.lua
More file actions
182 lines (159 loc) · 6.55 KB
/
NeuralNetwork.lua
File metadata and controls
182 lines (159 loc) · 6.55 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
-- 100% made by DeepSeek R1...
-- ...cuz im a skid :3
-- NeuralNetwork.new(inputSize, hiddenLayers, outputSize, options)
-- NeuralNetwork:forward(input)
-- NeuralNetwork:backwardPropagation(target)
-- NeuralNetwork:backwardReinforce(action, reward)
-- getgenv().NeuralNetwork=getgenv().NeuralNetwork or loadstring(game:HttpGet("https://raw.githubusercontent.com/IvanTheProtogen/dumpsterfire/refs/heads/main/NeuralNetwork.lua"))();
NeuralNetwork = {
new = function(inputSize, hiddenLayers, outputSize, options)
local nn = {}
options = options or {}
nn.learningRate = options.learningRate or 0.01
nn.activation = options.activation or function(x) return x end
nn.activationDeriv = options.activationDeriv or function() return 1 end
nn.outputActivation = options.outputActivation or function(x) return x end
nn.outputDeriv = options.outputDeriv or function() return 1 end
local function createMatrix(rows, cols)
local m = {}
for i = 1, rows do
m[i] = {}
for j = 1, cols do
m[i][j] = (math.random() - 0.5) * 0.1
end
end
return m
end
nn.layers = {}
local prevSize = inputSize
for _, size in ipairs(hiddenLayers) do
table.insert(nn.layers, {
weights = createMatrix(size, prevSize),
biases = createMatrix(size, 1)
})
prevSize = size
end
table.insert(nn.layers, {
weights = createMatrix(outputSize, prevSize),
biases = createMatrix(outputSize, 1)
})
function nn:forward(input)
local function matMulVec(m, v)
local result = {}
for i = 1, #m do
local sum = 0
for j = 1, #m[i] do
sum = sum + m[i][j] * v[j]
end
result[i] = sum
end
return result
end
self.activations = {input}
for i, layer in ipairs(self.layers) do
local preact = matMulVec(layer.weights, self.activations[i])
for j = 1, #preact do preact[j] = preact[j] + layer.biases[j][1] end
local activation = {}
if i == #self.layers then
for k = 1, #preact do activation[k] = self.outputActivation(preact[k]) end
else
for k = 1, #preact do activation[k] = self.activation(preact[k]) end
end
table.insert(self.activations, activation)
end
return self.activations[#self.activations]
end
function nn:backwardPropagation(target)
local function transpose(m)
local t = {}
for i = 1, #m[1] do
t[i] = {}
for j = 1, #m do
t[i][j] = m[j][i]
end
end
return t
end
local output = self.activations[#self.activations]
local delta = {}
for i = 1, #output do
delta[i] = (output[i] - target[i]) * self.outputDeriv(output[i])
end
for i = #self.layers, 1, -1 do
local layer = self.layers[i]
local gradWeights = {}
for j = 1, #delta do
gradWeights[j] = {}
for k = 1, #self.activations[i] do
gradWeights[j][k] = delta[j] * self.activations[i][k]
end
end
for j = 1, #layer.weights do
for k = 1, #layer.weights[j] do
layer.weights[j][k] = layer.weights[j][k] - self.learningRate * gradWeights[j][k]
end
layer.biases[j][1] = layer.biases[j][1] - self.learningRate * delta[j]
end
if i > 1 then
local newDelta = {}
local weightsT = transpose(layer.weights)
for j = 1, #weightsT do
newDelta[j] = 0
for k = 1, #weightsT[j] do
newDelta[j] = newDelta[j] + weightsT[j][k] * delta[k]
end
newDelta[j] = newDelta[j] * self.activationDeriv(self.activations[i][j])
end
delta = newDelta
end
end
end
function nn:backwardReinforce(action, reward)
local output = self.activations[#self.activations]
local oneHot = {}
for i = 1, #output do
oneHot[i] = (i == action) and 1 or 0
end
local delta = {}
for i = 1, #output do
delta[i] = (oneHot[i] - output[i]) * reward
end
for i = #self.layers, 1, -1 do
local layer = self.layers[i]
local gradWeights = {}
for j = 1, #delta do
gradWeights[j] = {}
for k = 1, #self.activations[i] do
gradWeights[j][k] = delta[j] * self.activations[i][k]
end
end
for j = 1, #layer.weights do
for k = 1, #layer.weights[j] do
layer.weights[j][k] = layer.weights[j][k] + self.learningRate * gradWeights[j][k]
end
layer.biases[j][1] = layer.biases[j][1] + self.learningRate * delta[j]
end
if i > 1 then
local newDelta = {}
local weightsT = {}
for x = 1, #layer.weights[1] do
weightsT[x] = {}
for y = 1, #layer.weights do
weightsT[x][y] = layer.weights[y][x]
end
end
for j = 1, #weightsT do
newDelta[j] = 0
for k = 1, #weightsT[j] do
newDelta[j] = newDelta[j] + weightsT[j][k] * delta[k]
end
newDelta[j] = newDelta[j] * self.activationDeriv(self.activations[i][j])
end
delta = newDelta
end
end
end
return nn
end
}
return NeuralNetwork