【问题标题】:Passing input tensor size to via tensorflow pipeline通过张量流管道将输入张量大小传递给
【发布时间】:2019-03-12 10:59:20
【问题描述】:

我收到以下错误

ValueError:应定义Dense 输入的最后一个维度。找到None

当使用 tf.data 管道将张量传递给 tf.layers.dense 时。我的代码的相关部分是:

def _parse_function(example_proto):
    features = {'X': tf.VarLenFeature(tf.float32),
                'Y': tf.VarLenFeature(tf.float32)}
    parsed_features = tf.parse_example(example_proto, features)
    X = tf.sparse_tensor_to_dense(parsed_features['X'])
    Y = tf.sparse_tensor_to_dense(parsed_features['Y'])
    return X, Y

dataset = tf.data.TFRecordDataset(fin)
dataset = dataset.batch(100)
dataset = dataset.map(_parse_function)
dataset = dataset.cache()
dataset = dataset.repeat()
iterator = dataset.make_one_shot_iterator()
X, Y = iterator.get_next()

hidden_0 = tf.layers.dense(X, N_HIDDEN_0, activation=tf.nn.crelu, use_bias=False)

这是因为使用 VarLenFeature 或 sparse_tensor_to_dense 操作无法将张量形状传递给 tf.layers.dense 吗?有没有办法在不使用 sess.run(X,Y) 并使用 feed_dict 提供输出的情况下修复它?

我想知道这是否与@mrry 在https://github.com/tensorflow/tensorflow/issues/13348 中解决的问题相似。

任何见解都将不胜感激!

谢谢!

【问题讨论】:

    标签: tensorflow tensorflow-datasets


    【解决方案1】:

    我想出了如何通过将我的 def _parse_function 更改为以下内容来解决问题:

    def _parse_function(example_proto):
        features = {'X': tf.FixedLenFeature([18],tf.float32), 'Y': tf.FixedLenFeature([3],tf.float32)}
        parsef = tf.parse_example(example_proto, features)
        return parsef['X'], parsef['Y']
    

    所以我猜问题是因为在我的原始代码中使用 tf.VarLenFeature,输入和输出节点的数量无法传达给 tf.layers.dense。我仍然不清楚为什么我必须指定 X 和 Y 的列数(上面代码中的 [18] 和 [3]),而不是将它们指定为 [ ] - 但我可以接受。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多