-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (111 loc) · 5.73 KB
/
main.py
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
import HopfieldNetwork
import PatternManager
from HopfieldUtils import *
import numpy as np
np.set_printoptions(precision=2)
np.set_printoptions(suppress=True)
N = 64
numPatternsByTask = [20]
numPatternsByTask.extend([1 for _ in range(3)])
# HYPERPARAMS ---------------------------------------------------------------------------------------------------------
# Pattern generation params ---------------------------------------------------
mappingFunction = HopfieldNetwork.UpdateRule.ActivationFunction.BipolarHeaviside()
patternManager = PatternManager.SequentialLearningPatternManager(
N, mappingFunction)
# Network params---------------------------------------------------------------
energyFunction = HopfieldNetwork.EnergyFunction.BipolarEnergyFunction()
activationFunction = HopfieldNetwork.UpdateRule.ActivationFunction.BipolarHeaviside()
updateRule = HopfieldNetwork.UpdateRule.AsynchronousPermutation(activationFunction, energyFunction)
EPOCHS = 1000
TEMPERATURE = 1000
DECAY_RATE = np.round((1) * (TEMPERATURE/EPOCHS), 3)
# learningRule = HopfieldNetwork.LearningRule.Delta(EPOCHS, trainUntilStable=False)
# learningRule = HopfieldNetwork.LearningRule.EnergyDirectedDelta(EPOCHS, trainUntilStable=False, alpha=0.)
learningRule = HopfieldNetwork.LearningRule.EnergyDirectedDeltaEWC(EPOCHS, trainUntilStable=False, alpha=0.7,
ewcTermGenerator=HopfieldNetwork.LearningRule.EWCTerm.SignCounterTerm(), ewcLambda=0.4,
useOnlyFirstEWCTerm=True, vanillaEpochsFactor=0.0)
# learningRule = HopfieldNetwork.LearningRule.ElasticWeightConsolidationThermalDelta(
# maxEpochs=EPOCHS, temperature=TEMPERATURE, temperatureDecay=0.0*DECAY_RATE,
# ewcTermGenerator=HopfieldNetwork.LearningRule.EWCTerm.WeightDecayTerm(), ewcLambda=0.01,
# useOnlyFirstEWCTerm=True, vanillaEpochsFactor=0.8)
# Network noise/error params --------------------------------------------------
allowableLearningStateError = 0.02
inputNoise = None
heteroassociativeNoiseRatio = 0.0
# SETUP ---------------------------------------------------------------------------------------------------------------
# Create network
network = HopfieldNetwork.GeneralHopfieldNetwork(
N=N,
energyFunction=energyFunction,
activationFunction=activationFunction,
updateRule=updateRule,
learningRule=learningRule,
allowableLearningStateError=allowableLearningStateError,
patternManager=patternManager,
weights=np.random.normal(size=(N, N))
)
tasks = patternManager.createTasks(
numPatternsByTask=numPatternsByTask
)
# We have currently seen no patterns
seenPatterns = []
# We declare an empty matrix of stabilities
# First index is epoch (currently 0) second is task index
taskPatternStabilities = np.empty(shape=(0, len(tasks)))
# And we track stability over epochs
numStableOverEpochs = []
# Print network details
print(network.getNetworkDescriptionString())
print()
# TRAINING ------------------------------------------------------------------------------------------------------------
for task in tasks:
seenPatterns.extend(task.getTaskPatterns())
print(f"{task}")
# print(f"Task Patterns:")
# for pattern in task.getTaskPatterns():
# print(pattern)
# This task has started, note this
task.startEpoch = network.epochs
# Learn the patterns
accuracies, numStable = network.learnPatterns(
patterns=task.taskPatterns,
allTaskPatterns=patternManager.allTaskPatterns,
heteroassociativeNoiseRatio=heteroassociativeNoiseRatio,
inputNoise=inputNoise
)
# print(f"Network Weights:\n{network.weights}")
taskPatternStabilities = np.vstack(
[taskPatternStabilities, accuracies.copy()])
numStableOverEpochs.extend(numStable)
print(f"Most Recent Epoch Stable States: {numStable[-1]}")
print()
# GRAPHING ------------------------------------------------------------------------------------------------------------
titleBasis = f"{network.N} Neuron, {network.learningRule}\n{network.allowableLearningStateError} Allowable Stability Error\n{heteroassociativeNoiseRatio} Heteroassociative Noise"
fileNameBasis = f"{network.N}Bipolar-{network.learningRule.infoString()}-{network.allowableLearningStateError}AllowableStabilityError-{heteroassociativeNoiseRatio}HeteroassociativeNoise"
taskEpochBoundaries = [task.startEpoch for task in tasks]
# plotSingleTaskStability(taskPatternStabilities[:, 0]*(len(tasks[0].taskPatterns)), taskEpochBoundaries[0],
# title=f"{titleBasis}\n Stability of First Task",
# legend=[str(tasks[0])], figsize=(12,6),
# fileName=f"graphs/{fileNameBasis}--StabilityOfTask0.png"
# )
plotTaskPatternStability(taskPatternStabilities, taskEpochBoundaries=taskEpochBoundaries, plotAverage=False,
title=f"{titleBasis}\n Stability by Task",
legend=[str(task) for task in tasks], figsize=(12, 6),
# fileName=f"graphs/{fileNameBasis}--StabilityByTask.png"
)
# plotTotalStablePatterns(numStableOverEpochs,
# title=f"{titleBasis}\n Total Stable States",
# figsize=(12,6),
# fileName=f"graphs/{fileNameBasis}--TotalStablePatterns.png"
# )
# saveDataAsJSON(f"data/{fileNameBasis}.json",
# networkDescription=network.getNetworkDescriptionJSON(),
# trainingInformation={
# "inputNoise": inputNoise,
# "heteroassociativeNoiseRatio": heteroassociativeNoiseRatio
# },
# taskPatternStabilities=taskPatternStabilities.tolist(),
# taskEpochBoundaries=taskEpochBoundaries,
# numStableOverEpochs=numStableOverEpochs,
# weights=network.weights.tolist(),
# tasks=[np.array(task.taskPatterns).tolist() for task in patternManager.taskPatternManagers])