TensorFlow 模型的输入和输出形状

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

Tensorflow model input and output shapes

问题

为了获得输出形状为 (BATCH_SIZE, N_STEPS, N_FEATURES),你可以在定义模型时将 input_shape 参数设置为 (None, N_STEPS, N_FEATURES),其中 None 表示输入的第一个维度可以是任何值(即批处理大小可以变化),而不仅仅是 32。这将使模型能够处理不同批处理大小的输入,并产生所需的输出形状。

以下是修改后的代码部分:

model = tf.keras.models.Sequential([
        tf.keras.layers.LSTM(32, return_sequences=True, input_shape=(None, N_STEPS, N_FEATURES)),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True)),
        tf.keras.layers.Dense(N_FEATURES)
])

这样,模型将能够适应不同批处理大小,并且输出形状将取决于输入的实际批处理大小。

英文:

considering i have the next model and the fact that the dataset is batched as 32.
Why don't the batch size is inside the output shape ?
If i have BATCH_SIZE=32 , shouldn't have an output shape of (BATCH_SIZE,N_STEPS,N_FEATURES) ?
how can i get an output shape like this ?


model = tf.keras.models.Sequential([
        tf.keras.layers.LSTM(32, return_sequences=True, input_shape=(N_STEPS, N_FEATURES)),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True)),
        tf.keras.layers.Dense(N_FEATURES)
])

model.summary()

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 10, 32)            4352      
                                                                 
 bidirectional (Bidirectiona  (None, 10, 64)           16640     
 l)                                                              
                                                                 
 dense (Dense)               (None, 10, 1)             65        
                                                                 
=================================================================
Total params: 21,057
Trainable params: 21,057
Non-trainable params: 0

I'm trying to get an output shape of (BATCH_SIZE,N_STEPS,N_FEAUTURES)

答案1

得分: 1

批次在您的摘要中标记为"None",在所有层的形状中都是"None"。这是因为您没有明确传递它给您的模型。
要做到这一点,您可以使用batch_input_shape=(BATCH_SIZE, N_STEPS, N_FEATURES)而不是input_shape=(N_STEPS, N_FEATURES)。

tf.keras.layers.LSTM(32, return_sequences=True, batch_input_shape=(BATCH_SIZE, N_STEPS, N_FEATURES)),
英文:

The batch is marked in your summary as None, in all layers shapes. It is None because you have not explicitly passed it to your model.
Now to do this you can use batch_input_shape=(BATCH_SIZE,N_STEPS,N_FEAUTURES) instead of input_shape=(N_STEPS, N_FEATURES)

tf.keras.layers.LSTM(32, return_sequences=True, batch_input_shape=(BATCH_SIZE,N_STEPS,N_FEAUTURES)),

huangapple
  • 本文由 发表于 2023年6月19日 18:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505760.html
匿名

发表评论

匿名网友

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

确定