要了解这SimpleDense
层并回答您的问题,我们需要解释weight
和bias
.SimpleDense
中的权重首先得到随机数,bias
得到zero
个数字,在模型的训练中,这个权重和偏差会发生变化,以使损失最小化.The answer to First Question:构建方法仅一次性调用,在第一次使用层时,此方法正在调用,权重和偏差设置为随机数和零数,但每个训练批中的调用方法正在调用.The answer to the Second Question:是的,在调用方法中,我们可以访问一批数据,第一个维度显示该批数据.我编写了一个示例,在build
和call
方法调用时打印,并打印输入和输出数据的形状,以澄清上述解释.
在以下示例中:
- 我使用
batch_size = 5
和25 sample data
,在每个时代,我们可以在call method
中看到5 sample data
.
one-time
层创建和构建以及one-time 101 is calling、5 epoch
和5-time 103 is calling.
Units = 4
和shape data = (100, 2) [sample, features]
,然后total params = 12
<-&燃气轮机;4*2 (weights*features) + 4 (bias)
- 添加末端,附加一张显示
matmul
如何工作以及为什么输出形状为(5,4)
的图像,以及计算intput*weight+bias.
的公式
import tensorflow as tf
class SimpleDense(tf.keras.layers.Layer):
def __init__(self, units=32):
super(SimpleDense, self).__init__()
self.units = units
def build(self, input_shape): # Create the state of the layer (weights)
tf.print('calling build method')
w_init = tf.random_normal_initializer()
self.w = tf.Variable(
initial_value=w_init(shape=(input_shape[-1], self.units),
dtype='float32'),trainable=True)
b_init = tf.zeros_initializer()
self.b = tf.Variable(initial_value=b_init(shape=(self.units,),
dtype='float32'),trainable=True)
def call(self, inputs): # Defines the computation from inputs to outputs
tf.print('\ncalling call method')
tf.print(f'input shape : {inputs.shape}')
out = tf.matmul(inputs, self.w) + self.b
tf.print(f'output shape : {out.shape}')
return out
model = tf.keras.Sequential()
model.add(SimpleDense(units = 4))
model.compile(optimizer = 'adam',loss = 'mse',)
model.fit(tf.random.uniform((25, 2)), tf.ones((25, 1)), batch_size = 5)
model.summary()
输出:
calling build method
calling call method
input shape : (5, 2)
output shape : (5, 4)
1/5 [=====>........................] - ETA: 1s - loss: 0.9794
calling call method
input shape : (5, 2)
output shape : (5, 4)
calling call method
input shape : (5, 2)
output shape : (5, 4)
calling call method
input shape : (5, 2)
output shape : (5, 4)
calling call method
input shape : (5, 2)
output shape : (5, 4)
5/5 [==============================] - 0s 15ms/step - loss: 0.9770
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
simple_dense (SimpleDense) (5, 4) 12
=================================================================
Total params: 12
Trainable params: 12
Non-trainable params: 0
_________________________________________________________________