TensorFlow 2.0: 无法运行最简TF教程:TypeError: 无法将int64转换为Tensor或Operation

我是机器学习的新手,正在尝试通过这个教程来入门:

https://www.tensorflow.org/tutorials/estimator/boosted_trees

from __future__ import absolute_import, division, print_function, unicode_literalsimport numpy as npimport pandas as pdfrom IPython.display import clear_outputfrom matplotlib import pyplot as plt# 加载数据集。dftrain = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/train.csv')dfeval = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/eval.csv')y_train = dftrain.pop('survived')y_eval = dfeval.pop('survived')import tensorflow as tftf.random.set_seed(123)fc = tf.feature_columnCATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck',                       'embark_town', 'alone']NUMERIC_COLUMNS = ['age', 'fare']def one_hot_cat_column(feature_name, vocab):  return tf.feature_column.indicator_column(      tf.feature_column.categorical_column_with_vocabulary_list(feature_name,                                                 vocab))feature_columns = []for feature_name in CATEGORICAL_COLUMNS:  # 需要对分类特征进行独热编码。  vocabulary = dftrain[feature_name].unique()  feature_columns.append(one_hot_cat_column(feature_name, vocabulary))for feature_name in NUMERIC_COLUMNS:  feature_columns.append(tf.feature_column.numeric_column(feature_name,                                           dtype=tf.float32))# 使用整个批次,因为数据集非常小。NUM_EXAMPLES = len(y_train)def make_input_fn(X, y, n_epochs=None, shuffle=True):  def input_fn():    dataset = tf.data.Dataset.from_tensor_slices((dict(X), y))    if shuffle:      dataset = dataset.shuffle(NUM_EXAMPLES)    # 对于训练,需要多次循环数据集(n_epochs=None)。    dataset = dataset.repeat(n_epochs)    # 内存训练不使用批处理。    dataset = dataset.batch(NUM_EXAMPLES)    return dataset  return input_fn# 训练和评估输入函数。train_input_fn = make_input_fn(dftrain, y_train)eval_input_fn = make_input_fn(dfeval, y_eval, shuffle=False, n_epochs=1)linear_est = tf.estimator.LinearClassifier(feature_columns)# 训练模型。linear_est.train(train_input_fn, max_steps=100)

我删除了大部分不必要的代码,以便使其更加简洁。

一切正常,直到我调用linear_est.train(train_input_fn, max_steps=100)函数。之后,我收到了以下错误消息。请原谅我提供了一个巨大的错误代码块,因为我不知道哪部分是重要的。

2020-01-03 19:10:31.309875: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dllWARNING:tensorflow:Using temporary folder as model directory: C:\Users\bueny\AppData\Local\Temp\tmpqudxt4e5WARNING:tensorflow:From C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.Instructions for updating:If using Keras pass *_constraint arguments to layers.WARNING:tensorflow:From C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\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.2020-01-03 19:10:33.796478: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll2020-01-03 19:10:34.518560: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: name: GeForce MX150 major: 6 minor: 1 memoryClockRate(GHz): 1.0375pciBusID: 0000:02:00.02020-01-03 19:10:34.518746: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.2020-01-03 19:10:34.519194: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0WARNING:tensorflow:Layer linear/linear_model is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2.  The layer has dtype float32 because it's dtype defaults to floatx.If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.WARNING:tensorflow:From C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\feature_column\feature_column_v2.py:518: Layer.add_variable (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.Instructions for updating:Please use `layer.add_weight` method instead.WARNING:tensorflow:From C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\feature_column\feature_column_v2.py:4276: IndicatorColumn._variable_shape (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.Instructions for updating:The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.WARNING:tensorflow:From C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\feature_column\feature_column_v2.py:4331: VocabularyListCategoricalColumn._num_buckets (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.Instructions for updating:The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.WARNING:tensorflow:From C:\Users\bueny\.conda\envs\TensorFlow\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\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\ftrl.py:143: 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 constructor2020-01-03 19:10:35.759525: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

提前感谢您的帮助,

Benjamin


回答:

我能够按照以下方式重现TF的Boosted_Trees估计器示例的正确结果(未对其代码进行任何修改):

enter image description here

我猜您可能主要是因为安装了错误版本的TensorFlow(或其他依赖项)而遇到错误。

通过在终端中导入TensorFlow后运行tf.__version__来检查您使用的TensorFlow版本。

希望这对您有帮助!

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注