Python 如何在tensorflow中创建要素列?

Python 如何在tensorflow中创建要素列?,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,我正在按照tensorflow文档进行feature_专栏的构建,但是我遇到了一些问题,有人能帮我吗?谢谢!下面是我的代码: #数据是列车数据集的字典 def demo(feature_column): feature_layer = layers.DenseFeatures(feature_column) return(feature_layer(data).numpy()) age = feature_column.numeric_column('Age') age_buck

我正在按照tensorflow文档进行feature_专栏的构建,但是我遇到了一些问题,有人能帮我吗?谢谢!下面是我的代码:

#数据是列车数据集的字典

def demo(feature_column):
    feature_layer = layers.DenseFeatures(feature_column)
    return(feature_layer(data).numpy())
age = feature_column.numeric_column('Age')
age_bucket = feature_column.bucketized_column(age, boundaries = [30, 40, 50, 60, 70, 80, 90])
age_fea = demo(age_bucket) #create age feature_column
gender = feature_column.categorical_column_with_vocabulary_list('Gender', ['F', 'M'])
gender_one_hot = feature_column.indicator_column(gender) #create gender feature_column
smoking = feature_column.categorical_column_with_vocabulary_list('Smoking', ['Y', 'N'])
smoking_one_hot = feature_column.indicator_column(smoking) #create smoking feature_column

#create the estimator
    model = tf.estimator.LinearClassifier( 
      n_classes = 2,
      model_dir = "ongoing/train6 ",
      feature_columns = [age_fea, gender_fea, icd9code_fea, smoking_fea]
    )
#训练估计员

  model.train( 
      input_fn = get_input_fn(csv_data, 5000, 10, True),
      steps = 1000
    )
并在下面列出了一个错误:

    (base) ous-MacBook-Pro:indiproject_copy congwang$ python try.py
2021-05-12 01:12:32.901336: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-05-12 01:12:32.901558: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING:tensorflow:From /Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/lazy_loader.py:63: The name tf.estimator.inputs is deprecated. Please use tf.compat.v1.estimator.inputs instead.

WARNING:tensorflow:From try.py:79: The name tf.estimator.inputs.pandas_input_fn is deprecated. Please use tf.compat.v1.estimator.inputs.pandas_input_fn instead.

