[TensorFlow] 關於tf.keras.layers.Embedding中參數input_length的作用


本文基於SO的帖子:Link: https://stackoverflow.com/questions/61848825/why-is-input-length-needed-in-layers-embedding-in-keras-tensorflow

在翻文檔的時候,發現了input_length這個參數,不知道有什么用。文檔里的注釋是:

input_length : Length of input sequences, when it is constant. This argument is required if you are going to connect Flatten then Dense layers upstream (without it, the shape of the dense outputs cannot be computed).

SO原提問是說,為什么我們在連接Flatten and Dense的時候不指定input_length時就會計算不出shape呢?

答案大致如下:

input_length這個參數是用來指定輸入的長度,相當於在Embedding層之前加了一個Input層。 我們在連接Flatten以及Dense之前,我們需要這么一個input的長度,是因為Dense這一步是一個全連接,因此所有的長度必須指定,不然的話Dense與Flatten之間的W矩陣形狀無法確定。

概念dim & length

列表: dim = 1, 矩陣: dim = 2。 length可以理解為batch的大小,即有多少個樣本。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM