简体   繁体   English

如何更改 keras 模型中密集层的输出?

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

Given the following 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

In keras, how to change the output layer to (None, 25, 7) dimension?在 keras 中,如何将输出层更改为(None, 25, 7)维? This is the current model configuration:这是当前的模型配置:

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)

You are looking for tf.keras.layers.Reshape .您正在寻找tf.keras.layers.Reshape Per our discussion in the comments, see how to reshape a layer from (None, 25) to (None, 5, 5) .根据我们在评论中的讨论,了解如何将图层从(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() yields 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   

EDIT:编辑:

To clarify how you would implement this into your code, add the following after outputs = layers.Dense(25, activation="softmax")(x)为了阐明您将如何将其实现到您的代码中,请在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