import tensorflow as tf
from tensorflow import keras
class Linear(keras.layers.Layer):
def __init__(self,units=32,input_dim=32):
super(Linear,self).__init__()
self.w = self.add_weight(
shape=(input_dim,units),
initializer='random_normal',
trainable = True,
)
self.b = self.add_weight(
shape=(units,),
initializer='zeros'
)
def call(self,input):
return tf.matmul(input,self.w) + self.b
x=tf.ones((2,2))
print(x)
my_Linear = Linear(4,2)
print(my_Linear.weights)
y = my_Linear(x)
print(y)
'''
tf.Tensor(
[[1. 1.]
[1. 1.]], shape=(2, 2), dtype=float32)
[<tf.Variable 'Variable:0' shape=(2, 4) dtype=float32, numpy=
array([[ 0.04358608, 0.00528855, 0.09322696, 0.01686472],
[-0.08990028, 0.0634019 , -0.0152836 , -0.05400625]],
dtype=float32)>, <tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>]
tf.Tensor(
[[-0.04631419 0.06869046 0.07794336 -0.03714152]
[-0.04631419 0.06869046 0.07794336 -0.03714152]], shape=(2, 4), dtype=float32)
'''
x=tf.keras.layers.Dense(64)
print(x.weights)
class Linear(keras.layers.Layer):
def __init__(self,units=32):
super(Linear,self).__init__()
self.units = units
def build(self,input_shape):
self.w = self.add_weight(
shape=(input_shape[-1],self.units),
initializer='random_normal',
trainable = True,
)
self.b = self.add_weight(
shape=(self.units,),
initializer='zeros'
)
def call(self,input):
return tf.matmul(input,self.w) + self.b
my_linear = Linear(4)
my_linear.weights
class MLPBlocks(keras.layers.Layer):
def __init__(self):
super(MLPBlocks,self).__init__()
self.lin1 = Linear(32)
self.lin2 = Linear(64)
self.lin3 = Linear(1)
def call(self,inputs):
x = self.lin1(inputs)
x = tf.nn.relu(x)
x = self.lin2(x)
x = tf.nn.relu(x)
x = self.lin3(x)
return x
x=tf.ones((2,2))
mlp = MLPBlocks()
y = mlp(x)
print(y)
x=tf.ones((2,2))
mlp = MLPBlocks()
y = mlp(x)
print(y)
x=tf.ones((2,2))
mlp = MLPBlocks()
y = mlp(x)
print(y)