首页 > 解决方案 > 如何在此 LSTM 示例代码中计算可训练参数数量为 335872?

问题描述

我得到了这个示例代码,但不知道如何计算可训练参数为 335872?(显示在以下输出中)

如果有人可以帮助解决这个问题,我将不胜感激。谢谢!

- - - - - - - - - - - - -代码 - - - - - - - - - - - - ------------

input_shape = (None, num_encoder_tokens)

# Define an input sequence and process it.
  encoder_inputs = Input(shape=input_shape)
  encoder = LSTM(latent_dim, return_state=True)
  encoder_outputs, state_h, state_c = encoder(encoder_inputs)

# We discard `encoder_outputs` and only keep the states.
  encoder_states = [state_h, state_c]

  encoder_model = Model(encoder_inputs, encoder_states)
  encoder_model.summary(line_length=100)

  encoder_model.output_shape

----------输出如下----------


Layer (type)           Output Shape                      Param #        
=================================================================================
input_2 (InputLayer)   (None, None, 71)                        0              
_________________________________________________________________________________
lstm_5 (LSTM)     [(None, 256), (None, 256), (None, 256)] 335872         
=================================================================================
Total params: 335,872
Trainable params: 335,872
Non-trainable params: 0
_________________________________________________________________________________
[(None, 256), (None, 256)]

标签: pythonmachine-learningkeraslstm

解决方案


我假设您想知道如何训练模型以便可以计算 、 等weight matricesbiases

您的代码的问题在于您只定义了模型的体系结构。你还没有真正编译它。最后这样做:

encoder_model.compile(loss='binary_crossentropy', optimizer='adam', metrics='binary_accuracy')

在上面的代码行中lossoptimizermetrics由您根据问题的类型进行选择。


推荐阅读