【问题标题】:TensorFlow - Split and SqueezeTensorFlow - 拆分和挤压
【发布时间】:2017-02-28 19:44:50
【问题描述】:

我是 TensorFlow 新手,我正在格式化一些数据以输入循环神经网络。我的数据由输入占位符x 的 3d 张量给出。我想沿第三维拆分x,为此我有(注意n_timesteps 对应于沿第三维的x 的长度):

# Split the previous 3d tensor to get a list of 'n_timesteps' 2d tensors of
# shape (batch_size, features_dimension)
x = tf.split (x, n_timesteps, axis = 2)

虽然,正如我尝试使用 numpy:

x = np.split (x, n_timesteps, axis = 2)

如果 x 是 3d ndarray,则 np.split 将返回维度为 3 的 n_timesteps 数组列表,因此第三维度是单例的。使用numpy,我知道我可以使用np.squeeze 和列表理解轻松解决这个问题,以删除单例维度:

x = [np.squeeze(a, axis=2) for a in np.split(x, n_timesteps, axis=2)]

但是我怎样才能在 TF 上做同样的事情呢?

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

    您可能正在寻找tf.unstack 操作:

    x = tf.unstack(x, axis=2)
    

    【讨论】:

      【解决方案2】:

      尝试使用Tensorflow的squeeze函数(tf.squeeze)和Tensorflow的scan函数(tf.scan),而不是列表推导式。

      tf.scan(lambda a, x_i: tf.squeeze(x_i, [2]), x, initializer=tf.constant(0, shape=[n_dim0, n_dim1]))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-24
        • 2012-07-08
        • 1970-01-01
        • 1970-01-01
        • 2017-12-23
        相关资源
        最近更新 更多