Skip to content

Commit 5bb2641

Browse files
author
Guled
committed
Added ability to edit weights and bias values
1 parent 9542ce0 commit 5bb2641

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

Example/MLKit/GameScene.swift

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -303,30 +303,32 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
303303
self.generationLabel.text = "Gen: \(self.generationCounter)"
304304
print("--------------------------- \n")
305305

306-
/* DEBUGGING
307-
if (currentBird?.fitness)! >= Float(7.0) {
308-
print("-----BEST BIRD FOUND------")
309-
310-
currentBird?.brain?.inputLayer.printLayer(layer: (currentBird?.brain?.inputLayer)!)
311-
currentBird?.brain?.hiddenLayer.printLayer(listOfHiddenLayers: (currentBird?.brain?.listOfHiddenLayers)!)
312-
currentBird?.brain?.outputLayer.printLayer(layer: (currentBird?.brain?.outputLayer)!)
313-
314-
}
315-
*/
316-
317306
// Go to next flappy bird
318307
currentFlappy += 1
319308

320309
// Filter out the worst birds after gen 6 (can be adjusted)
310+
311+
if let bird = currentBird {
312+
if bird.fitness >= 9.0 {
313+
print("FOUND RARE BIRD")
314+
print(bird.brain?.layers[0].weights)
315+
print(bird.brain?.layers[1].weights)
316+
print(bird.brain?.layers[0].bias)
317+
print(bird.brain?.layers[1].bias)
318+
}
319+
}
320+
321321
if generationCounter >= 3 {
322322

323323
// Experiment: Keep some of the last best birds and put them back into the population
324324
lastBestGen = (flappyBirdGenerationContainer?.filter({$0.fitness >= 7.0}))!
325325
}
326326

327-
if (currentBird?.fitness)! > maxFitness {
328-
maxFitness = (currentBird?.fitness)!
329-
maxBird = currentBird
327+
if let bird = currentBird {
328+
if bird.fitness > maxFitness {
329+
maxFitness = bird.fitness
330+
maxBird = bird
331+
}
330332
}
331333

332334
// If we have hit the 20th bird, we need to move on to the next generation
@@ -381,18 +383,15 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
381383

382384
// Set the current bird
383385
if (flappyBirdGenerationContainer?.count)! > currentFlappy {
384-
print("OK!")
385386
currentBird = flappyBirdGenerationContainer?[currentFlappy]
386387
} else {
387-
print("NOPE!!!!")
388388
currentBird = maxBird
389389
}
390390

391391
} else {
392392

393393
// Set the current bird
394394
if (flappyBirdGenerationContainer?.count)! > currentFlappy {
395-
print("SUPER OK")
396395
currentBird = flappyBirdGenerationContainer?[currentFlappy]
397396
}
398397

@@ -466,14 +465,6 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
466465

467466
let normalizedPosToGap = (posToGap - (-439))/(279 - (-439))
468467

469-
/*
470-
print("======================== \n")
471-
print(" DIST: \((finalDistanceOfNextPipe))")
472-
print("PIPE POSITION: \(finalPosToGap)")
473-
print("Bird POS Y: \(birdPos)")
474-
print("======================== \n")
475-
*/
476-
477468
// Decision AI makes
478469
let input = Matrix<Float>(rows: 4, columns: 1, elements: [Float(normalizedDistanceOfNextPipe), Float(normalizedPosToGap), Float(birdYPos), Float(normalizedDistanceFromBottomPipe)])
479470
let decision = currentBird?.brain?.feedforward(input: input)

MLKit/Classes/ANN/Layer.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,42 @@ open class Layer {
5555
}
5656

5757

58+
/**
59+
Manipulate the weights values of a particular Layer object.
60+
61+
- parameter newWeights: Weights as a matrix. Shape (rows and columns) must be equivalent to the weights of the Layer object being manipulated.
62+
63+
*/
64+
public func editWeights(newWeights:Matrix<Float>) throws{
65+
66+
if let currentWeights = self.weights {
67+
if newWeights.rows != currentWeights.rows && newWeights.columns != currentWeights.columns {
68+
throw MachineLearningError.invalidInput
69+
}
70+
}
71+
72+
self.weights = newWeights
73+
}
74+
75+
76+
/**
77+
Manipulate the bias values of a particular Layer object.
78+
79+
- parameter newBias: Bias as a matrix. Shape (rows and columns) must be equivalent to the bias of the Layer object being manipulated.
80+
81+
*/
82+
public func editBias(newBias:Matrix<Float>) throws{
83+
84+
if let currentBias = self.bias {
85+
if newBias.rows != currentBias.rows && newBias.columns != currentBias.columns {
86+
throw MachineLearningError.invalidInput
87+
}
88+
}
89+
90+
self.bias = newBias
91+
}
92+
93+
5894
/**
5995
The forward method passes input into the layers neurons and produces an output matrix. The method saves the input into the Layer objects 'input' atrribute. The method also saves the activation and net value of each neuron into the 'activationValues' and 'zValues' attributes.
6096

0 commit comments

Comments
 (0)