Autoencoder help needed- Real to complex conversion and multiplication with rayleigh fading on one-hot encoded vector

Viewed 59

I'm trying to build a code for designing autoencoder with but facing problems with converting real to complex and performing multiplication/ addition of rayleigh fading and white gaussian noise in the non-trainable intermediate layer. Details are as follows;

In the encoder part of autoencoder, the input layer takes one-hot encoded matrix of size 16x1 or 64x1, which goes through a couple of dense layers and the output of encoder part is intended to be converted to complex (real+imag parts) and then normalized and sent to intermediate layer.

The intermediate layer is intended to be non-trainable. In this layer, I intend to multiply the output of encoder part with a complex function (rayleigh fading), followed by addition of white gaussian noise (complex) is performed over it, which is complex (real+imaginary). After this the output of this part goes to decoder part.

In the decoder part, the first layer converts complex back into real equivalent and then a couple of dense layer are put followed by the softmax.

I have tried multiple ways of doing the above but the am facing errors with training, which doesn't start at all. Also I need to confirm if I'm doing things the right way in converting real to complex and back, and multiplication with rayleigh/ addition with gaussian noise. Code is given here for reference.

import keras
from keras.layers import Input, Dense, GaussianNoise,Lambda,Dropout, Concatenate
from keras.models import Model
import numpy as np
from numpy import sum, isrealobj, sqrt
from keras import regularizers
from numpy.random import standard_normal
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.optimizers import Adam,SGD
from keras import backend as K
import matplotlib.pyplot as plt


# defining parameters
M = 64 # M= Number of messages to encode (Here taking 64 for 64QAM)
k = np.log2(M)
k = int(k)
n_channel = 7
R = k/n_channel
print ('M:',M,'   k:',k, '   n_channel:',n_channel,'   R', R)
EbNo=10.0**(15/10.0)
noise_std = np.sqrt(1/(2*R*EbNo)) #(Beta=(2*R*EbNo)^-1)


#generating data of size N
N = 40000
label = np.random.randint(M,size=N)


#creating one hot encoded vectors
data = []
for i in label:
    temp = np.zeros(M)
    temp[i] = 1
    data.append(temp)


data = np.array(data)
print (data.shape)


#To check randomly generated data
data_check = [28,1608,2730,3978,4620,7018,12359,17334,19173]
for i in data_check:
  print(label[i],data[i])


#defining real to complex and back.
def real_to_complex(x):
    real = x[:,0]
    imag = x[:,1]
    return tf.reshape(tf.dtypes.complex(real,imag),shape=[-1,7])

def complex_to_real(x):
    real = tf.math.real(x)
    imag = tf.math.imag(tf.dtypes.cast(x,tf.complex64))
    real_expand = tf.expand_dims(real,-1)
    imag_expand = tf.expand_dims(imag,-1)
    concated = tf.concat([real_expand, imag_expand],-1)
    return tf.reshape(concated,shape=[-1,7])


# Create random Complex Channel
h_real = 1/np.sqrt(2)*K.random_normal((n_channel,),mean=0,stddev=1)
h_imag = 1/np.sqrt(2)*K.random_normal((n_channel,),mean=0,stddev=1)
h = tf.dtypes.complex(h_real,h_imag)
 
 # Create random Complex Gaussian Noise
noise_real = 1/np.sqrt(2)*K.random_normal((n_channel,),mean=0,stddev=noise_std)
noise_imag = 1/np.sqrt(2)*K.random_normal((n_channel,),mean=0,stddev=noise_std)
noise = tf.dtypes.complex(noise_real,noise_imag)



# Autoencoder structure

###Encoder###
#R = k/7
#n_channel = 7
print (int(k/R))
input_data = Input(shape=(M,))
encoded = Dense(M, activation='relu')(input_data)
encoded1 = Dense(2*n_channel, activation='linear')(encoded)
encoded2 = BatchNormalization()(encoded1)

###Intermediate layer###
EbNo=10.0**(15/10.0)
channel_in = Lambda(real_to_complex)(encoded2)
channel = (channel_in)*(h) + (noise)
#channel = tf.multiply(channel_in, h) + (noise)
#channel1 = tf.multiply(channel, noise)
channel_out = Lambda(complex_to_real)(channel)

###Decoder###
decoded = Dense(M, activation='linear')(channel_out)
decoded1 = Dense(M, activation='relu')(decoded)
decoded2 = Dense(M, activation='softmax')(decoded1)

autoencoder = Model(input_data, decoded2)
#rmsprop = RMSprop(learning_rate=0.001)
sgd = SGD(learning_rate=0.02)
autoencoder.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])


print (autoencoder.summary())


# traning auto encoder
autoencoder.fit(data, data, epochs=100, batch_size=399)```


**Error:**
InvalidArgumentError                      Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9724/4154741599.py in <module>
      1 # traning auto encoder
----> 2 autoencoder.fit(data, data, epochs=100, batch_size=399)

C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     52   try:
     53     ctx.ensure_initialized()
---> 54     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     55                                         inputs, attrs, num_outputs)
     56   except core._NotOkStatusException as e:

InvalidArgumentError: Graph execution error:
Node: 'categorical_crossentropy/softmax_cross_entropy_with_logits'
logits and labels must be broadcastable: logits_size=[114,64] labels_size=[399,64]
     [[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_27526]
0 Answers
Related