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__()
# 权重
# w_init = tf.random_normal_initializer()#标准正态分布初始值
# self.w = tf.Variable(#tensorflow变量封装
# w_init(shape=(input_dim,units),dtype='float32'),
# trainable = True
# )
self.w = self.add_weight(
shape=(input_dim,units),
initializer='random_normal',
trainable = True,
)
# 偏置
# b_init = tf.zeros_initializer()
# self.b = tf.Variable(
# b_init(shape=(units,),dtypeshape=(input_dim,units),dtype='float32'='float32')
# )
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)#[],权重初始化推迟到得知输入的形状之后
#权重初始化推迟到得知输入的形状之后,bulid()
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),#形状最后一个维度,第一维度batch
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)
#tf.Tensor(
#[[0.0017798]
# [0.0017798]], shape=(2, 1), dtype=float32)
十四.自定义层
最新推荐文章于 2024-08-06 19:50:28 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)