[英]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)
whereactivation
is the element-wise activation function passed as the activation argument,kernel
is a weights matrix created by the layer, andbias
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.