【问题标题】:Tensorflow: How to define a one-hot feature column for a canned estimatorTensorflow:如何为固定估计器定义单热特征列
【发布时间】:2017-12-14 20:18:56
【问题描述】:

我的 one-hot 编码在训练期间似乎错误地具有 3 个维度(我认为它应该具有 2 个维度),这会导致 OOM。我如何错误地构造 one-hot 特征列?

当我开始训练神经网络时出现此错误:

使用 shape[114171,829,829] 分配张量时的 OOM

[[节点: dnn/input_from_feature_columns/input_layer/air_store_id_indicator/one_hot = OneHot[T=DT_FLOAT, TI=DT_INT64, 轴=-1, _device="/job:localhost/replica:0/task:0/gpu:0"](dnn/input_from_feature_columns/input_layer/air_store_id_indicator/SparseToDense/_149, dnn/input_from_feature_columns/input_layer/air_store_id_indicator/one_hot/depth, dnn/input_from_feature_columns/input_layer/air_store_id_indicator/one_hot/on_value, dnn/input_from_feature_columns/input_layer/air_store_id_indicator/one_hot/off_value)]]

我尝试定义一个单热特征列以在我的 DNNRegressor 中使用,如下所示:

tf.feature_column.indicator_column(
    tf.feature_column.categorical_column_with_identity(key='id', num_buckets=df_train['id'].unique().size))

DNNRegressor::fit() 的 input_fn 中,我像这样填充 one-hot 编码:

labels, uniques = pd.factorize(df_train['id'])
returned_feature_columns[k] = tf.one_hot(labels, uniques.size, 1, 0)

当我打印那个 one-hot 编码时,它的尺寸看起来是正确的,因为我有 114171 个训练示例和 829 个唯一 ID:

Tensor("one_hot:0", shape=(114171, 829), dtype=int32)

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    定义的张量消耗大量内存。 tf.GraphDef 协议缓冲区有 2GB 的限制。您应该使用较小的批次来训练您的模型。有一个很好的更高级别的 Estimator API 可以为pandas 数据帧构建input_fn

    input_fn = tf.estimator.inputs.pandas_input_fn(
      x=pd.DataFrame({'x':x_data}),
      num_epochs=num_epochs,
      shuffle=True)
    

    有关更多详细信息,您可以找到文档here

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 2017-08-15
      • 2019-05-03
      • 1970-01-01
      相关资源
      最近更新 更多