I want to add a Custom Layer to a Tensorflow Model which transforms every row of a the input set to the model in a set pattern.
For example, below is the custom layer than I have:
class CustomLayer:
def __init__(self, x, step, mean, std_dev):
self.x = x
self.step = step
self.mean= mean
self.std_dev = std_dev
self.size = len(self.x)
def transform_signal(self, y):
indices_transform = np.arange(0,len(y), self.step)
a1 = y[indices_transform]
a2 = a1[1:]
a1 = a1[:-1]
return np.repeat( (a1 + (a2-a1)/2), self.step )
def gaussian_noise(self):
return np.random.normal(loc=self.mean, scale=self.std_dev, size=self.size)
def transform(self):
x_gauss_noise = self.x + self.gaussian_noise()
return self.transform_signal(y=x_gauss_noise)
And below is a small three row example dataset that I have:
arr = np.expand_dims( np.array([ [1, 5, 7, 8, 10, 11, 11.5, 12, 12.5, 13, 13.2, 13.8, 14.3],
[11, 15, 17, 18, 110, 111, 111.5, 112, 112.5, 113, 113.2, 113.8, 114.3],
[2, 6, 8, 9, 11, 12, 12.5, 13, 13.5, 14, 15.2, 14.8, 15.3]]), axis=2 )
Application of the custom layer to the first row will do a transformation like the one shown in the below picture. Here arr[0,:,0] has length=13, and the transformed array, i.e., arr1 has length=12.
test = CustomLayer(x=arr[0,:,0],
mean=0,
std_dev=0.03,
step=2)
arr1 = test.transform()
It is okay if the values of the parameters step, mean and std_dev are set as 2, 0 and 0.03 respectively. But, I would like the CustomLayer to act independently on each row during its operation in the Tensorflow model.
I would like my Tensorflow model to be like the below. I would also like the input_shape in this model to be (13,1) since the length of each row in the array is 13.
import tensorflow as tf
from tensorflow.keras.layers import (Conv1D,
Dense)
model = tf.keras.Sequential([
CustomLayer(mean=0, std_dev=0.03, step=2),
Conv1D(filters=2,
kernel_size=5,
padding="same",
activation="relu"),
Dense(units=1, activation='relu')
])
How can I use the aforementioned CustomLayer in the model specified above as the initial transformation layer?
At present, the above structure of model gives me an error which says:
TypeError Traceback (most recent call last)
Cell In[94], line 2
1 model = tf.keras.Sequential([
----> 2 CustomLayer(mean=0, std_dev=0.03, step=2),
3 Conv1D(filters=2,
4 kernel_size=5,
5 padding="same",
6 activation="relu"),
7 Dense(units=1, activation='relu')
8 ])
10 model.summary()
TypeError: CustomLayer.__init__() missing 1 required positional argument: 'x'
