问题是,在尝试训练线性回归模型时,我总是会遇到相同的错误吗?我的数据集有问题吗?
训练数据集:https://www.mediafire.com/file/mc1ukij1rp2kkcz/FishTrain.csv/file
用于测试的数据集:https://www.mediafire.com/file/t0a75spn7xc9xsq/FishTest.csv/file
这是我的代码:
import numpy as np
import os
import tensorflow as tf
import matplotlib.pyplot as plt
df_train = pd.read_csv (r"C:\Users\femi0\Desktop\ccc\FishTrain.csv")
df_test = pd.read_csv (r"C:\Users\femi0\Desktop\ccc\FishTest.csv")
y_train = df_train.pop('Weight')
y_test = df_test.pop('Weight')
CATEGORICAL_COLUMNS = ['Species']
NUMERIC_COLUMNS = ['Length1', 'Length2', 'Length3', 'Height', 'Width']
feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
vocabulary = df_train[feature_name].unique() # gets a list of all unique values from given feature column
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))
print(df_test.loc[4])
def make_input_fn(data_df, label_df, num_epochs=13, 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) # split dataset into batches of 32 and repeat process for number of epochs
return ds # return a batch of the dataset
return input_function # return a function object for use
train_input_fn = make_input_fn(df_train, y_train) # here we will call the input_function that was returned to us to get a dataset object we can feed to the model
eval_input_fn = make_input_fn(df_test, y_test, 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 tetsing data
print(result)
这是错误代码:
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\femi0\AppData\Local\Temp\tmp8j262hhj
WARNING:tensorflow:From C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\training\training_util.py:236: 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:Entity <bound method LinearModel.call of <tensorflow.python.feature_column.feature_column_v2.LinearModel object at 0x00000211D73B1B38>> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: converting <bound method LinearModel.call of <tensorflow.python.feature_column.feature_column_v2.LinearModel object at 0x00000211D73B1B38>>: AssertionError: Bad argument number for Name: 3, expecting 4
WARNING:tensorflow:Entity <bound method _LinearModelLayer.call of <tensorflow.python.feature_column.feature_column_v2._LinearModelLayer object at 0x00000211D7440D30>> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: converting <bound method _LinearModelLayer.call of <tensorflow.python.feature_column.feature_column_v2._LinearModelLayer object at 0x00000211D7440D30>>: AssertionError: Bad argument number for Name: 3, expecting 4
WARNING:tensorflow:From C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\feature_column\feature_column_v2.py:2655: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
WARNING:tensorflow:From C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow_estimator\python\estimator\canned\linear.py:308: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.cast` instead.
WARNING:tensorflow:From C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\keras\optimizer_v2\ftrl.py:142: 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
2020-05-06 12:20:04.140136: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\client\session.py", line 1356, in _do_call
return fn(*args)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\client\session.py", line 1341, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\client\session.py", line 1429, in _call_tf_sessionrun
run_metadata)
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/ToFloat:0) = ] [[90][456][514]...] [y (head/losses/check_label_range/Const:0) = ] [1]
[[{{node head/losses/check_label_range/assert_less_equal/Assert/AssertGuard/Assert}}]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/femi0/PycharmProjects/tensorenv/test.py", line 43, in <module>
linear_est.train(train_input_fn) # train
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 367, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1158, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1192, in _train_model_default
saving_listeners)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1484, in _train_with_estimator_spec
_, loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss])
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\training\monitored_session.py", line 754, in run
run_metadata=run_metadata)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1252, in run
run_metadata=run_metadata)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1353, in run
raise six.reraise(*original_exc_info)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\six.py", line 703, in reraise
raise value
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1338, in run
return self._sess.run(*args, **kwargs)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1411, in run
run_metadata=run_metadata)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1169, in run
return self._sess.run(*args, **kwargs)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\client\session.py", line 950, in run
run_metadata_ptr)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\client\session.py", line 1173, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_run
run_metadata)
File "C:\Users\femi0\Anaconda2\envs\tensor\lib\site-packages\tensorflow\python\client\session.py", line 1370, in _do_call
raise type(e)(node_def, op, message)
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/ToFloat:0) = ] [[90][456][514]...] [y (head/losses/check_label_range/Const:0) = ] [1]
[[node head/losses/check_label_range/assert_less_equal/Assert/AssertGuard/Assert (defined at /Users/femi0/PycharmProjects/tensorenv/test.py:43) ]]
**