TensorFlow 2.0:无法运行最小TF教程:TypeError:无法将int64转换为Tensor操作

2024-09-20 06:51:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我是机器学习的新手,我正努力按照本教程来掌握:

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

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import pandas as pd
from IPython.display import clear_output
from matplotlib import pyplot as plt

# Load dataset.
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 tf
tf.random.set_seed(123)

fc = tf.feature_column
CATEGORICAL_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:
  # Need to one-hot encode categorical features.
  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))

# Use entire batch since this is such a small dataset.
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)
    # For training, cycle thru dataset as many times as need (n_epochs=None).
    dataset = dataset.repeat(n_epochs)
    # In memory training doesn't use batching.
    dataset = dataset.batch(NUM_EXAMPLES)
    return dataset
  return input_fn

# Training and evaluation input functions.
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)

# Train model.
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.dll
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\bueny\AppData\Local\Temp\tmpqudxt4e5
WARNING: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.dll
2020-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.0375
pciBusID: 0000:02:00.0
2020-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: 0
WARNING: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 constructor
2020-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
2020-01-03 19:10:35.894359: 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.0375
pciBusID: 0000:02:00.0
2020-01-03 19:10:35.894607: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2020-01-03 19:10:35.898315: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2020-01-03 19:10:37.034419: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-01-03 19:10:37.034645: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0 
2020-01-03 19:10:37.034780: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N 
2020-01-03 19:10:37.035817: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1356 MB memory) -> physical GPU (device: 0, name: GeForce MX150, pci bus id: 0000:02:00.0, compute capability: 6.1)
Traceback (most recent call last):
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\client\session.py", line 305, in __init__
    fetch, allow_tensor=True, allow_operation=True))
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3607, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3699, in _as_graph_element_locked
    (type(obj).__name__, types_str))
TypeError: Can not convert a int64 into a Tensor or Operation.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/bueny/PycharmProjects/untitled3/MinimalLinearFile.py", line 58, in <module>
    linear_est.train(train_input_fn, max_steps=100)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 370, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1160, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1194, in _train_model_default
    saving_listeners)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1489, in _train_with_estimator_spec
    log_step_count_steps=log_step_count_steps) as mon_sess:
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\monitored_session.py", line 584, in MonitoredTrainingSession
    stop_grace_period_secs=stop_grace_period_secs)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\monitored_session.py", line 1014, in __init__
    stop_grace_period_secs=stop_grace_period_secs)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\monitored_session.py", line 725, in __init__
    self._sess = _RecoverableSession(self._coordinated_creator)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\monitored_session.py", line 1207, in __init__
    _WrappedSession.__init__(self, self._create_session())
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\monitored_session.py", line 1212, in _create_session
    return self._sess_creator.create_session()
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\monitored_session.py", line 885, in create_session
    hook.after_create_session(self.tf_sess, self.coord)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\basic_session_run_hooks.py", line 580, in after_create_session
    self._save(session, global_step)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\basic_session_run_hooks.py", line 611, in _save
    self._get_saver().save(session, self._save_path, global_step=step)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\saver.py", line 1149, in save
    global_step = training_util.global_step(sess, global_step)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\training\training_util.py", line 68, in global_step
    return int(sess.run(global_step_tensor))
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\client\session.py", line 956, in run
    run_metadata_ptr)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\client\session.py", line 1165, in _run
    self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\client\session.py", line 474, in __init__
    self._fetch_mapper = _FetchMapper.for_fetch(fetches)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\client\session.py", line 276, in for_fetch
    return _ElementFetchMapper(fetches, contraction_fn)
  File "C:\Users\bueny\.conda\envs\TensorFlow\lib\site-packages\tensorflow_core\python\client\session.py", line 309, in __init__
    (fetch, type(fetch), str(e)))
TypeError: Fetch argument 0 has invalid type <class 'numpy.int64'>, must be a string or Tensor. (Can not convert a int64 into a Tensor or Operation.)

事先非常感谢

本杰明


Tags: inpycorelibpackagestensorflowlinesite
1条回答
网友
1楼 · 发布于 2024-09-20 06:51:33

我能够从TF's Boosted_Trees Estimator example复制正确的结果,如下所示(没有对其代码进行任何修改):

enter image description here

我猜你可能会得到错误,主要是因为一个错误的TensorFlow版本(或其他依赖)安装。你知道吗

导入tensorflow后,在终端中运行tf.__version__,检查您正在使用的tensorflow的版本。你知道吗

希望这有帮助!你知道吗

相关问题 更多 >