AttributeError: module 'tensorflow_estimator.python.estimator.api._v1.estimator' has no attribute 'inpus'

11.2k views Asked by At

I am trying to use linear classifier to predict, the constructing and training of the estimator is listed here:

model = tf.estimator.LinearClassifier(
  n_classes = 2,
  model_dir = "ongoing",
  feature_columns = categorical_features + continuous_features
(
FEATURES = ['Age', 'Gender', 'ICD9Code']
LABEL = 'Condition'

def get_input_fn(data_set, num_epochs, n_batch, shuffle):
    input = tf.compat.v1.estimator.inputs.pandas_input_fn(
       x = pd.DataFrame({k: data_set[k].values for k in FEATURES}),
       y = pd.Series(data_set[LABEL].values),
       batch_size = n_batch,
       num_epochs = num_epochs,
       shuffle = shuffle
     )
     return input
model.train(
  input_fn = get_input_fn(csv_data, num_epochs = None, n_batch = 10461, shuffle = False
  ),
  steps = 1000
)
predict_data = pd.read_csv('feature_condition.csv', usecols = ['PatientGuid', 'Age', 'Gender', 'ICD9Code'], nrows = 5)
predict_input_fn = tf.estimator.inpus.numpy_input_fn(
                      x = {"x": predict_data},
                      y = None,
                      batch_size = 5,
                      shuffle = False,
                  num_threads = 5
                   )
predict_results = model.predict(predict_input_fn)
print(predict_results)

got the error:

AttributeError: module 'tensorflow_estimator.python.estimator.api._v1.estimator' has no attribute 'inpus'

my tensorflow version is 2.4.1

can you please help me to resolve this problem? THX!

update: I have already corrected the typo error, and the error has been fixed, but I got one warning listed here:

The name tf.estimator.inputs.numpy_input_fn is deprecated. Please use tf.compat.v1.estimator.inputs.numpy_input_fn instead.

after I used the suggested function, I got the same wanring listed here:

The name tf.estimator.inputs.numpy_input_fn is deprecated. Please use tf.compat.v1.estimator.inputs.numpy_input_fn instead

It really confused me, can you please help to fix it? THX!

I uploaded my complete code in google drive, this is the link here: https://drive.google.com/file/d/1R6bRcv8Afjx4cPLBZaBpuCcDg71fNN3Y/view?usp=sharing

1

There are 1 answers

1
AudioBubble On

Your issue can be resolved if you can change tf.estimator.inpus.numpy_input_fn to tf.estimator.inputs.numpy_input_fn. It's typo error.

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import json
import os
import numpy as np
import pandas as pd
from pandas.core.frame import DataFrame
from tensorflow.train import SequenceExample, FeatureLists
from tensorflow import feature_column
from tensorflow.keras import layers


csv_file = 'feature_condition.csv'
csv_data = pd.read_csv(csv_file, low_memory = False)
csv_df = pd.DataFrame(csv_data)

test_file = 'test.csv'
test_data = pd.read_csv(test_file, low_memory = False)
test_df = pd.DataFrame(test_data)



CONTI_FEATURES = ['Age']
CATE_FEATURES = ['Gender', 'ICD9Code']

# create the feature column:
continuous_features = [tf.feature_column.numeric_column(k) for k in CONTI_FEATURES]

categorical_features = [tf.feature_column.categorical_column_with_hash_bucket(k, hash_bucket_size = 1000) for k in CATE_FEATURES]


model = tf.estimator.LinearClassifier(
  n_classes = 2,
  model_dir = "ongoing",
  feature_columns = categorical_features + continuous_features
)

FEATURES = ['Age', 'Gender', 'ICD9Code']
LABEL = 'Condition'

# input function:
def get_input_fn(data_set, num_epochs, n_batch, shuffle):
    input = tf.compat.v1.estimator.inputs.pandas_input_fn(
       x = pd.DataFrame({k: data_set[k].values for k in FEATURES}),
       y = pd.Series(data_set[LABEL].values),
       batch_size = n_batch,
       num_epochs = num_epochs,
       shuffle = shuffle
     )
    return input
# train the model
model.train(
  input_fn = get_input_fn(csv_data, num_epochs = None, n_batch = 10461, shuffle = False
  ),
  steps = 1000
)

# iterate every data in test dataset and make a prediction:
row_pre = 0
for i in test_data.loc[:,'PatientGuid']:
    dict = {'Age': test_data.loc[row_pre]['Age'],
            'Gender': test_data.loc[row_pre]['Gender'],
            'ICD9Code': test_data.loc[row_pre]['ICD9Code'],
    }
    df = pd.DataFrame(dict, index = [1,2,3])
    predict_input_fn = tf.compat.v1.estimator.inputs.numpy_input_fn(
    #predict_input_fn = tf.estimator.inputs.numpy_input_fn(

                          x = {k: df[k].values for k in FEATURES},
                          y = None,
                          batch_size = 1,
                          num_epochs = 1,
                          shuffle = False,
                          num_threads = 1
                       )
    predict_results = model.predict(predict_input_fn)
    row_pre += 1

You can ignore deprecated warnings.