【问题标题】:input function requirement for train_in_memorytrain_in_memory 的输入函数要求
【发布时间】:2020-12-20 16:22:07
【问题描述】:

我关注https://www.tensorflow.org/tutorials/estimator/boosted_trees_model_understanding

要使用 train_in_memory=false 训练估计器,我们使用输入函数

 def make_input_fn(X, y, n_epochs=None, shuffle=True):
  def input_fn():
    dataset = tf.data.Dataset.from_tensor_slices((X.to_dict(orient='list'), y))
    if shuffle:
      dataset = dataset.shuffle(NUM_EXAMPLES)
    # For training, cycle thru dataset as many times as need (n_epochs=None).
    dataset = (dataset
      .repeat(n_epochs)
      .batch(NUM_EXAMPLES))
    return dataset
  return input_fn

为了做到这一点,train_in_memory=true,我们使用输入函数

def make_inmemory_train_input_fn(X, y):
  y = np.expand_dims(y, axis=1)
  def input_fn():
    return dict(X), y
  return input_fn

为什么这两个功能如此不同?

我想让它类似于make_input_fn 并尝试了以下代码

def make_inmemory_train_input_fn(X, y):
    # y = np.expand_dims(y, axis=1)

    def input_fn():
        return tf.data.Dataset.from_tensor_slices((dict(X), y))
        # return dict(X), y

    return input_fn

报错

tensorflow.python.framework.errors_impl.InvalidArgumentError:维度 0 的切片索引 0 超出范围。对于“boosted_trees/strided_slice”(操作:“StridedSlice”),输入形状:[0]、[1]、[1]、[1],计算输入张量:input[1] = 、input[2 ] = ,输入[3] = 。

我完全不知道怎么读。

我阅读了文档https://www.tensorflow.org/api_docs/python/tf/estimator/BoostedTreesClassifier

train_in_memorytrain_in_memory bool, when true, it assumes the dataset is in memory, i.e., input_fn should return the entire
dataset as a single batch, n_batches_per_layer should be set as 1, num_worker_replicas
should be 1, and num_ps_replicas should be 0 in tf.Estimator.RunConfig.

我需要进行哪些更改才能使 from_tensor_slicesmake_inmemory_train_input_fn 中工作?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    据我所知,两个输入函数之间的主要区别在于,内存中将返回 numpy 数组的字典,而非内存中将返回张量字典以供 tf.dataset API 使用。此外,前者不需要批处理内存数据集。
    我不知道如何从切片创建一个tf.dataset,这将使内存计算工作。但我尝试在输入法中传递批处理tf.dataset,我得到“初始化错误”。所以我的猜测是 in-memory 方法不采用 tf.dataset 对象,而是直接采用 numpy 切片。

    【讨论】:

      猜你喜欢
      • 2015-07-21
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      相关资源
      最近更新 更多