我是 pytorch 用户。我在 tensorflow 中有一个预训练模型,我想将它转移到 pytorch 中。在模型架构的一部分中,我的意思是在 tensorflow 定义的模型中,有一个函数tf.space_to_depth将输入大小 (None, 38,38,64) 转换为 (None, 19,19, 256)。( https://www.tensorflow.org/api_docs/python/tf/space_to_depth ) 是这个函数的文档。但我无法理解这个函数实际上是做什么的。您能否提供一些 numpy 代码来为我说明?
实际上,我想在 pytorch 中制作一个完全相同的图层。
tensorflow 中的一些代码揭示了另一个秘密:这里有一些代码:
import numpy as np
import tensorflow as tf
norm = tf.random_normal([1, 2, 2, 1], mean=0, stddev=1)
trans = tf.space_to_depth(norm,2)
with tf.Session() as s:
norm = s.run(norm)
trans = s.run(trans)
print("Norm")
print(norm.shape)
for index,value in np.ndenumerate(norm):
print(value)
print("Trans")
print(trans.shape)
for index,value in np.ndenumerate(trans):
print(value)
这是输出:
Norm
(1, 2, 2, 1)
0.695261
0.455764
1.04699
-0.237587
Trans
(1, 1, 1, 4)
1.01139
0.898777
0.210135
2.36742
正如您在上面看到的,除了数据整形之外,张量值也发生了变化!