【问题标题】:Replace tf.squeeze using as tf.reshape使用 as tf.reshape 替换 tf.squeeze
【发布时间】:2018-05-19 09:55:32
【问题描述】:

我需要使用 tensorflow 训练一个移动网络。不支持 tf.squeeze 层。我可以用 tf.reshape 替换它吗?

是操作:

tf.squeeze(net, [1, 2], name='squeeze')

同:

tf.reshape(net, [50,1000], name='reshape')

其中 net 的形状为 [50,1,1,1000]。

【问题讨论】:

    标签: tensorflow reshape


    【解决方案1】:

    为什么说不支持tf.squeeze?为了从张量中移除一维轴,tf.squeeze 是正确的操作。但是您也可以使用tf.reshape 完成您想要的工作,尽管我建议您使用tf.squeeze

    【讨论】:

    • 感谢您的回复!我正在为移动设备使用另一个 sdk,它加载 tensorflow 模型但不支持 tf.squeeze 层。
    【解决方案2】:

    tf 2.0 中,您可以轻松检查这些操作是否相同。唯一的区别是您可以使用dim == 1 删除所有轴而不指定它们。所以在最后一行你可以使用tf.squeeze(x_resh) 而不是tf.squeeze(x_resh, [1, 2])

    size = [2, 3]
    tf.random.set_seed(42)
    x = tf.random.normal(size)
    x
    <tf.Tensor: shape=(2, 3), dtype=float32, numpy=
    array([[ 0.3274685, -0.8426258,  0.3194337],
           [-1.4075519, -2.3880599, -1.0392479]], dtype=float32)>
    
    x_resh = tf.reshape(x, [2, 1, 1, 3])
    x_resh
    <tf.Tensor: shape=(2, 1, 1, 3), dtype=float32, numpy=
    array([[[[ 0.3274685, -0.8426258,  0.3194337]]],
           [[[-1.4075519, -2.3880599, -1.0392479]]]], dtype=float32)>
    
    tf.reshape(x_resh, [2, 3])
    <tf.Tensor: shape=(2, 3), dtype=float32, numpy=
    array([[ 0.3274685, -0.8426258,  0.3194337],
           [-1.4075519, -2.3880599, -1.0392479]], dtype=float32)>
    
    tf.squeeze(x_resh, [1, 2])
    <tf.Tensor: shape=(2, 3), dtype=float32, numpy=
    array([[ 0.3274685, -0.8426258,  0.3194337],
           [-1.4075519, -2.3880599, -1.0392479]], dtype=float32)>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2022-07-04
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2018-08-30
      相关资源
      最近更新 更多