我创建一个线性回归模型,我正在收到一个错误

发布于 2025-02-09 02:59:25 字数 3331 浏览 1 评论 0原文

我正在创建一个线性回归模型,并使用了TensorFlow的线性估计器,但是运行线性估计器火车功能后,我收到一个无效的参数错误,该错误说标签必须为< = n_classes -1. 1.我不知道代码的哪一部分我错了,

这是我正在运行的代码

import tensorflow as tf 
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv(r"C:\Users\XPRESS\Downloads\CarPrice_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.

I was creating a linear regression model and I used TensorFlow's linear estimator but after I run the linear estimator train function I receive an invalid argument error which says Labels must be <= n_classes - 1.I don't know which part of the code i have gone wrong

this is the code i was running

import tensorflow as tf 
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv(r"C:\Users\XPRESS\Downloads\CarPrice_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)

this is the error i received

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2025-02-16 02:59:25

您提到您正在创建回归,但是在这里,您在代码中具有tf.estimator.linearclassifier。您可能是要使用tf.estimator.linearregressor

You mentioned that you are creating regression, but here you have tf.estimator.LinearClassifier in the code. May be you meant to use tf.estimator.LinearRegressor instead?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文