tensorflow / adanet

Fast and flexible AutoML with learning guarantees.
https://adanet.readthedocs.io
Apache License 2.0
3.47k stars 529 forks source link

create_estimator_spec() missing 1 required positional argument: 'self' #132

Closed jeffltc closed 4 years ago

jeffltc commented 4 years ago

I run this code and come across create_estimator_spec() missing 1 required positional argument: 'self'

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import tensorflow as tf

import adanet

(x_train, y_train), (x_test, y_test) = (
    tf.keras.datasets.boston_housing.load_data())

def input_fn(partition):
    def _input_fn():
        feat_tensor_dict = {}
        if partition == 'train':
            x = x_train.copy()
            y = y_train.copy()
        else:
            x = x_test.copy()
            y = y_test.copy()
        for i in range(0,np.size(x,1)):
            feat_nam = ('feat'+str(i))
            feat_tensor_dict[feat_nam] = tf.convert_to_tensor(x[:,i], dtype=tf.float32)
        label_tensor = tf.convert_to_tensor(y, dtype=tf.float32)
        return (feat_tensor_dict,label_tensor)
    return _input_fn

feat_nam_lst = ['feat'+str(i) for i in range(0,np.size(x_train,1))]

feature_columns = []
for item in feat_nam_lst:
    feature_columns.append(tf.feature_column.numeric_column(item))

head = tf.estimator.RegressionHead

lr_estimator = tf.estimator.LinearEstimator(
                head = head,
                feature_columns = feature_columns
                )

dnn_estimator_1 = tf.estimator.DNNRegressor(
                feature_columns = feature_columns,
                hidden_units=[100, 500, 100])

dnn_estimator_2 = tf.estimator.DNNRegressor(
                feature_columns = feature_columns,
                hidden_units=[100, 500, 100])

estimator = adanet.AutoEnsembleEstimator(
    head=head,
    candidate_pool=lambda config: {
        "dnn1": dnn_estimator_1,
        "dnn2":dnn_estimator_2 },
    max_iteration_steps=100000)

estimator.train(input_fn=input_fn(partition = 'train'), steps=1000)
metrics = estimator.evaluate(input_fn=input_fn(partition = 'test'),steps = 1000)
cweill commented 4 years ago

@jeffltc: Replace head = tf.estimator.RegressionHead with head = tf.estimator.RegressionHead(1).

cweill commented 4 years ago

Here's a working script, you can run:

# Lint as: python3
import numpy as np
import tensorflow as tf

from absl import app
import adanet

def main(args):
  (x_train, y_train), (x_test, y_test) = (
      tf.keras.datasets.boston_housing.load_data())

  def input_fn(partition):

    def _input_fn():
      feat_tensor_dict = {}
      if partition == 'train':
        x = x_train.copy()
        y = y_train.copy()
      else:
        x = x_test.copy()
        y = y_test.copy()
      for i in range(0, np.size(x, 1)):
        feat_nam = ('feat' + str(i))
        feat_tensor_dict[feat_nam] = tf.convert_to_tensor(
            x[:, i], dtype=tf.float32)
      label_tensor = tf.convert_to_tensor(y, dtype=tf.float32)
      return (feat_tensor_dict, label_tensor)

    return _input_fn

  feat_nam_lst = ['feat' + str(i) for i in range(0, np.size(x_train, 1))]

  feature_columns = []
  for item in feat_nam_lst:
    feature_columns.append(tf.feature_column.numeric_column(item))

  head = tf.estimator.RegressionHead(1)

  lr_estimator = tf.estimator.LinearEstimator(
      head=head, feature_columns=feature_columns)

  dnn_estimator_1 = tf.estimator.DNNRegressor(
      feature_columns=feature_columns, hidden_units=[100, 500, 100])

  dnn_estimator_2 = tf.estimator.DNNRegressor(
      feature_columns=feature_columns, hidden_units=[100, 500, 100])

  config = tf.estimator.RunConfig(model_dir="/tmp/boston")
  estimator = adanet.AutoEnsembleEstimator(
      head=head,
      candidate_pool=lambda config: {
          'dnn1': dnn_estimator_1,
          'dnn2': dnn_estimator_2
      },
      max_iteration_steps=10000,
      config=config)

  estimator.train(input_fn=input_fn(partition='train'), steps=30000)
  metrics = estimator.evaluate(input_fn=input_fn(partition='test'), steps=1000)

if __name__ == "__main__":
  app.run(main)