如何将pytorch的adaptive_avg_pool2d方法转换为keras或tensorflow [英] how to convert pytorch adaptive_avg_pool2d method to keras or tensorflow
问题描述
我不知道如何将PyTorch方法 adaptive_avg_pool2d
转换为Keras或TensorFlow。有人可以帮忙吗?
PyTorch方法是
I don't know how to convert the PyTorch method adaptive_avg_pool2d
to Keras or TensorFlow. Anyone can help?
PyTorch mehod is
adaptive_avg_pool2d(14,[14])
我尝试使用平均池化,重塑了Keras中的张量,但得到了错误:
I tried to use the average pooling, the reshape the tensor in Keras, but got the error:
ValueError:新数组的总大小必须保持不变
ValueError: total size of new array must be unchanged
推荐答案
我不确定我是否理解您的问题,但是在PyTorch中,您将空间尺寸传递给 AdaptiveAvgPool2d
。例如,如果您希望输出大小为5x7,则可以使用 nn.AdaptiveAvgPool2d((5,7))
。
I'm not sure if I understood your question, but in PyTorch, you pass the spatial dimensions to AdaptiveAvgPool2d
. For instance, if you want to have an output sized 5x7, you can use nn.AdaptiveAvgPool2d((5,7))
.
如果要使用全局平均池化层,可以使用 nn.AdaptiveAvgPool2d(1)
。在Keras中,您可以使用 GlobalAveragePooling2D
。
If you want a global average pooling layer, you can use nn.AdaptiveAvgPool2d(1)
. In Keras you can just use GlobalAveragePooling2D
.
对于Keras中的其他输出大小,您需要使用 AveragePooling2D
,但是您不能直接指定输出形状。您需要计算/定义 pool_size
,跨度
和填充
参数取决于所需的输出形状。如果您需要计算方面的帮助,请查看 CS231n课程的本页。
For other output sizes in Keras, you need to use AveragePooling2D
, but you can't specify the output shape directly. You need to calculate/define the pool_size
, stride
, and padding
parameters depending on how you want the output shape. If you need help with the calculations, check this page of CS231n course.
这篇关于如何将pytorch的adaptive_avg_pool2d方法转换为keras或tensorflow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!