-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
218 lines (160 loc) · 7.19 KB
/
model.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from numpy.matrixlib.defmatrix import matrix
from syngular.tensor import MatrixProductOperator, MatrixProductState, matrix_product_operator
import numpy as np
from opt_einsum import contract
class Model:
def __init__(self, layers):
self.layers = layers
def predict(self, inputs):
values = inputs
for layer in self.layers:
values = layer(values)
# print("Values shape", values._shape)
return values
def feed_forward(self, sample):
values = sample
for layer in self.layers:
values = layer(values)
return values
def build(self):
for layer in self.layers:
if not layer.is_built:
layer.build(None)
layer.is_built = True
def train(self, x, y, batchsize=32, epochs=1, verbose=1):
if verbose: print("[START] Training ")
for e in range(epochs):
if verbose: print(f"Epoch {str(e+1)} : ")
for layer in self.layers:
for weight in layer.trainable_tensor_weights:
# print(weight)
weight["weight"] += MatrixProductOperator.random((2,2), (2,2), (4,))
# print(weight["weight"])
if verbose: print("[END] Training ")
def draw(self):
repr = ''
for layer in self.layers:
repr += layer.draw()
return repr
def describe(self):
strict_layer = ['output']
params = 0
params_fictive = 0
layers_num = 0
for layer in self.layers:
info = layer.describe()
layers_num += 1
params += info['param']
params_fictive += info['fictive-param']
print('---------------------------------------------------------------------------------------------------------------')
if info["type"] not in strict_layer:
print(
f"| Layer [{info['type']}] -> {info['name']} \t " + \
f"Parameters: {info['param']} \t " + \
f"Saved: {info['fictive-param']-info['param']} " + \
f"(compression {round((info['fictive-param']-info['param'])/info['fictive-param'], 4)*100}%)"
)
else:
print(f"| Layer [{info['type']}] -> {info['name']} \t ")
print('---------------------------------------------------------------------------------------------------------------')
print('===============================================================================================================')
print(f"| \t Total number of layers : {layers_num}")
print(f"| \t Total number of parameters : {params} (v.s {params_fictive})")
print(f"| \t Total compression factor : {round((params_fictive-params)/params_fictive, 4)*100}%")
print('===============================================================================================================')
class Layer:
def __init__(self, name):
self.name = name
self.trainable_tensor_weights = []
self.trainable_tensor_bias = []
self.is_built = False
def __call__(self, inputs):
input_shape = inputs.shape
if not self.is_built:
self.build(input_shape)
self.is_built = True
else:
pass #print("Built")
def build(self, input_shape):
pass
def draw(self):
repr = ''
for weight in self.trainable_tensor_weights:
mp = weight["weight"]
repr += "\t"+"| " * mp.sites_number + "\n"
repr += "\t"+("O---" * (mp.sites_number-1)) + "O" + "\n"
repr += "\t"+"| " * mp.sites_number + "\n"
return repr
def describe(self):
description = {}
total_parameters = 0
total_true_parameters = 0
for w in self.trainable_tensor_weights:
total_parameters += w['weight'].parameters_number
total_true_parameters += w['weight'].real_parameters_number
for b in self.trainable_tensor_bias:
total_parameters += b['bias'].parameters_number
total_true_parameters += b['bias'].real_parameters_number
description['param'] = total_parameters
description['fictive-param'] = total_true_parameters
description['name'] = self.name
description['type'] = self.type
return description
def add_weight(self, input_shape, output_shape, bond_shape, name=None, initializer="normal"):
if name == None:
name = f'weight_{np.random.randint(0,999999)}'
# if initializer == "normal":
# weight = np.random.normal(size=(*self._input_shape, *self._output_shape))
# else:
# weight = np.zeros(shape=(*self._input_shape, *self._output_shape))
# matrix_product_weight = MatrixProductOperator(weight, bond_shape=bond)
# matrix_product_weight.decompose()
matrix_product_weight = MatrixProductOperator.random(input_shape, output_shape, bond_shape)
self.trainable_tensor_weights.append({'name': name, 'weight': matrix_product_weight})
def add_bias(self, size, name=None, initializer="normal"):
# if name == None:
# name = f'bias_{np.random.randint(0,999999)}'
# if initializer == "normal":
# bias = np.random.normal(size=size)
# else:
# bias = np.zeros(shape=size)
# self.trainable_tensor_bias.append({'name': name, 'bias': bias})
pass
class Linear(Layer):
def __init__(self,
input_units, output_units,
core=1, bond=None,
bias_initializer="normal",
weights_initializer="normal",
activation="relu",
name="linear"):
super(Linear, self).__init__(name)
self.input_units = input_units
self.output_units = output_units
self.core = core
self.bond_dimension = bond
self.input_core_dim = round(self.input_units**(1/self.core))
self.output_core_dim = round(self.output_units**(1/self.core))
self._input_shape = (self.input_core_dim,) * self.core
self._output_shape = (self.output_core_dim,) * self.core
self._bond_shape = (self.bond_dimension,) * (self.core-1)
self.bias_initializer = bias_initializer
self.weights_initializer = weights_initializer
self.activation = activation
self.type = 'linear'
def build(self, input_shape):
if self.weights_initializer == "normal":
self.add_weight(self._input_shape, self._output_shape, bond_shape=self._bond_shape, name="weight", initializer="normal")
else:
self.trainable_tensor_weights.append({'name': '', 'weight': self.weights_initializer})
def __call__(self, inputs):
super(Linear, self).__call__(inputs)
weight = self.trainable_tensor_weights[0]["weight"]
return weight @ inputs
class Output(Layer):
def __init__(self, output_shape, name = "output"):
super(Output, self).__init__(name)
self.output_shape = output_shape
self.type = 'output'
def __call__(self, inputs):
return inputs.to_tensor().reshape(self.output_shape)