-
Notifications
You must be signed in to change notification settings - Fork 5
/
OverlapChunkTest1.py
148 lines (109 loc) · 4.41 KB
/
OverlapChunkTest1.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
139
140
141
142
143
144
145
146
147
from keras.utils import to_categorical
import numpy as np
import math
import matplotlib.pyplot as plt
class OverlapChunkTest1:
def __init__(self, time_delay):
self.chunk= 0
self.output_size = 8
self.counter = -1
self.time_delay = time_delay
self.time_counter = time_delay
self.output_class= 0
self.previous_output_class= None
self.sequenceA_length = 4
self.sequenceB_length = 4 #np.random.randint(2)+5
self.previous_previous_output_class= None
def getOutputSize(self):
return self.output_size
def trueLabel(self):
truelabel= np.array((0,0,0,1,1,2,2,2))
return truelabel
def updateTimeDelay(self):
self.time_counter+= 1
if self.time_counter > self.time_delay:
self.time_counter = 0
return True
else:
return False
#create an input pattern for the system
def getInput(self, reset = False):
if reset == True:
self.chunk=0
self.counter=-1
update = self.updateTimeDelay()
if update == True:
if self.chunk == 0:
if self.counter > self.sequenceA_length:
self.chunk = 1
self.counter= 0
else:
self.counter+= 1
else:
if self.counter > self.sequenceB_length:
#self.sequenceB_length = np.random.randint(20)+5
self.chunk = 0
self.counter= 0
else:
self.counter+= 1
if self.chunk == 0:
#input_value = np.random.randint(10)
#input_value= self.counter
self.previous_previous_output_class= self.previous_output_class
self.previous_output_class= self.output_class
#possible outputs are 0,1,2,3,4
self.output_class = np.random.randint(5)
else:
self.previous_previous_output_class= self.previous_output_class
self.previous_output_class= self.output_class
#possible outputs are 3,4,5,6,7
self.output_class = 3 + np.random.randint(5)
noise_intensity= 0.0
#input_value = to_categorical(self.output_class, self.output_size) + np.random.randn(self.output_size)*noise_intensity
if self.previous_output_class is None or self.previous_output_class == self.output_class:
input_value = to_categorical(self.output_class, self.output_size)*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity
else:
input_value = to_categorical(self.output_class, self.output_size)*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity + to_categorical(self.previous_output_class, self.output_size)*np.exp(-0.1*(self.time_counter+self.time_delay))
# if self.previous_output_class is None or np.array_equal(self.previous_output_class, self.output_class):
# input_value = self.output_class*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity
# else:
# if self.previous_previous_output_class is None or np.array_equal(self.previous_previous_output_class, self.previous_output_class):
# input_value = self.output_class*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity + self.previous_output_class*np.exp(-0.1*(self.time_counter+self.time_delay))
# else:
# input_value = self.output_class*np.exp(-0.1*self.time_counter) + np.random.randn(self.output_size)*noise_intensity + self.previous_output_class*np.exp(-0.1*(self.time_counter+self.time_delay)) + self.previous_previous_output_class*np.exp(-0.1*(self.time_counter+2.0*self.time_delay))
return input_value
def getSequence(self, iterations):
input_class = np.empty(iterations)
input_sequence = np.empty((iterations, self.output_size))
for i in range(iterations):
input_value = self.getInput()
#input_class.append(self.chunk)
#input_sequence.append(input_value)
input_class[i] = self.chunk
input_sequence[i] = input_value
return input_sequence, input_class
def plot(self, input_class, input_sequence = None, save = False):
a = np.asarray(input_class)
t = [i for i,value in enumerate(a)]
plt.plot(t, a)
if input_sequence != None:
sequence = [np.argmax(x) for x in input_sequence]
plt.plot(t, sequence)
if save == True:
plt.savefig("plot.png")
plt.show()
plt.close()
def plotSuperposed(self, input_class, input_sequence = None, save = False):
input_sequence= np.asarray(input_sequence)
t = [i for i,value in enumerate(input_sequence)]
print(input_sequence.shape)
#exit()
for i in range(input_sequence.shape[1]):
a = input_sequence[:,i]
plt.plot(t, a)
a = np.asarray(input_class)
plt.plot(t, a)
if save == True:
plt.savefig("plot.png")
plt.show()
plt.close()