-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_LOS.py
70 lines (62 loc) · 2.51 KB
/
main_LOS.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
'''
This is the main script implementing the codebook learning solution. It could be used for
both LOS and NLOS situations. The difference is basically in the training and testing
data.
INPUTS: A data preparing function should be added to load and pre-process the training
and testing data. The processed data should take the following format:
- train_inp: is a numpy array of input channel vectors for training.
Size is (# of training samples X # of antennas).
- train_out: is a numpy vector with EGC targets. Size is (# of training samples,)
- val_inp: is a numpy array of input channel vectors for testing.
Size is (# of testing samples X # of antennas).
- val_out: is a numpy vector with EGC targets. Size is (# of testing samples,).
OUTPUTS:
The codebook is saved at the end of the file
Author: Muhammad Alrabeiah
Sept. 2019
'''
import numpy as np
from keras import optimizers
from keras.models import Input, Model
from complex_fc import CompFC
from auxiliary import PowerPooling
import scipy.io as scio
from DataPrep import dataPrep # Example of data preparing function
num_of_beams = [2, 4, 8, 16, 32, 64, 96, 128]
# Training and testing data:
# --------------------------
batch_size = 500
#-------------------------------------------#
# Here should be the data_preparing function
# It is expected to return:
# train_inp, train_out, val_inp, and val_out
#-------------------------------------------#
len_inp = train_inp.shape[1]
for N in num_of_beams:
print(str(N) + '-beams Codebook')
# Model:
# ------
xBatch = Input(shape=(len_inp,))
fc1 = CompFC(N, seed=None, scale=np.sqrt(64), activation='linear')(xBatch)
max_pooling = PowerPooling(2 * N)(fc1)
model = Model(inputs=xBatch, outputs=max_pooling)
# Training:
# ---------
adam = optimizers.Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0, amsgrad=False)
model.compile(optimizer=adam, loss='mse')
model.fit(train_inp, train_out,
epochs=100,
batch_size=batch_size,
shuffle=True,
validation_data=(val_inp, val_out))
# Extract learned codebook:
# -------------------------
theta = np.array(model.get_weights()[0])
print(theta.shape)
name_of_file = 'theta_NLOS' + str(N) + 'vec.mat'
scio.savemat(name_of_file,
{'train_inp': train_inp,
'train_out': train_out,
'val_inp': val_inp,
'val_out': val_out,
'codebook': theta})