首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow线性回归模型中的断言失败误差

Tensorflow线性回归模型中的断言失败误差
EN

Stack Overflow用户
提问于 2022-07-30 22:24:57
回答 1查看 55关注 0票数 1
代码语言:javascript
复制
# IMPORTS
import os
import numpy as np
import tensorflow as tf
import pandas as pd

# IMPORT CSV FILES AS PANDASDB
dfTrain = pd.read_csv('carPriceTrain.csv')
dfEval = pd.read_csv('carPriceEval.csv')

# RENAME COLUMN NAMES
dfTrain = dfTrain.rename(columns={"Gear box type": "gearBoxType"})
dfEval = dfTrain.rename(columns={"Gear box type": "gearBoxType"})
dfTrain = dfTrain.rename(columns={"Leather interior": "leatherInterior"})
dfEval = dfTrain.rename(columns={"Leather interior": "leatherInterior"})

# REMOVE EXTRANEOUS CAR INFORMATION
dfTrain.pop('ID')
dfTrain.pop('Levy')
dfTrain.pop('Category')
dfTrain.pop('Fuel type')
dfTrain.pop('Drive wheels')
dfTrain.pop('leatherInterior')
dfTrain.pop('gearBoxType')
dfTrain.pop('Doors')
dfTrain.pop('Wheel')
dfTrain.pop('Color')
dfTrain.pop('Airbags')

dfEval.pop('ID')
dfEval.pop('Levy')
dfEval.pop('Category')
dfEval.pop('Fuel type')
dfEval.pop('Drive wheels')
dfEval.pop('leatherInterior')
dfEval.pop('gearBoxType')
dfEval.pop('Doors')
dfEval.pop('Wheel')
dfEval.pop('Color')
dfEval.pop('Airbags')

# POP PRICE (PREDICTING VAL)
yTrain = dfTrain.pop('Price')
yEval = dfEval.pop('Price')

# CREATE COLUMNS NAMES
CATEGORICAL_COLUMNS = ['Manufacturer', 'Model']
NUMERIC_COLUMNS = ['Prod. year', 'Mileage', 'Cylinders', 'Engine volume']

# FEATURE COLUMNS FOR CATERGORICAL
featureColumns = []
for featureName in CATEGORICAL_COLUMNS:
  vocabulary = dfTrain[featureName].unique()
  featureColumns.append(tf.feature_column.categorical_column_with_vocabulary_list(featureName, vocabulary))

# FEATURE COLUMNS FOR NUMERIC
for featureName in NUMERIC_COLUMNS:
    featureColumns.append(tf.feature_column.numeric_column(featureName, dtype=tf.float32))

# CREATING INPUT FN
def makeInputFN(dfData, dfLabel, nEpochs=10, shuffle=True, batchSize=32):
  def inputFN():
    ds = tf.data.Dataset.from_tensor_slices((dict(dfData), dfLabel))
    if shuffle:
      ds = ds.shuffle(1000)
    ds = ds.batch(batchSize).repeat(nEpochs)
    return ds
  return inputFN

trainInputFN = makeInputFN(dfTrain, yTrain)
evalInputFN = makeInputFN(dfEval, yEval, nEpochs=1, shuffle=False)

linearEst = tf.estimator.LinearClassifier(feature_columns=featureColumns)

linearEst.train(trainInputFN)
result = linearEst.evaluate(evalInputFN)

我使用TensorFlow 2.0创建了一个线性回归模型。我导入了一个CSV文件,其中包含ID、Price、Levy、Prod、Model和Prod的数据。采购产品年份,类别,皮革内部,燃料类型,发动机体积,里程,气缸,变速箱型,驱动轮,门,车轮,颜色,安全气囊。我弹出了我不会从这些行中使用的行,然后继续这个过程。我遇到了这个错误:

代码语言:javascript
复制
WARNING:tensorflow:Using temporary folder as model directory: /var/folders/kq/823s5gqs0ds7wqckr9sxfcnm0000gn/T/tmp22bryhkx
WARNING:tensorflow:From /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/training/training_util.py:396: 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 /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/optimizers/optimizer_v2/ftrl.py:153: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
2022-07-30 15:22:47.534048: I tensorflow/core/platform/cpu_feature_guard.cc:193] 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.
2022-07-30 15:22:47.549419: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:354] MLIR V1 optimization pass is not enabled
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1377, in _do_call
    return fn(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1360, in _run_fn
    return self._call_tf_sessionrun(options, feed_dict, fetch_list,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1453, in _call_tf_sessionrun
    return tf_session.TF_SessionRun_wrapper(self._session, options, feed_dict,
tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [Labels must be <= n_classes - 1] [Condition x <= y did not hold element-wise:] [x (head/losses/Cast:0) = ] [[19444][56509][8781]...] [y (head/losses/check_label_range/Const:0) = ] [1]
         [[{{function_node head_losses_check_label_range_assert_less_equal_Assert_AssertGuard_false_667}}{{node Assert}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/anirud/CarPricePredictor/main.py", line 75, in <module>
    linearEst.train(trainInputFN)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 360, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1186, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1217, in _train_model_default
    return self._train_with_estimator_spec(estimator_spec, worker_hooks,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1533, in _train_with_estimator_spec
    _, loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss])
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/training/monitored_session.py", line 782, in run
    return self._sess.run(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/training/monitored_session.py", line 1311, in run
    return self._sess.run(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/training/monitored_session.py", line 1416, in run
    raise six.reraise(*original_exc_info)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/six.py", line 719, in reraise
    raise value
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/training/monitored_session.py", line 1401, in run
    return self._sess.run(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/training/monitored_session.py", line 1469, in run
    outputs = _WrappedSession.run(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/training/monitored_session.py", line 1232, in run
    return self._sess.run(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 967, in run
    result = self._run(None, fetches, feed_dict, options_ptr,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1190, in _run
    results = self._do_run(handle, final_targets, final_fetches,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1370, in _do_run
    return self._do_call(_run_fn, feeds, fetches, targets, options,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1396, in _do_call
    raise type(e)(node_def, op, message)  # pylint: disable=no-value-for-parameter
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

assertion failed: [Labels must be <= n_classes - 1] [Condition x <= y did not hold element-wise:] [x (head/losses/Cast:0) = ] [[19444][56509][8781]...] [y (head/losses/check_label_range/Const:0) = ] [1]
         [[{{node Assert}}]]

我不知道为什么会发生这个错误,也不知道该从哪里着手。

EN

回答 1

Stack Overflow用户

发布于 2022-07-31 08:40:13

我想问题就在这条线上:

代码语言:javascript
复制
linearEst = tf.estimator.LinearClassifier(feature_columns=featureColumns)

您应该根据n_classes的Tensorflow文档设置参数LinearClassifier。参见n_classes的论点:

标签类的数量。默认是二进制分类。注意,类标签是表示类索引的整数(即从0到n_class-1的值)。对于任意的标签值(例如字符串标签),首先转换为类索引。

因为默认的是二进制分类,所以应该设置类的数量。如果我正确理解你有六个类,那么你应该有:

代码语言:javascript
复制
linearEst = tf.estimator.LinearClassifier(feature_columns=featureColumns, n_classes=6)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73179438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档