使用Tensor Flow自定义Hidden Layer

本文介绍了如何在Keras中使用LambdaLayer和自定义Layer类来构建神经网络模型。LambdaLayer允许您直接使用lambda函数或定义简单的激活函数,而自定义Layer类则涉及更多的初始化和权重管理。通过示例展示了如何定义和应用这些自定义层,并通过训练模型和进行预测来验证其功能。
摘要由CSDN通过智能技术生成

文章目录

Lambda Layer

使用keras.layers提供的Lambda layer API

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

如下:


model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128),
  tf.keras.layers.Lambda(lambda x: tf.abs(x)), 
  tf.keras.layers.Dense(10, activation='softmax')
])

也可以不使用lambda表达式:

def my_relu(x):
	return K.maxium(0,x)
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128),
  tf.keras.layers.Lambda(my_relu), 
  tf.keras.layers.Dense(10, activation='softmax')
])

上述定义的Layer都是untrainable,他们不具备权重。
一般layer具有如下结构:
在这里插入图片描述

定义类

我们还可以从基类 Layer总继承,从而创建一个新的类。

# inherit from this base class
from tensorflow.keras.layers import Layer

class SimpleDense(Layer):

    def __init__(self, units=32, activation=None):
        '''Initializes the instance attributes'''
        super(SimpleDense, self).__init__()
        self.units = units
        self.activation = tf.keras.activations.get(activation)

    def build(self, input_shape):
        '''Create the state of the layer (weights)'''
        # initialize the weights
        w_init = tf.random_normal_initializer()
        self.w = tf.Variable(name="kernel",
            initial_value=w_init(shape=(input_shape[-1], self.units),
                                 dtype='float32'),
            trainable=True)

        # initialize the biases
        b_init = tf.zeros_initializer()
        self.b = tf.Variable(name="bias",
            initial_value=b_init(shape=(self.units,), dtype='float32'),
            trainable=True)

    def call(self, inputs):
        '''Defines the computation from inputs to outputs'''
        return  self.activation(tf.matmul(inputs, self.w) + self.b)

我们并不需要在定义的Layer中进行任何back-propagation的操作,只需要指定forward-propagation就行。


# define the dataset
xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)


# use the Sequential API to build a model with our custom layer
my_layer = SimpleDense(units=1)
model = tf.keras.Sequential([my_layer])

# configure and train the model
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(xs, ys, epochs=500,verbose=0)

# perform inference
print(model.predict([10.0]))

# see the updated state of the variables
print(my_layer.variables)
I am an AI language model and cannot create images directly. However, I can describe the structure of the DeepNeuralNet class in a text format, which you can use as a guide to drawing the network structure. The structure looks like this: 1. Input Layer: This is where the network receives user and item inputs. Each input goes through an embedding layer, with n_users and n_items as the number of embeddings, and n_factors as the size of the embeddings. 2. Concatenation Layer: The output of the user and item embedding layers is concatenated, resulting in a tensor of shape (batch_size, n_factors*2). 3. Fully Connected Hidden Layers: The concatenated tensor is then passed through a series of fully connected layers. In your case, you have two hidden layers of sizes 64 and 32. Each layer is defined as a Linear layer with a specified number of input and output features, and these layers are stored in a ModuleList (fc_layers). 4. Dropout Layer: After passing through the hidden layers, the network goes through a dropout layer with probability 0.2. This randomly sets some elements to zero during training to prevent overfitting. 5. Output Layer: After the dropout layer, the network passes through another Linear layer, which reduces the tensor's dimension to 1. 6. Sigmoid Activation: Finally, the output goes through a sigmoid activation function, which squashes the output value between 0 and 1. The sigmoid activation is applied to make the output ready for predicting ratings or binary outcomes such as preferences. To draw the structure, you can use rectangles to represent the Linear layers and circles for activation functions. Label the rectangles with the number of input and output features, and label the circles with the activation function's name. Connect the rectangles with lines to visualize the information flow.用图展示这个网络层·
最新发布
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东风中的蒟蒻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值