簡體   English   中英

如何更改 keras 模型中密集層的輸出?

[英]How to change the output of a dense layer in a keras model?

鑒於以下模型

Layer (type)                 Output Shape              Param #   
=================================================================
input_91 (InputLayer)        [(None, 25)]              0         
_________________________________________________________________
token_and_position_embedding (None, 25, 400)           5938800   
_________________________________________________________________
transformer_block_97 (Transf (None, 25, 400)           74832    
_________________________________________________________________
global_average_pooling1d_82  (None, 400)               0         
_________________________________________________________________
dropout_337 (Dropout)        (None, 400)               0         
_________________________________________________________________
dense_339 (Dense)            (None, 25)                22575     
_________________________________________________________________
dropout_338 (Dropout)        (None, 25)                0         
_________________________________________________________________
dense_340 (Dense)            (None, 25)                570      
=================================================================
Total params: 3,709,907
Trainable params: 3,709,907
Non-trainable params: 0

在 keras 中,如何將輸出層更改為(None, 25, 7)維? 這是當前的模型配置:

embed_dim = 400  # Embedding size for each token
num_heads = 2  # Number of attention heads
ff_dim = 32  # Hidden layer size in feed forward network inside transformer

inputs = layers.Input(shape=(25,))


embedding_layer = TokenAndPositionEmbedding(maxlen, vocab_size, embed_dim)
X = embedding_layer(inputs)
transformer_block = TransformerBlock(embed_dim, num_heads, ff_dim)
X = transformer_block(X)
X = layers.GlobalAveragePooling1D()(X)
X = layers.Dropout(0.1)(X)
X = layers.Dense(25, activation="relu")(X)
X= layers.Dropout(0.1)(X)

outputs = layers.Dense(25, activation="softmax")(x)

您正在尋找tf.keras.layers.Reshape 根據我們在評論中的討論,了解如何將圖層從(None, 25)重塑為(None, 5, 5)

inp = tf.keras.layers.Input((25))                                                                                   
layer = tf.keras.layers.Dense((25))(inp)                                                                            
reshaped = tf.keras.layers.Reshape((5,5))(layer)                                                                    
model = tf.keras.Model(inp, reshaped)

model.summary()產生

_________________________________________________________________                                                       
Layer (type)                 Output Shape              Param #                                                          
=================================================================                                                       
input_3 (InputLayer)         [(None, 25)]              0                                                                
_________________________________________________________________                                                       
dense_1 (Dense)              (None, 25)                650                                                              
_________________________________________________________________                                                       
reshape_2 (Reshape)          (None, 5, 5)              0                                                                
=================================================================                                                       
Total params: 650                                                                                                       
Trainable params: 650                                                                                                   
Non-trainable params: 0   

編輯:

為了闡明您將如何將其實現到您的代碼中,請在outputs = layers.Dense(25, activation="softmax")(x)之后添加以下內容

reshaped_outputs = layers.Reshape((5,5))(outputs)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM