我是一个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中的一些代码揭示了另一个秘密: 以下是一些代码:
正如您在上面看到的,除了数据重塑之外,张量值已经发生了变化!
实际上,我想在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
正如您在上面看到的,除了数据重塑之外,张量值已经发生了变化!
norm, trans = s.run([norm, trans])
。 - Allan Zelener