There are many of the class inherits that are allowed for the customizations. One is the Dense and LSTM layer. They are suitable to perform tasks internally and return you specific data as blocks of code.
Sample:
import tensorflow as tf
import matplotlib.pyplot as plt
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Class / Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class My_3D_noises_generator(tf.keras.layers.Layer):
def __init__(self, num_outputs):
super(My_3D_noises_generator, self).__init__()
self.num_outputs = num_outputs
def build(self, input_shape):
self.kernel = self.add_weight("kernel",
shape=[int(input_shape[-1]),
self.num_outputs],
initializer=tf.ones_initializer())
def call(self, inputs):
pi = 3.141592653589793
start = 0.0
stop = 1.0 * 2.0 * pi
x = tf.linspace( start, stop, self.num_outputs, name='linspace', axis=0 )
y1 = 3 * tf.math.sin( x )
escape_sine = tf.random.normal(
shape=( self.num_outputs, ),
mean=0.0,
stddev=0.15 * tf.math.abs( y1, name='abs' ),
dtype=tf.dtypes.float32,
seed=32,
name=None
)
y1 = tf.concat( (tf.zeros(60), y1 + escape_sine, tf.zeros(60)), axis=0, name='concat' )
initial_degree = tf.experimental.numpy.arange( -3, 0, 3 / 60, dtype=tf.float32 )
midring_degree = tf.experimental.numpy.arange( 0, 3 * 2 * pi, ( 3 * 2 * pi) / self.num_outputs, dtype=tf.float32 )
skipped_degree = tf.experimental.numpy.arange( 3 * 2 * pi, 3 * 2 * pi + 3, ( 3 * 2 * pi - 3 * 2 * pi + 3 ) / 60, dtype=tf.float32 )
x = tf.concat(( initial_degree.numpy(), midring_degree.numpy(), skipped_degree.numpy()), axis=0, name='concat')
y2 = 0.1 * x + 1
y = y1 + y2
z = 15 * tf.random.normal(
shape=( 1, self.num_outputs, ),
mean=0.0,
stddev=1,
dtype=tf.dtypes.float32,
seed=32,
name=None
)
x = tf.expand_dims(x, axis=0)
y = tf.expand_dims(y, axis=0)
z = tf.matmul(inputs, z)
x = tf.matmul(inputs, x)
y = tf.matmul(inputs, y)
x = x[int(tf.math.argmax(x, axis=0)[0])]
y = y[int(tf.math.argmax(y, axis=0)[0])]
z = z[int(tf.math.argmax(z, axis=0)[0])]
x = tf.expand_dims(x, axis=-1)
y = tf.expand_dims(y, axis=-1)
z = tf.expand_dims(z, axis=-1)
return x, y, z
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Perform operations
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
start = 3
limit = 33
delta = 3
sample = tf.range(start, limit, delta)
sample = tf.cast( sample, dtype=tf.float32 )
sample = tf.constant( sample, shape=( 10, 1 ) )
layer = My_3D_noises_generator(100)
xdata, ydata, zdata = layer(sample)
ax = plt.axes(projection='3d')
# Data for a three-dimensional line
zline = tf.range(0, 1000, 25)
zline = tf.cast( zline, dtype=tf.float32 )
xline = 20 * tf.math.sin(zline)
yline = 20 * tf.math.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')
ax.scatter3D(xdata[0:100,:], ydata[0:100,:], zdata[0:100,:], c=zdata[0:100,:], cmap='Greens');
plt.show()
Output: Generated random noises in 3D try to catches them.
