【问题标题】:ValueError from tensorflow estimator RNNClassifier with gcloud ml-engine job来自带有 gcloud ml-engine 作业的 tensorflow 估计器 RNNClassifier 的 ValueError
【发布时间】:2019-04-01 15:46:54
【问题描述】:

我正在处理 task.py 文件以提交 gcloud MLEngine 作业。以前我使用 tensorflow.estimator.DNNClassifier 成功地使用我的数据提交作业(仅包含 8 列用于加密货币价格和数量的连续数字数据;没有分类)。

我现在已经切换到 tensorflow 贡献者估计器 RNNClassifier。这是我当前相关部分的代码:

def get_feature_columns():
  return [
      tf.feature_column.numeric_column(feature, shape=(1,))
      for feature in column_names[:len(column_names)-1]
  ]

def build_estimator(config, learning_rate, num_units):
  return tf.contrib.estimator.RNNClassifier(
    sequence_feature_columns=get_feature_columns(),
    num_units=num_units,
    cell_type='lstm',
    rnn_cell_fn=None,
    optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate),
    config=config)

estimator = build_estimator(
    config=run_config,
    learning_rate=args.learning_rate,
    num_units=[32, 16])

tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

但是,我收到以下 ValueError:

ValueError: All feature_columns must be of type _SequenceDenseColumn. You can wrap a sequence_categorical_column with an embedding_column or indicator_column. Given (type <class 'tensorflow.python.feature_column.feature_column_v2.NumericColumn'>): NumericColumn(key='LTCUSD_close', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)

我不明白,因为数据不是分类的。

【问题讨论】:

    标签: tensorflow lstm gcloud recurrent-neural-network tensorflow-estimator


    【解决方案1】:

    正如@Ben7 指出的sequence_feature_columns 接受sequence_numeric_column 之类的列。但是,根据文档,RNNClassifier sequence_feature_columns 期望 SparseTensors 而 sequence_numeric_column 是密集张量。这似乎是矛盾的。

    这是我用来解决此问题的解决方法(我从this answer 获取了 to_sparse_tensor 函数):

    def to_sparse_tensor(dense):
    
        # sequence_numeric_column default is float32
        zero = tf.constant(0.0, dtype=tf.dtypes.float32) 
    
        where = tf.not_equal(dense, zero)
        indices = tf.where(where)
        values = tf.gather_nd(dense, indices)
    
        return tf.SparseTensor(indices, values, tf.shape(dense, out_type=tf.dtypes.int64))
    
    def get_feature_columns():
      return [
          tf.feature_column.sequence_numeric_column(feature, shape=(1,), normalizer_fn=to_sparse_tensor)
          for feature in column_names[:len(column_names)-1]
      ]
    

    【讨论】:

      【解决方案2】:

      您收到此错误是因为您使用了数字特征列,而这种估计器只能接受序列特征列,正如您在 init function 上看到的那样。

      因此,您必须使用sequence_numeric_column,而不是使用数字列。

      【讨论】:

      • tf.feature_column.numeric_column 更改为 tf.contrib.feature_column.sequence_numeric_column 会导致:“TypeError: Input must be a SparseTensor.”
      猜你喜欢
      • 2019-06-02
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多