python 从输出中删除尺寸

jpfvwuh4  于 2023-04-04  发布在  Python
关注(0)|答案(2)|浏览(169)

我已经建立了这个简单的LSTM模型,它提供了与我的输入相同的3维输出。但是我的目标数据是2维的。有没有办法在特定的访问中平均输出。

batch_sizes = 1
epochs = 2
timesteps = 20

inputs_1_mae = tf.keras.layers.Input(shape = (20,10),batch_size = batch_sizes)
lstm_1_mae = tf.keras.layers.LSTM(10, stateful = True, return_sequences = True)(inputs_1_mae) 
lstm_2_mae = tf.keras.layers.LSTM(10, stateful = True, return_sequences = True)(lstm_1_mae) 

output_1_mae = tf.keras.layers.Dense(units = 10)(lstm_2_mae) 

regressor_mae = tf.keras.Model(inputs= inputs_1_mae ,outputs = output_1_mae) 
regressor_mae.compile (optimizer = "adam", loss = "mae") 
regressor_mae.summary() 

regressor_mae.fit(final_x_array, final_y_array, batch_size = batch_sizes, epochs=epochs)

下面是模型的总结:

_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_16 (InputLayer)       [(1, 20, 10)]             0         
                                                                 
 lstm_17 (LSTM)              (1, 20, 10)               840       
                                                                 
 lstm_18 (LSTM)              (1, 20, 10)               840       
                                                                 
 dense_16 (Dense)            (1, 20, 10)               110

我希望输出的形状是(1,10)。我如何消除那个特定的轴?谢谢

p4rjhz4m

p4rjhz4m1#

只需remove return_sequences = True of your last lstm layer,因为您只需要最后的输出。

tf7tbtn2

tf7tbtn22#

您可以使用Lambda层来实现这一点。以下内容可能会有所帮助:

import tensorflow as tf

batch_sizes = 1
epochs = 2
timesteps = 20

inputs_1_mae = tf.keras.layers.Input(shape = (20,10),batch_size = batch_sizes)
lstm_1_mae = tf.keras.layers.LSTM(10, stateful = True, return_sequences = True)(inputs_1_mae) 
lstm_2_mae = tf.keras.layers.LSTM(10, stateful = True, return_sequences = True)(lstm_1_mae) 

output_1_mae = tf.keras.layers.Dense(units = 10)(lstm_2_mae) 

output_1_mae_avg=tf.keras.layers.Lambda(lambda var_x: tf.keras.backend.mean(var_x, axis=1),)(output_1_mae)

regressor_mae = tf.keras.Model(inputs= inputs_1_mae ,outputs=output_1_mae_avg) 
regressor_mae.compile (optimizer = "adam", loss = "mae") 
regressor_mae.summary()

输出:

Model: "model_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_2 (InputLayer)        [(1, 20, 10)]             0         
                                                                 
 lstm_2 (LSTM)               (1, 20, 10)               840       
                                                                 
 lstm_3 (LSTM)               (1, 20, 10)               840       
                                                                 
 dense_1 (Dense)             (1, 20, 10)               110       
                                                                 
 lambda_1 (Lambda)           (1, 10)                   0         
                                                                 
=================================================================
Total params: 1,790
Trainable params: 1,790
Non-trainable params: 0
_________________________________________________________________

相关问题