【发布时间】:2017-10-31 02:08:34
【问题描述】:
我是 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
正如你在上面看到的,除了数据重塑之外,张量值也发生了变化!
【问题讨论】:
-
您的值可能正在改变,因为您必须分开 session.run 调用 norm 和 trans 所以生成的随机值在调用之间是不同的。
-
也就是说,尝试尝试
norm, trans = s.run([norm, trans])。
标签: tensorflow deep-learning keras pytorch