In Keras, using the Flatten()
layer retains the batch size. For eg, if the input shape to Flatten is (32, 100, 100)
, in Keras
output of Flatten is (32, 10000)
, but in PyTorch it is 320000
. Why is it so?
You can use torch. flatten() or Tensor. flatten() with start_dim=1 to start the flattening operation after the batch dimension.
Flattens input by reshaping it into a one-dimensional tensor. If start_dim or end_dim are passed, only dimensions starting with start_dim and ending with end_dim are flattened. The order of elements in input is unchanged.
nn. Flatten flattens all dimensions starting from the second dimension (index 1) by default. You can see this behaviour in the default values of the start_dim and end_dim arguments.
Batch size is a term used in machine learning and refers to the number of training examples utilized in one iteration. If this is right than 100 training data should be loaded in one iteration.
As OP already pointed out in their answer, the tensor operations do not default to considering a batch dimension. You can use torch.flatten()
or Tensor.flatten()
with start_dim=1
to start the flattening operation after the batch dimension.
Alternatively since PyTorch 1.2.0 you can define an nn.Flatten()
layer in your model which defaults to start_dim=1
.
Yes, As mentioned in this thread, PyTorch operations such as Flatten, view, reshape.
In general when using modules like Conv2d
, you don't need to worry about batch size. PyTorch takes care of it. But when dealing directly with tensors, you need to take care of batch size.
In Keras, Flatten()
is a layer. But in PyTorch, flatten()
is an operation on the tensor. Hence, batch size needs to be taken care manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With