forked from TianLin0509/BF-design-with-DL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_v2.py
62 lines (58 loc) · 2.79 KB
/
train_v2.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
import numpy as np
from utils2 import *
from tensorflow.python.keras.layers import *
# tf.config.experimental_run_functions_eagerly(True)
# ------------------------------------------
# Load and generate simulation data
# ------------------------------------------
path = 'train_set/example/train' # the path of the dictionary containing pcsi.mat and ecsi.mat
# Noticed that this is only a default path containing few samples to test if the program can run successfully
# you can download provided train sets or trained weights from the given google driver in readme
H, H_est = mat_load(path)
# use the estimated csi as the input of the BFNN
H_input = np.expand_dims(np.concatenate([np.real(H_est), np.imag(H_est)], 1), 1)
# H denotes the perfect csi
H = np.squeeze(H)
# generate SNRs associated with different samples
SNR = np.power(10, np.random.randint(-20, 20, [H.shape[0], 1]) / 10)
# -----------------------
# Construct the BFNN Model
# -----------------------
# imperfect CSI is used to output the vrf
imperfect_CSI = Input(name='imperfect_CSI', shape=(H_input.shape[1:4]), dtype=tf.float32)
# perfect_CSI is only used to compute the loss, and not required in prediction
perfect_CSI = Input(name='perfect_CSI', shape=(H.shape[1],), dtype=tf.complex64)
# the SNR is also fed into the BFNN
SNR_input = Input(name='SNR_input', shape=(1,), dtype=tf.float32)
temp = BatchNormalization()(imperfect_CSI)
temp = Flatten()(temp)
temp = BatchNormalization()(temp)
temp = Dense(256, activation='relu')(temp)
temp = BatchNormalization()(temp)
temp = Dense(128, activation='relu')(temp)
phase = Dense(Nt)(temp)
V_RF = Lambda(trans_Vrf, dtype=tf.complex64, output_shape=(Nt,))(phase)
rate = Lambda(Rate_func, dtype=tf.float32, output_shape=(1,))([perfect_CSI, V_RF, SNR_input])
model = Model(inputs=[imperfect_CSI, perfect_CSI, SNR_input], outputs=rate)
# the y_pred is the actual rate, thus the loss is y_pred, without labels
model.compile(optimizer='adam', loss=lambda y_true, y_pred: y_pred)
model.summary()
# -----------------------
# Train Your Model
# -----------------------
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=20, min_lr=0.00005)
checkpoint = tf.keras.callbacks.ModelCheckpoint('./temp_trained.h5', monitor='val_loss',
verbose=0, save_best_only=True, mode='min', save_weights_only=True)
model.fit(x=[H_input, H, SNR], y=H, batch_size=256,
epochs=50000, verbose=2, validation_split=0.1, callbacks=[reduce_lr, checkpoint])
# -----------------------
# Test Your Model
# -----------------------
rate = []
# model.load_weights('./0db.h5')
for snr in range(-20, 25, 5):
SNR = np.power(10, np.ones([H.shape[0], 1]) * snr / 10)
y = model.evaluate(x=[H_input, H, SNR], y=H, batch_size=10000)
print(snr, y)
rate.append(-y)
print(rate)