LSTM在Keras中的输入维度是多少?

huangapple go评论77阅读模式
英文:

What is the input dimension for a LSTM in Keras?

问题

I'm trying to use deeplearning with LSTM in keras.
我尝试在keras中使用LSTM进行深度学习。

I use a number of signal as input (nb_sig) that may vary during the training with a fixed number of samples (nb_sample).
我使用一定数量的信号作为输入(nb_sig),这在训练过程中可能会发生变化,但样本数保持不变(nb_sample)。

I would like to make parameter identification, so my output layer is the size of my parameter number (nb_param).
我想进行参数识别,因此我的输出层的大小与我的参数数量(nb_param)相同。

So I created my training set of size (nb_sig x nb_sample) and the label (nb_param x nb_sample).
因此,我创建了大小为(nb_sig x nb_sample)的训练集和标签(nb_param x nb_sample)。

My issue is I cannot find the correct dimension for the deep learning model.
我的问题是我无法找到深度学习模型的正确维度。

I tried this:
我尝试了以下代码:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM

nb_sample = 500
nb_sig = 100 # number that may change during the training
nb_param = 10

train = np.random.rand(nb_sig,nb_sample)
label = np.random.rand(nb_sig,nb_param)

print(train.shape,label.shape)
DLmodel = Sequential()
DLmodel.add(LSTM(units=nb_sample, return_sequences=True, input_shape=(None, nb_sample), activation='tanh'))
DLmodel.add(Dense(nb_param, activation="linear", kernel_initializer="uniform"))

DLmodel.compile(loss='mean_squared_error', optimizer='RMSprop', metrics=['accuracy', 'mse'], run_eagerly=True)
print(DLmodel.summary())

DLmodel.fit(train, label, epochs=10, batch_size=nb_sig)

but I get this error message:
但是我得到了以下错误消息:

