【问题标题】:Problems reshaping an array in TF在 TF 中重塑数组的问题
【发布时间】:2017-07-04 13:48:27
【问题描述】:

我正在传递一个 [64,240] tf.array。 我想将其重塑为 [64,10,24] tf.array。 我尝试了各种不同的方法,但总是遇到同样的错误。

... raise ValueError("Shape %s must have rank %d" % (self, rank)) ValueError: Shape (64, 240) 必须有排名 1

代码在以下位置失败: self.x_expand[i] = tf.reshape(self.input_x[i],[num_classes,data_size])

我想我犯了一个基本错误,希望有人能指出它是什么......

更大的代码片段是:

类 CNN(对象):

def __init__(
  self, sequence_length, num_classes, data_size,
  filter_sizes, num_filters, l2_reg_lambda=0.0, batch_size=64):

    # Placeholders for input, output and dropout
    self.input_x = tf.placeholder(tf.int32, [batch_size, sequence_length], name="input_x")
    self.input_y = tf.placeholder(tf.float32, [batch_size, num_classes], name="input_y")
    self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")

    with tf.device('/cpu:0'), tf.name_scope("reshaping"):
        self.x_expand = tf.placeholder(tf.int32, [batch_size, num_classes,data_size], name="expand_x")
        for i in range(batch_size):
            self.x_expand[i] = tf.reshape(self.input_x[i],[num_classes,data_size]) 
        self.x_expanded = tf.expand_dims(self.x_expanded, -1)

【问题讨论】:

    标签: python-3.x tensorflow


    【解决方案1】:

    您似乎希望 self.x_expand 成为重新整形的输入,我认为以下可能更合适:

    with tf.device('/cpu:0'), tf.name_scope("reshaping"):
        self.x_expand = tf.reshape(self.input_x, [batch_size, num_classes, data_size])
    

    注意两点:

    1. 没有为self.x_expand 使用tf.placeholder。占位符用于提供用户的输入,但在这里您似乎真正想要的是重塑现有的提要

    2. 不是在每个元素的基础上改变形状,而是通过单个操作。

    注意:我无法重现您看到的错误消息,可能是因为我们使用了不同版本的 TensorFlow,并且最近版本的错误消息报告发生了变化。我正在使用 1.0.0。

    【讨论】:

      【解决方案2】:

      我觉得你应该试试:

      self.x_expand = tf.reshape(self.input_x, [batch_size, num_classes,data_size]

      【讨论】:

      • 感谢您的快速回复。这解决了这个问题。我也会考虑更新 tensorflow。
      猜你喜欢
      • 1970-01-01
      • 2020-09-20
      • 2013-11-30
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      相关资源
      最近更新 更多