我正在创建一个线性回归模型,我使用TensorFlow的线性估计器,但在我运行线性估计器训练函数后,我收到一个无效的参数错误,说标签必须是<= n_classes - 1。我不知道代码的哪一部分出错了
这是我正在运行的代码
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv(r"C:UsersXPRESSDownloadsCarPrice_Assignment.csv") #load the data
data.head()
#split data into traiing and testing
from sklearn.model_selection import train_test_split
train , test = train_test_split(data,random_state=42,test_size=0.2)
train_x = train
train_y = train.pop('price')
eval_x = test
eval_y = test.pop('price')
lst = list(train_x.columns)
#get numerical and categorical columns
categorical_columns = []
numerical_columns = []
for cat in lst:
if train_x[cat].dtypes == 'object':
categorical_columns.append(_)
for nums in lst:
if nums not in categorical_columns:
numerical_columns.append(nums)
train_x.info()
#convert categorical data to numeric data
feature_columns = []
for feature_name in categorical_columns:
vocabulary = train_x[feature_name].unique()
feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name,vocabulary))
for feature_name in numerical_columns: feature_columns.append(tf.feature_column.numeric_column(feature_name,dtype=tf.float32))
def make_input_fn(data,label,num_epochs=10,shuffle=True,batch_size=32):
def input_fn():
ds = tf.data.Dataset.from_tensor_slices((dict(data),label))
if shuffle:
ds=ds.shuffle(1000)
ds = ds.batch(batch_size).repeat(num_epochs)
return ds
return input_fn
train_input_funtion = make_input_fn(train_x,train_y)
eval_input_function = make_input_fn(eval_x,eval_y,shuffle=False,num_epochs=1)
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_funtion)
这是我收到的错误
InvalidArgumentError: 2 root error(s) found.
(0) INVALID_ARGUMENT: assertion failed: [Labels must be <= n_classes - 1] [Condition x <= y did not hold element-wise:] [x (head/losses/Cast:0) = ] [[7895][10795][17710]...] [y (head/losses/check_label_range/Const:0) = ] [1]
[[{{function_node head_losses_check_label_range_assert_less_equal_Assert_AssertGuard_false_22323}}{{node Assert}}]]
[[training/Ftrl/gradients/gradients/linear/linear_model/linear/linear_model/linear/linear_model/enginelocation/weighted_sum_grad/Select_1/_1047]]
(1) INVALID_ARGUMENT: assertion failed: [Labels must be <= n_classes - 1] [Condition x <= y did not hold element-wise:] [x (head/losses/Cast:0) = ] [[7895][10795][17710]...] [y (head/losses/check_label_range/Const:0) = ] [1]
[[{{function_node head_losses_check_label_range_assert_less_equal_Assert_AssertGuard_false_22323}}{{node Assert}}]]
0 successful operations.
0 derived errors ignored.
...
[[training/Ftrl/gradients/gradients/linear/linear_model/linear/linear_model/linear/linear_model/enginelocation/weighted_sum_grad/Select_1/_1047]]
(1) INVALID_ARGUMENT: assertion failed: [Labels must be <= n_classes - 1] [Condition x <= y did not hold element-wise:] [x (head/losses/Cast:0) = ] [[7895][10795][17710]...] [y (head/losses/check_label_range/Const:0) = ] [1]
[[{{node Assert}}]]
0 successful operations.
0 derived errors ignored.
您提到您正在创建回归,但这里您在代码中有tf.estimator.LinearClassifier
。也许你打算用tf.estimator.LinearRegressor
代替?