WARNING:tensorflow:From /Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/training/training_util.py:235: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.
WARNING:tensorflow:From /Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/inputs/queues/feeding_queue_runner.py:60: QueueRunner.__init__ (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
WARNING:tensorflow:From /Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/inputs/queues/feeding_functions.py:491: add_queue_runner (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
Traceback (most recent call last):
  File "try.py", line 106, in <module>
    model.train(
  File "/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 349, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1175, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1203, in _train_model_default
    estimator_spec = self._call_model_fn(features, labels, ModeKeys.TRAIN,
  File "/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1163, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/canned/linear.py", line 982, in _model_fn
    return _linear_model_fn(
  File "/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/canned/linear.py", line 744, in _linear_model_fn
    logits = logit_fn(features=features)
  File "/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/canned/linear.py", line 429, in linear_logit_fn
    linear_model = feature_column._LinearModel(  # pylint: disable=protected-access
  File "/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/feature_column/feature_column.py", line 639, in __init__
    self._feature_columns = _normalize_feature_columns(
  File "/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/feature_column/feature_column.py", line 2333, in _normalize_feature_columns
    raise ValueError('Items of feature_columns must be a _FeatureColumn. '
ValueError: Items of feature_columns must be a _FeatureColumn. Given (type <class 'numpy.ndarray'>): [[0. 0. 0. ... 0. 0. 1.]
 [0. 0. 1. ... 0. 0. 0.]
 [1. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 1. 0. 0.]
 [0. 1. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 1. 0. 0.]].
(基本)ous MacBook Pro:indiproject\u copy congwang$python try.py
2021-05-121:1:12: 32.901336:I TysFult/Cys/JIT/XLAYCPUIONE.CC:41)不创建XLA设备,TFXXLANEABLE LXLAX设备未设置
2021-05-12 01:12:32.901558:I tensorflow/core/platform/cpu_feature_guard.cc:142]此tensorflow二进制文件使用oneAPI深度神经网络库(oneDNN)进行优化,以便在性能关键型操作中使用以下cpu指令:AVX2 FMA
要在其他操作中启用它们,请使用适当的编译器标志重新生成TensorFlow。
警告:tensorflow:From/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow/python/util/lazy_loader.py:63:不推荐使用tf.estimator.inputs名称。请改用tf.compat.v1.estimator.inputs。
警告:tensorflow:From try.py:79:名称tf.estimator.inputs.pandas\u input\u fn已弃用。请改用tf.compat.v1.estimator.inputs.pandas\u input\u fn。
警告:tensorflow:From/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow/python/training/training_util.py:235:Variable.initialized_值(来自tensorflow.python.ops.variables)已弃用,将在未来版本中删除。
更新说明:
使用Variable.read\u值。在eager和graph(在tf.defun内)上下文中,2.X中的变量都会自动初始化。
警告:tensorflow:From/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow_estimator/python/estimator/inputs/queues/Feed_queue_runner.py:60:QueueRunner.u init_u(From tensorflow.python.training.queue_runner_impl)已被弃用,并将在未来版本中删除。
更新说明:
要构造输入管道,请使用'tf.data'模块。
警告:tensorflow:From/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow\u estimator/python/estimator/inputs/queues/feed\u functions.py:491:add\u queue\u runner(来自tensorflow.python.training.queue\u runner\u impl)已被弃用,并将在未来版本中删除。
更新说明:
要构造输入管道,请使用'tf.data'模块。
回溯(最近一次呼叫最后一次):
文件“try.py”,第106行,在
模型火车(
文件“/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow_estimator/python/estimator/estimator/estimator.py”,第349行,列车中
损失=自我训练模型(输入、挂钩、保存侦听器)
文件“/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow\u estimator/python/estimator/estimator.py”,第1175行,列车模型
返回self.\u train\u model\u default(输入\u fn、挂钩、保存\u侦听器)
文件“/Users/congwang/opt/anaconda3/lib/python3.8/site-packages/tensorflow\u-estimator/python/estimator/estimator.py”,第1203行,默认为列车模型
估计器\u spec=self.\u调用\u模型\u fn(特性、标签、模式键.TRAIN、,
文件“/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow\u estimator/python/estimator/estimator.py”,第1163行,在调用模型fn中
模型\结果=自身。\模型\结果(特征=特征,**kwargs)
文件“/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow_estimator/python/estimator/canted/linear.py”,第982行,in_model_fn
返回(线性)模型(fn)(
文件“/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow\u estimator/python/estimator/canted/linear.py”,第744行,在线性模型中
logits=logit\u fn(特征=特征)
文件“/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow_estimator/python/estimator/canted/linear.py”,第429行,以线性逻辑形式
线性模型=功能列。线性模型(#pylint:disable=受保护访问
文件“/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow/python/feature_column/feature_column.py”,第639行,在u_init中__
self.\u feature\u columns=\u normalize\u feature\u columns(
文件“/Users/congwang/opt/anaconda3/lib/python3.8/site packages/tensorflow/python/feature\u column/feature\u column.py”,第2333行,在“normalize\u feature\u columns”中
raise VALUETERROR('功能列的项目必须是功能列。'
ValueError:feature_列的项必须是_FeatureColumn。给定(类型):[[0.0.0…0.0.1]
[0. 0. 1. ... 0. 0. 0.]
[1. 0. 0. ... 0. 0. 0.]
...
[0. 0. 0. ... 1. 0. 0.]
[0. 1. 0. ... 0. 0. 0.]
[0. 0. 0. ... 1. 0. 0.]].

valueError中的列表是年龄fea,当我从estimator construction中删除年龄fea时,valueError列出了性别fea列表。由于演示返回的是numpy ndarray而不是张量,我对构建模型时在feature列中输入什么感到困惑。

始终输入完整的错误消息(从word开始)作为文本(不是截图,也不是指向外部门户的链接)的问题(不是评论)。还有其他有用的信息。在代码中,您应该使用带有
#
的注释,而不是
//
谢谢您的帮助!我已经对完整的错误消息提出了疑问,请看一看。我无法运行它,所以只能猜测:在
演示()
中,您可以
返回..numpy()
和此
.numpy()
会产生问题是的,所以我不知道在估算器构造步骤中应该将什么输入到feature_列中