【问题标题】:AttributeError: module 'tensorflow_estimator.python.estimator.api._v1.estimator' has no attribute 'inpus'AttributeError:模块'tensorflow_estimator.python.estimator.api._v1.estimator'没有属性'inpus'
【发布时间】:2021-04-23 09:58:34
【问题描述】:

我正在尝试使用线性分类器进行预测,这里列出了估计器的构造和训练:

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)

得到错误:

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

我的 tensorflow 版本是 2.4.1

你能帮我解决这个问题吗?谢了!

更新:我已经更正了拼写错误,并且该错误已得到修复,但我在此处列出了一个警告:

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

使用建议的功能后,我得到了相同的警告:

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

这真的让我很困惑,你能帮忙解决它吗?谢了!

我在谷歌驱动器上传了我的完整代码,这是这里的链接: https://drive.google.com/file/d/1R6bRcv8Afjx4cPLBZaBpuCcDg71fNN3Y/view?usp=sharing

【问题讨论】:

  • 你能把tf.estimator.inpus.numpy_input_fn改成tf.estimator.inputs.numpy_input_fn试试吗?这是拼写错误。
  • 我更正了这个错误并解决了错误,但出现了一个警告:'名称 tf.estimator.inputs.numpy_input_fn 已弃用。请改用 tf.compat.v1.estimator.inputs.numpy_input_fn。'当我使用这个建议的功能时,终端仍然显示相同的警告。你知道如何解决吗?谢了!
  • 你能告诉我们警告信息吗?您能否分享独立代码来复制您的问题,以便我们尝试帮助您。
  • 感谢您的建议,我在我的问题中添加了它,请看一下
  • 你能重启你的内核再试一次吗?您的代码在此处未完成,无法复制该问题。如果你能提供完整的代码就很容易调试了。

标签: python tensorflow tensorflow-estimator


【解决方案1】:

如果您将tf.estimator.inpus.numpy_input_fn 更改为tf.estimator.inputs.numpy_input_fn,您的问题就可以解决。这是拼写错误。

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

您可以忽略已弃用的警告。

【讨论】:

  • @RA.IBOY,如果它回答了您的问题,请接受并投票。谢谢!
猜你喜欢
  • 2022-11-28
  • 1970-01-01
  • 2018-04-14
  • 2019-02-18
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 2019-07-20
相关资源
最近更新 更多