十四.自定义层

本文介绍了如何使用TensorFlow和Keras库从头实现一个简单的线性层,包括权重和偏置的初始化,以及前向传播过程。重点展示了如何在不同情况下处理权重初始化,如在知道输入形状和未知输入形状时。此外,还探讨了层的递归组合,如MLPBlocks的实例应用。
摘要由CSDN通过智能技术生成
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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值