-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprueba_test.gd
178 lines (176 loc) · 5.79 KB
/
prueba_test.gd
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
extends Control
#
#class NeuralNetwork:
#const LEARNING_RATE = 0.5
#
#var num_inputs: int
#var hidden_layer: NeuronLayer
#var output_layer: NeuronLayer
#
#func _init(num_inputs: int, num_hidden: int, num_outputs: int, hidden_layer_weights = null, hidden_layer_bias = null, output_layer_weights = null, output_layer_bias = null):
#self.num_inputs = num_inputs
#hidden_layer_bias = hidden_layer_bias if hidden_layer_bias else randf()
#output_layer_bias = output_layer_bias if output_layer_bias else randf()
#
#self.hidden_layer = NeuronLayer.new(num_hidden, hidden_layer_bias)
#self.output_layer = NeuronLayer.new(num_outputs, output_layer_bias)
#
#_init_weights(hidden_layer_weights, output_layer_weights)
#
#func _init_weights(hidden_layer_weights, output_layer_weights):
#var weight_num = 0
#
## Initialize hidden layer weights
#for h in range(hidden_layer.neurons.size()):
#for _i in range(self.num_inputs):
#if hidden_layer_weights == null:
#hidden_layer.neurons[h].weights.append(randf())
#else:
#hidden_layer.neurons[h].weights.append(hidden_layer_weights[weight_num])
#weight_num += 1
#
## Initialize output layer weights
#weight_num = 0
#for o in range(output_layer.neurons.size()):
#for _h in range(hidden_layer.neurons.size()):
#if output_layer_weights == null:
#output_layer.neurons[o].weights.append(randf())
#else:
#output_layer.neurons[o].weights.append(output_layer_weights[weight_num])
#weight_num += 1
#
#func feed_forward(inputs: Array) -> Array:
#var hidden_outputs = hidden_layer.feed_forward(inputs)
#return output_layer.feed_forward(hidden_outputs)
#
#func train(training_inputs: Array, training_outputs: Array):
#feed_forward(training_inputs)
#
## 1. Output neuron deltas
#var pd_errors_output = []
#for o in range(output_layer.neurons.size()):
#var neuron = output_layer.neurons[o]
#pd_errors_output.append(neuron.calculate_pd_error_wrt_total_net_input(training_outputs[o]))
#
## 2. Hidden neuron deltas
#var pd_errors_hidden = []
#for h in range(hidden_layer.neurons.size()):
#var error_wrt_output = 0.0
#for o in range(output_layer.neurons.size()):
#error_wrt_output += pd_errors_output[o] * output_layer.neurons[o].weights[h]
#pd_errors_hidden.append(error_wrt_output * hidden_layer.neurons[h].calculate_pd_total_net_input_wrt_input())
#
## 3. Update weights in output layer
#for o in range(output_layer.neurons.size()):
#var neuron = output_layer.neurons[o]
#for w_ho in range(neuron.weights.size()):
#var pd_error_wrt_weight = pd_errors_output[o] * neuron.inputs[w_ho]
#neuron.weights[w_ho] -= LEARNING_RATE * pd_error_wrt_weight
#
## 4. Update weights in hidden layer
#for h in range(hidden_layer.neurons.size()):
#var neuron = hidden_layer.neurons[h]
#for w_ih in range(neuron.weights.size()):
#var pd_error_wrt_weight = pd_errors_hidden[h] * neuron.inputs[w_ih]
#neuron.weights[w_ih] -= LEARNING_RATE * pd_error_wrt_weight
#
#func calculate_total_error(training_sets: Array) -> float:
#var total_error = 0.0
#for set in training_sets:
#var training_inputs = set[0]
#var training_outputs = set[1]
#feed_forward(training_inputs)
#for o in range(output_layer.neurons.size()):
#total_error += output_layer.neurons[o].calculate_error(training_outputs[o])
#return total_error
#
#
#class NeuronLayer:
#var neurons: Array
#var bias: float
#
#func _init(num_neurons: int, bias: float):
#self.bias = bias
#for _i in range(num_neurons):
#neurons.append(Neuron.new(self.bias))
#
#func feed_forward(inputs: Array) -> Array:
#var outputs = []
#for neuron in neurons:
#outputs.append(neuron.calculate_output(inputs))
#return outputs
#
#class Neuron:
#var weights: Array
#var inputs: Array
#var output: float
#var bias: float
#
#func _init(bias: float):
#self.bias = bias
#self.weights = []
#
#func calculate_output(inputs: Array) -> float:
#self.inputs = inputs
#self.output = _sigmoid(_calculate_total_net_input())
#return self.output
#
#func _calculate_total_net_input() -> float:
#var total = 0.0
#for i in range(inputs.size()):
#total += inputs[i] * weights[i]
#return total + bias
#
#func _sigmoid(x: float) -> float:
#return 1.0 / (1.0 + exp(-x))
#
#func _sigmoid_derivative(output: float) -> float:
#return output * (1.0 - output)
#
#func calculate_pd_error_wrt_total_net_input(target_output: float) -> float:
#return calculate_pd_error_wrt_output(target_output) * _sigmoid_derivative(output)
#
#func calculate_error(target_output: float) -> float:
#return 0.5 * pow(target_output - output, 2)
#
#func calculate_pd_error_wrt_output(target_output: float) -> float:
#return -(target_output - output)
#
## Missing method: Add this function
#func calculate_pd_total_net_input_wrt_input() -> float:
#return _sigmoid_derivative(output) # output * (1 - output)
#
#
## Example usage in Godot:
#
#func _ready():
#var nn = NeuralNetwork.new(2, 2, 2, [0.15, 0.2, 0.25, 0.3], 0.35, [0.4, 0.45, 0.5, 0.55], 0.6)
#for i in range(100):
#nn.train([0.05, 0.1], [0.01, 0.99])
#print("Iteration ", i, ", Total Error: ", nn.calculate_total_error([[[0.05, 0.1], [0.01, 0.99]]]))
#
## XOR example:
#
#var training_sets = [
#[[0, 0], [0]],
#[[0, 1], [1]],
#[[1, 0], [1]],
#[[1, 1], [0]]
#]
#var nl = NeuralNetwork.new(2, 8, 1)
#
#for i in range(4000):
#var training_data = training_sets.pick_random()
#var inputs = training_data[0]
#var targets = training_data[1]
#nl.train(inputs, targets)
#if i % 1000 == 0:
#print("Iteration %d, Error: %.9f" % [i, nl.calculate_total_error(training_sets)])
#
## Test the neural network
#print("Testing trained network:")
#for set in training_sets:
#var output = nl.feed_forward(set[0])
#print("Input: %s, Predicted Output: %.5f, Target: %s" % [set[0], output[0], set[1]])
#
#