-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestMps.py
More file actions
175 lines (140 loc) · 6.97 KB
/
testMps.py
File metadata and controls
175 lines (140 loc) · 6.97 KB
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
import scipy as sp
import numpy as np
from keras.models import Sequential
import keras
import keras.backend as K
import tensorflow as tf
import copy
import KerasMps
LR = 1
DECAY = 0.01
MOMENTUM = 0.9
OPTIMIZER = keras.optimizers.SGD(lr=LR, decay=DECAY, momentum=MOMENTUM)
def generate_samples(func, matrix_size, samples):
# Initialize source matrix with random elements in the [-1,-1j]X[1,1j]
# square on the complex plane
data = np.vectorize(complex)(
np.random.random((samples,matrix_size,matrix_size)),
np.random.random((samples,matrix_size,matrix_size)))*2-1-1j
# Calculate expected results
labels = []
for datum in data:
labels.append(func(datum))
labels = np.array(labels)
# Seprate into real and complex parts
ridata = np.array(list(map(lambda x: [x.real, x.imag], data)))
rilabels = np.array(list(map(lambda x: [x.real, x.imag], labels)))
return (ridata,rilabels)
# func - the function approximation being tested
# layer - the layer being tested
def generic_test_scalar(func, layer, epochs=100, samples=4000, test_samples=100, matrix_size=6, degree=5, batch_size=256):
# Create training and test datasets
train_data, train_labels = generate_samples(func, matrix_size, samples)
test_data, test_labels = generate_samples(func, matrix_size, test_samples)
# Create model
model = Sequential([
layer(degree=degree, input_shape=(2,matrix_size,matrix_size))
])
model.compile(optimizer=OPTIMIZER,
loss='logcosh')
model.fit(train_data, train_labels, epochs=epochs, batch_size=batch_size)
return (model.evaluate(test_data, test_labels, batch_size=batch_size), model)
zero = lambda x: np.zeros(x.shape)
unit = lambda x: np.identity(x.shape[0])
identity = lambda x: x
square = lambda x: np.matmul(x,x)
cube = lambda x: np.matmul(x,square(x))
expm = sp.linalg.expm
# This function was designed as a test for the M2 variant
# It cannot be approximated by matrix multiplication from only one side
def bold(x):
x = copy.deepcopy(x)
# random numbers
x[0,:] *= 3+1j
x[:,0] *= -2-3j
return x
test_zero = lambda **kwargs: generic_test_scalar(zero, KerasMps.MPS, **kwargs)
test_unit = lambda **kwargs: generic_test_scalar(unit, KerasMps.MPS, **kwargs)
test_id = lambda **kwargs: generic_test_scalar(identity, KerasMps.MPS, **kwargs)
test_square = lambda **kwargs: generic_test_scalar(square, KerasMps.MPS, **kwargs)
test_cube = lambda **kwargs: generic_test_scalar(cube, KerasMps.MPS, **kwargs)
test_exp = lambda **kwargs: generic_test_scalar(expm, KerasMps.MPS, **kwargs)
test_bold = lambda **kwargs: generic_test_scalar(bold, KerasMps.MPS, **kwargs)
test_zero_m = lambda **kwargs: generic_test_scalar(zero, KerasMps.MMPS, **kwargs)
test_unit_m = lambda **kwargs: generic_test_scalar(unit, KerasMps.MMPS, **kwargs)
test_id_m = lambda **kwargs: generic_test_scalar(identity, KerasMps.MMPS, **kwargs)
test_square_m = lambda **kwargs: generic_test_scalar(square, KerasMps.MMPS, **kwargs)
test_cube_m = lambda **kwargs: generic_test_scalar(cube, KerasMps.MMPS, **kwargs)
test_exp_m = lambda **kwargs: generic_test_scalar(expm, KerasMps.MMPS, **kwargs)
test_bold_m = lambda **kwargs: generic_test_scalar(bold, KerasMps.MMPS, **kwargs)
test_zero_m2 = lambda **kwargs: generic_test_scalar(zero, KerasMps.MM2PS, **kwargs)
test_unit_m2 = lambda **kwargs: generic_test_scalar(unit, KerasMps.MM2PS, **kwargs)
test_id_m2 = lambda **kwargs: generic_test_scalar(identity, KerasMps.MM2PS, **kwargs)
test_square_m2 = lambda **kwargs: generic_test_scalar(square, KerasMps.MM2PS, **kwargs)
test_cube_m2 = lambda **kwargs: generic_test_scalar(cube, KerasMps.MM2PS, **kwargs)
test_exp_m2 = lambda **kwargs: generic_test_scalar(expm, KerasMps.MM2PS, **kwargs)
test_bold_m2 = lambda **kwargs: generic_test_scalar(bold, KerasMps.MM2PS, **kwargs)
# convention:
# i is batch size
# j is output channels
# k is input channels
# l is 0/1 - real/complex part
# m,n are elements of the input matrix
# o - degree of polynomial
def multichannel_generate_samples(func, matrix_size, input_channels, samples):
# Initialize source matrix with random elements in the [-1,-1j]X[1,1j]
# square on the complex plane
data = np.vectorize(complex)(
np.random.random((samples, input_channels, matrix_size, matrix_size)),
np.random.random((samples, input_channels, matrix_size, matrix_size)))*2-1-1j
labels = []
for channel in data:
f_input_channels = []
for datum in channel:
f_input_channels.append(func(datum))
labels.append(f_input_channels)
labels = np.array(labels)
ridata = np.einsum('likmn->iklmn', np.array([data.real, data.imag]))
rilabels = np.einsum('likjmn->ijklmn', np.array([labels.real, labels.imag]))
return (ridata,rilabels)
def generic_multichannel_test(func,
layer,
out_channels,
input_channels=5,
epochs=100,
samples=1000,
test_samples=100,
matrix_size=6,
degree=5,
batch_size=64):
train_data, train_labels = multichannel_generate_samples(func, matrix_size, input_channels, samples)
test_data, test_labels = multichannel_generate_samples(func, matrix_size, input_channels, test_samples)
model = Sequential([
layer(
degree=degree,
out_channels=out_channels,
input_shape=(input_channels,2,matrix_size,matrix_size)) # input_shape=[k,l,m,n]
# output_shape: [j,k,l,m,n]
])
model.compile(optimizer=OPTIMIZER,
loss='logcosh')
model.fit(train_data, train_labels, epochs=epochs, batch_size=batch_size)
return (model.evaluate(test_data, test_labels, batch_size=batch_size), model)
zero_id_unit = lambda x: np.array([zero(x),unit(x),identity(x)])
square_exp_cube = lambda x: np.array([square(x),expm(x),cube(x)])
bold_2 = lambda x: np.array([bold(x),bold(x)])
def make_test_multi(func, layer, out_channels, **kwargs):
return lambda **kwargs: generic_multichannel_test(
func=func,
layer=layer,
out_channels=out_channels,
**kwargs)
test_multichannel = make_test_multi(zero_id_unit, KerasMps.MchMPS, 3)
test_multichannel_nonlin = make_test_multi(square_exp_cube, KerasMps.MchMPS, 3)
test_multichannel_bold = make_test_multi(bold_2, KerasMps.MchMPS, 2)
test_multichannel_m = make_test_multi(zero_id_unit, KerasMps.MchMMPS, 3)
test_multichannel_nonlin_m = make_test_multi(square_exp_cube, KerasMps.MchMMPS, 3)
test_multichannel_bold_m = make_test_multi(bold_2, KerasMps.MchMMPS, 2)
test_multichannel_m2 = make_test_multi(zero_id_unit, KerasMps.MchMM2PS, 3)
test_multichannel_nonlin_m2 = make_test_multi(square_exp_cube, KerasMps.MchMM2PS, 3)
test_multichannel_bold_m2 = make_test_multi(bold_2, KerasMps.MchMM2PS, 2)