gpt4 book ai didi

python - InvalidArgumentError : assertion failed: [Labels must be <= n_classes - 1] [Condition x <= y did not hold element-wise:] [x (head/losses/Cast:0) = ]

转载 作者:行者123 更新时间:2023-12-03 17:22:56 33 4
gpt4 key购买 nike

我一直在尝试将此ML线性模型实现到我的数据集中。 (https://www.tensorflow.org/tutorials/estimator/linear)
语言:Python 3.8.3
库:
TensorFlow 2.4.0
脾气暴躁:1.19.3
Pandas
Matplotliband其他人:

import os
import sys

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllib
import tensorflow.compat.v2.feature_column as fc
import tensorflow as tf
ss1517是我的数据集的名称。这是一个具有4116行和20列的CSV文件,具有很多NaN值(没有没有NaN值的列)
traindata = ss1517.iloc[0:2470,:] # 60 % of my dataset is splitted by training set
evaldata = ss1517.iloc[2470:4116, :] # 40 % of my dataset is splitted by eval set
ytrain = traindata.pop("AvgOfMajor N")
yeval = evaldata.pop("AvgOfMajor N")
CATEGORICAL_COLUMNS是我的数据集中的分类列。
NUMERIC_COLUMNS是我的数据集中的数字列。
CATEGORICAL_COLUMNS = ['Location-Name', 'Location-Code', 'Borough', 'Building-Name', 'Schools-in-Building', 'ENGroupA', 'RangeA']
NUMERIC_COLUMNS = ['Geographical-District-Code', 'Register', '#-Schools', 'Major-N', 'Oth-N', 'NoCrim-N', 'Prop-N', 'Vio-N', 'AvgOfOth-N', 'AvgOfNoCrim-N', 'AvgOfProp-N', 'AvgOfVio-N']

feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
vocabulary = traindata[feature_name].unique()
feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))
for feature_name in NUMERIC_COLUMNS:
feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))
def make_input_fn(data_df, label_df, num_epochs=10, shuffle=True, batch_size=32):
def input_function():# inner function, this will be returned.
ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df)) # Create tf.data.Dataset object with data and its label
if shuffle:
ds = ds.shuffle(1000) # randomize order of data
ds = ds.batch(batch_size).repeat(num_epochs)
return ds # return a batch of dataset
return input_function # return the input_function

train_input_fn = make_input_fn(traindata, ytrain)
eval_input_fn = make_input_fn(evaldata, yeval, num_epochs=1, shuffle=False)
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn) #train
result = linear_est.evaluate(eval_input_fn) #get model metrics/stats by testing on testing data

clear_output() #clears console output
print(result["accuracy"]) #the result variable is simply dict of stats about our model
我遇到此错误 InvalidArgumentError: assertion failed: [Labels must be <= n_classes - 1] [Condition x <= y did not hold element-wise:] [x (head/losses/Cast:0) = ] [[0.28][0.28][1.69]...] [y (head/losses/check_label_range/Const:0) = ] [1] 运行此单元格时的 :
linear_est.train(train_input_fn) #train
result = linear_est.evaluate(eval_input_fn) #get model metrics/stats by testing on testing data

clear_output() #clears console output
print(result["accuracy"]) #the result variable is simply dict
注意我在数据集(ss1517)上使用了 fillna(method="bfill") fillna(method="ffill)来填充Na值。
我该如何解决这个错误?

最佳答案

Tensorflow期望从0到类数的整数作为类标签(range(0, num_classes))
如果要保持标签不变,请将label_vocabulary参数添加到classifier-definition:

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns, label_vocabulary={add_list_of_labels_here})
label_vocabulary:

A list of strings represents possible label values. If given, labelsmust be string type and have any value in label_vocabulary. If it isnot given, that means labels are already encoded as integer or floatwithin [0, 1] for n_classes=2 and encoded as integer values in {0,1,..., n_classes-1} for n_classes>2 . Also there will be errors ifvocabulary is not provided and labels are string.

关于python - InvalidArgumentError : assertion failed: [Labels must be <= n_classes - 1] [Condition x <= y did not hold element-wise:] [x (head/losses/Cast:0) = ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65642746/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com