使用形状 (2,2) 调用输入函数时出错

数据挖掘 机器学习 Python 神经网络 深度学习 张量流
2022-03-05 02:32:59

我是 TensorFlow 和机器学习的新手。

我正在尝试使用来自 Tensorflow 的高级 API。

请告诉我我做错了什么。

import tensorflow as tf
import numpy as np


features = np.array([[-1,-2],[1,2]],dtype='int32')
label = np.array([0,1],dtype='int32')

feature_columns = [tf.feature_column.numeric_column('features',shape=[2,2])]

model = tf.estimator.LinearClassifier(feature_columns=feature_columns,n_classes=2)

model.train(input_fn= tf.estimator.inputs.numpy_input_fn(x=features,y=label,shuffle=True))

我收到一个错误

ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.framework.ops.Tensor'>
2个回答

tf.estimator.inputs.numpy_input_fn() 中的 x, y 必须是数组或数组字典。引用链接(https://www.tensorflow.org/api_docs/python/tf/estimator/inputs/numpy_input_fn) - x:numpy 数组对象或 numpy 数组对象的字典。如果是数组,则该数组将被视为单个特征。

因此,在您的情况下,它将“功能”视为单个功能。你可以试试这个:

x1 = np.array([-1,-2])

x2 = np.array([1,2])

features = {'x1': x1, 'x2': x2}

我找到了我的问题的答案。我必须改变一行,一切正常

变化是

model.train(input_fn= tf.estimator.inputs.numpy_input_fn(x={'features' : features}, 
                                                         y=label,
                                                         shuffle=True))