Traceback (most recent call last):
  File "C:\Users\maxime\Desktop\SESAME\PycharmProjects\LargeScale_2022_09_07\di3.py", line 22, in <module>
    DLmodel.fit(train, label, epochs=10, batch_size=nb_sig)
  File "C:\Python310\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Python310\lib\site-packages\keras\engine\input_spec.py", line 232, in assert_input_compatibility
    raise ValueError(
ValueError: Exception encountered when calling layer "sequential" (type Sequential).

Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (100, 500)

Call arguments received by layer "sequential" (type Sequential):
  • inputs=tf.Tensor(shape=(100, 500), dtype=float32)
  • training=True
  • mask=None

I don't understand what I'm suppose to put as `input_shape` for the LSTM layer and as the number of signals I use during the training will changed, this is not so clear to me.
我不明白我应该为LSTM层的`input_shape`参数放什么,因为在训练过程中我使用的信号数量会发生变化,这对我来说不太清楚。


<details>
<summary>英文:</summary>

I&#39;m trying to use deeplearning with LSTM in keras .
I use a number of signal as input (`nb_sig`) that may vary during the training with a fixed number of samples (`nb_sample`)
I would like to make parameter identification, so my output layer is the size of my parameter number (`nb_param`)

so I created my training set of size (`nb_sig` x `nb_sample`) and the label (`nb_param` x `nb_sample`)

my issue is I cannot find the correct dimension for the deep learning model. 
I tried this :

    import numpy as np
    from keras.models import Sequential
    from keras.layers import Dense, LSTM
    
    nb_sample = 500
    nb_sig = 100 # number that may change during the training
    nb_param = 10
    
    
    train = np.random.rand(nb_sig,nb_sample)
    label = np.random.rand(nb_sig,nb_param)
    
    
    print(train.shape,label.shape)
    DLmodel = Sequential()
    DLmodel.add(LSTM(units=nb_sample, return_sequences=True, input_shape =(None,nb_sample), activation=&#39;tanh&#39;))
    DLmodel.add(Dense(nb_param, activation=&quot;linear&quot;, kernel_initializer=&quot;uniform&quot;))
    
    DLmodel.compile(loss=&#39;mean_squared_error&#39;, optimizer=&#39;RMSprop&#39;, metrics=[&#39;accuracy&#39;, &#39;mse&#39;], run_eagerly=True)
    print(DLmodel.summary())
    
    DLmodel.fit(train, label, epochs=10, batch_size=nb_sig)


but I get this error message:

    Traceback (most recent call last):
      File &quot;C:\Users\maxime\Desktop\SESAME\PycharmProjects\LargeScale_2022_09_07\di3.py&quot;, line 22, in &lt;module&gt;
        DLmodel.fit(train, label, epochs=10, batch_size=nb_sig)
      File &quot;C:\Python310\lib\site-packages\keras\utils\traceback_utils.py&quot;, line 70, in error_handler
        raise e.with_traceback(filtered_tb) from None
      File &quot;C:\Python310\lib\site-packages\keras\engine\input_spec.py&quot;, line 232, in assert_input_compatibility
        raise ValueError(
    ValueError: Exception encountered when calling layer &quot;sequential&quot; &quot;                 f&quot;(type Sequential).
    
    Input 0 of layer &quot;lstm&quot; is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (100, 500)
    
    Call arguments received by layer &quot;sequential&quot; &quot;                 f&quot;(type Sequential):
      • inputs=tf.Tensor(shape=(100, 500), dtype=float32)
      • training=True
      • mask=None

I don&#39;t understand what I&#39;m suppose to put as `input_shape` for the LSTM layer and as the number of signals I use during the training will changed, this is not so clear to me.

</details>


# 答案1
**得分**: 1

LSTM的输入应为3D,第一个维度是样本大小,在您的情况下为500。假设输入的形状为(500,x,y),则输入形状应为(x,y)。

<details>
<summary>英文:</summary>

The input to the LSTM should be 3D with the first dimension being the sample size in your case 500. Assuming input having shape (500,x,y), input_shape should (x,y).

</details>



# 答案2
**得分**: 1

根据Keras的[文档][1],LSTM层以三维张量作为输入,需要一个维度专门用于时间步。由于您正在使用默认参数time_major=False,输入应该采用[批次,时间步,特征]的形式。

这个[相关问题][2]可能有助于您更好地理解LSTM输入形状。

  [1]: https://keras.io/api/layers/recurrent_layers/lstm/
  [2]: https://stats.stackexchange.com/questions/274478/understanding-input-shape-parameter-in-lstm-with-keras

<details>
<summary>英文:</summary>

As per the Keras [documentation][1], the LSTM layer takes a three-dimensional tensor as input, and requires one dimension dedicated to timesteps. Since you are using the default parameter time_major=False, the input should be in the form [batch, timesteps, feature]. 

This [related question][2] may help you understand LSTM input shapes better.


  [1]: https://keras.io/api/layers/recurrent_layers/lstm/
  [2]: https://stats.stackexchange.com/questions/274478/understanding-input-shape-parameter-in-lstm-with-keras

</details>



# 答案3
**得分**: 1

LSTM的输入必须采用以下格式:

    [样本数, 时间步, 特征数]

或者按照你的记法:

    [nb_sig x nb_sample x n_features]

因此,你需要将训练数据重塑为这种格式。而你现在的格式是:

> 所以我创建了大小为 (nb_sig x nb_sample) 的训练集

可以看到,这是一个二维输入,而不是三维的。你缺少的是第三维,这在你的情况下似乎是特征数。只需使用 `numpy.expand_dims()` 来给向量添加一个额外的维度 https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html
然后你应该可以正常进行 - 假设你有单变量时间序列。

<details>
<summary>英文:</summary>

The input to LSTM has to be in the following format:

    [sample, timestep, n_features]
or in your notation

    [nb_sig x nb_sample x n_features]

Therefore you need to reshape the training data to that format. Instead you have:

&gt; so I created my training set of size (nb_sig x nb_sample) 

As you can see that is 2D input not 3D. You are missing the 3D dimension which in your case seems to be number of features. 
Simply add 1 extra dimension to the vector using `numpy.expand_dims()` https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html
and you should be good to go :) - assuming you have univariate time-series.

</details>



huangapple
  • 本文由 发表于 2023年2月27日 16:28:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定