简体   繁体   English

Tensorflow - 构建 LSTM 模型 - 需要 tf.keras.layers.Dense()

[英]Tensorflow - building LSTM model - need for tf.keras.layers.Dense()

Python 3.7 tensorflow Python 3.7 张量流

I am experimenting Time series forecasting w Tensorflow我正在尝试使用Tensorflow进行时间序列预测

I understand the second line creates a LSTM RNN ie a Recurrent Neural Network of type Long Short Term Memory.我知道第二行创建了一个LSTM RNN,即长短期记忆类型的循环神经网络。

Why do we need to add a Dense(1) layer in the end?为什么我们最后需要添加一个Dense(1)层?

single_step_model = tf.keras.models.Sequential()
single_step_model.add(tf.keras.layers.LSTM(32, input_shape=x_train_single.shape[-2:]))
single_step_model.add(tf.keras.layers.Dense(1))

Tutorial for Dense() says Dense()教程说

Dense implements the operation: output = activation(dot(input, kernel) + bias) where activation is the element-wise activation function passed as the activation argument, kernel is a weights matrix created by the layer, and bias is a bias vector created by the layer (only applicable if use_bias is True). Dense 实现操作: output = activation(dot(input, kernel) + bias)其中activation是作为激活参数传递的逐元素激活函数, kernel是层创建的权重矩阵, bias是创建的偏置向量按层(仅当 use_bias 为 True 时适用)。

would you like to rephrase or elaborate on need for Dense() here ?您想在此处重新表述或详细说明对Dense()吗?

The following line以下行

single_step_model.add(tf.keras.layers.LSTM(32, input_shape=x_train_single.shape[-2:]))

creates an LSTM layer which transforms each input step of size #features into a latent representation of size 32. You want to predict a single value so you need to convert this latent representation of size 32 into a single value.创建一个 LSTM 层,它将大小为 #features 的每个输入步骤转换为大小为 32 的潜在表示。您想要预测单个值,因此您需要将这个大小为 32 的潜在表示转换为单个值。 Hence, you add the following line因此,您添加以下行

single_step_model.add(tf.keras.layers.Dense(1))

which adds a Dense Layer (Fully-Connected Neural Network) with one neuron in the output which, obviously, produces a single value.它在输出中添加了一个带有一个神经元的密集层(全连接神经网络),显然,它会产生一个值。 Look at it as a way to transform an intermediate result of higher dimensionality into the final result.将其视为将更高维度的中间结果转换为最终结果的一种方式。

Well in the tutorial you are following Time series forecasting , they are trying to forecast temperature (6 hrs ahead).在本教程中,您正在关注时间序列预测,他们正在尝试预测温度(提前 6 小时)。 For which they are using an LSTM followed by a Dense layer.为此,他们使用LSTM然后是Dense层。

single_step_model = tf.keras.models.Sequential()
single_step_model.add(tf.keras.layers.LSTM(32, input_shape=x_train_single.shape[-2:]))
single_step_model.add(tf.keras.layers.Dense(1))

Dense layer is nothing but a regular fully-connected NN layer. Dense层只不过是一个常规的全连接 NN 层。 In this case you are bringing down the output dimensionality to 1, which should represent some proportionality (need not be linear) to the temperature you are trying to predict.在这种情况下,您将输出维度降低到 1,这应该代表与您尝试预测的温度的某种比例(不必是线性的)。 There are other layers you could use as well.您也可以使用其他层。 Check out, Keras Layers .看看, Keras 层

If you are confused about the input and output shape of LSTM, check out I/O Shape .如果您对 LSTM 的输入和输出形状感到困惑,请查看I/O Shape

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该代码显示语法错误“ tf.keras.layers.Dense(units = 4)” - The code is showing syntax error “tf.keras.layers.Dense(units=4)” tf.keras.layers.Dense output 对同一输入行的更改 - tf.keras.layers.Dense output changes for the same input row Keras - 使用 tf.keras.layers.Dense(1,activation='sigmoid')(x) 时指定 from_logits=False - Keras - Specifying from_logits=False when using tf.keras.layers.Dense(1,activation='sigmoid')(x) tf.contrib.layer.fully_connected, tf.layers.dense, tf.contrib.slim.fully_connected, tf.keras.layers.Dense 之间的不一致 - Inconsistencies between tf.contrib.layer.fully_connected, tf.layers.dense, tf.contrib.slim.fully_connected, tf.keras.layers.Dense Tensorflow tf.keras.layers.Reshape RNN/LSTM - Tensorflow tf.keras.layers.Reshape RNN/LSTM 使用 Keras 和 TensorFlow 构建 LSTM Sequential 模型 - Using Keras and TensorFlow building LSTM Sequential model 在具有Tensorflow后端的Keras上,在不同的输入部分上并行拟合LSTM和一些密集层 - on Keras with Tensorflow backend, fitting a LSTM and some dense layers in parallel on different fractions of input TensorFlow - tf.keras.layers.Layer 与 tf.keras.Model 之间的区别 - TensorFlow - Difference between tf.keras.layers.Layer vs tf.keras.Model TensorFlow 2.0 如何从 tf.keras.layers 层获取可训练变量,如 Conv2D 或 Dense - TensorFlow 2.0 How to get trainable variables from tf.keras.layers layers, like Conv2D or Dense 尝试使用 tf.keras.utils.plot_model 时,带有 TimeDistributed Layers 的 CNN-LSTM 表现异常 - CNN-LSTM with TimeDistributed Layers behaving weirdly when trying to use tf.keras.utils.plot_model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM