Tensorflow TypeError:无法将'numpy.int64'对象隐式转换为str

Posted

技术标签:

【中文标题】Tensorflow TypeError:无法将\'numpy.int64\'对象隐式转换为str【英文标题】:Tensorflow TypeError: Can't convert 'numpy.int64' object to str implicitlyTensorflow TypeError:无法将'numpy.int64'对象隐式转换为str 【发布时间】:2018-09-12 22:05:08 【问题描述】:

这是我的 jupyter 笔记本:

import pandas as pd
from pprint import pprint
import pickle 
import numpy as np

with open('preDF.p', 'rb') as f:
    preDF = pickle.load(f)
#pprint(preDF)
df = pd.DataFrame(data=preDF)
#df.rename(columns=166: '166', inplace=True)
df.head()
0 1   2   3   4   5   6   7   8   9   ... 157 158 159 160 161 162 163 164 165 166
0 3   8   1   13  15  13  9   12  12  1   ... 0   0   0   0   0   0   0   0   0   1
1 3   1   13  15  13  9   12  12  1   27  ... 0   0   0   0   0   0   0   0   0   1
2 3   8   1   13  15  13  9   12  12  1   ... 0   0   0   0   0   0   0   0   0   1
3 13  5   20  18  9   3   1   18  9   1   ... 0   0   0   0   0   0   0   0   0   1
4 3   8   12  15  18  8   5   24  9   4   ... 0   0   0   0   0   0   0   0   0   2
5 rows × 167 columns
import numpy as np 
#msk = np.random.rand(len(df)) < 0.8
#train = df[msk]
#test = df[~msk]

from sklearn.model_selection import KFold
kf = KFold(n_splits=2)
train = df.iloc[train_index]
test = df.iloc[test_index]
train.columns = train.columns.astype(np.int32)
test.columns = test.columns.astype(np.int32)


import tensorflow as tf

def train_input_fn(features, labels, batch_size):
    """An input function for training"""
    # Convert the inputs to a Dataset.
    dataset = tf.data.Dataset.from_tensor_slices((dict(features.astype(np.int32)), labels.astype(np.int32)))

    # Shuffle, repeat, and batch the examples.
    dataset = dataset.shuffle(1000).repeat().batch(batch_size)

    # Return the dataset.
    return dataset


def eval_input_fn(features, labels, batch_size):
    """An input function for evaluation or prediction"""
    features=dict(features.astype(np.int32))
    if labels is None:
        # No labels, use only features.
        inputs = features
    else:
        inputs = (features, labels)

    # Convert the inputs to a Dataset.
    dataset = tf.data.Dataset.from_tensor_slices(inputs)

    # Batch the examples
    assert batch_size is not None, "batch_size must not be None"
    dataset = dataset.batch(batch_size)

    # Return the dataset.
    return dataset

def load_data(train,test,y_name=166):

    train_x, train_y = train, train.pop(y_name)

    test_x, test_y = test, test.pop(y_name)

    return (train_x, train_y), (test_x, test_y)

def main(train,test):
    batch_size = np.int32(100)
    train_steps = np.int32(1000)
    # Fetch the data

    SPECIES = ['neg', 'stable', 'pos']
    (train_x, train_y), (test_x, test_y) = load_data(train,test)

    # Feature columns describe how to use the input.
    my_feature_columns = []
    for key in train_x.keys():
        my_feature_columns.append(tf.feature_column.numeric_column(key=key))

    # Build 2 hidden layer DNN with 10, 10 units respectively.
    classifier = tf.estimator.DNNClassifier(
        feature_columns=my_feature_columns,
        # Two hidden layers of 10 nodes each.
        hidden_units=[30, 10,30],
        # The model must choose between 3 classes.
        n_classes=3)

    classifier.train(
        input_fn=lambda:train_input_fn(train_x, train_y,
                                                 batch_size),
        steps=train_steps)
    # Evaluate the model.
    eval_result = classifier.evaluate(
        input_fn=lambda:eval_input_fn(test_x, test_y,
                                                batch_size))

    print('\nTest set accuracy: accuracy:0.3f\n'.format(**eval_result))

    # Generate predictions from the model
    expected = ['exp neg', 'exp stable', 'exp pos']
    predict_x = 
        'open': [5.1, 5.9, 6.9],
        'high': [3.3, 3.0, 3.1],
        'low':   [1.7, 4.2, 5.4],
        'close': [0.5, 1.5, 2.1],
    

    predictions = classifier.predict(
        input_fn=lambda:eval_input_fn(predict_x,
                                                labels=None,
                                                batch_size=batch_size))

    template = ('\nPrediction is "" (:.1f%), expected ""')

    for pred_dict, expec in zip(predictions, expected):
        class_id = pred_dict['class_ids'][0]
        probability = pred_dict['probabilities'][class_id]

        print(template.format(SPECIES[class_id],
                              100 * probability, expec))


if __name__ == '__main__':
    #tf.logging.set_verbosity(tf.logging.INFO)
    tf.app.run(main(train,test))

所以我得到这个错误:

INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpz7rw1puj
INFO:tensorflow:Using config: '_task_type': 'worker', '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f478ba9bdd8>, '_tf_random_seed': None, '_keep_checkpoint_max': 5, '_is_chief': True, '_master': '', '_session_config': None, '_log_step_count_steps': 100, '_global_id_in_cluster': 0, '_evaluation_master': '', '_service': None, '_save_summary_steps': 100, '_save_checkpoints_secs': 600, '_num_ps_replicas': 0, '_task_id': 0, '_num_worker_replicas': 1, '_model_dir': '/tmp/tmpz7rw1puj', '_save_checkpoints_steps': None, '_keep_checkpoint_every_n_hours': 10000
INFO:tensorflow:Calling model_fn.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-141-fcd417d2c3ff> in <module>()
     98 if __name__ == '__main__':
     99     #tf.logging.set_verbosity(tf.logging.INFO)
--> 100     tf.app.run(main(train,test))

<ipython-input-141-fcd417d2c3ff> in main(train, test)
     64         input_fn=lambda:train_input_fn(train_x, train_y,
     65                                                  batch_size),
---> 66         steps=train_steps)
     67     # Evaluate the model.
     68     eval_result = classifier.evaluate(

/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py in train(self, input_fn, hooks, steps, max_steps, saving_listeners)
    350 
    351     saving_listeners = _check_listeners_type(saving_listeners)
--> 352     loss = self._train_model(input_fn, hooks, saving_listeners)
    353     logging.info('Loss for final step: %s.', loss)
    354     return self

/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py in _train_model(self, input_fn, hooks, saving_listeners)
    810       worker_hooks.extend(input_hooks)
    811       estimator_spec = self._call_model_fn(
--> 812           features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
    813 
    814       if self._warm_start_settings:

/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py in _call_model_fn(self, features, labels, mode, config)
    791 
    792     logging.info('Calling model_fn.')
--> 793     model_fn_results = self._model_fn(features=features, **kwargs)
    794     logging.info('Done calling model_fn.')
    795 

/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/canned/dnn.py in _model_fn(features, labels, mode, config)
    352           dropout=dropout,
    353           input_layer_partitioner=input_layer_partitioner,
--> 354           config=config)
    355 
    356     super(DNNClassifier, self).__init__(

/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/canned/dnn.py in _dnn_model_fn(features, labels, mode, head, hidden_units, feature_columns, optimizer, activation_fn, dropout, input_layer_partitioner, config)
    183         dropout=dropout,
    184         input_layer_partitioner=input_layer_partitioner)
--> 185     logits = logit_fn(features=features, mode=mode)
    186 
    187     def _train_op_fn(loss):

/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/canned/dnn.py in dnn_logit_fn(features, mode)
     89         partitioner=input_layer_partitioner):
     90       net = feature_column_lib.input_layer(
---> 91           features=features, feature_columns=feature_columns)
     92     for layer_id, num_hidden_units in enumerate(hidden_units):
     93       with variable_scope.variable_scope(

/usr/local/lib/python3.5/dist-packages/tensorflow/python/feature_column/feature_column.py in input_layer(features, feature_columns, weight_collections, trainable, cols_to_vars)
    271   """
    272   return _internal_input_layer(features, feature_columns, weight_collections,
--> 273                                trainable, cols_to_vars)
    274 
    275 

/usr/local/lib/python3.5/dist-packages/tensorflow/python/feature_column/feature_column.py in _internal_input_layer(features, feature_columns, weight_collections, trainable, cols_to_vars, scope)
    192       ordered_columns.append(column)
    193       with variable_scope.variable_scope(
--> 194           None, default_name=column._var_scope_name):  # pylint: disable=protected-access
    195         tensor = column._get_dense_tensor(  # pylint: disable=protected-access
    196             builder,

/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py in __enter__(self)
   1901 
   1902     try:
-> 1903       return self._enter_scope_uncached()
   1904     except:
   1905       if self._graph_context_manager is not None:

/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py in _enter_scope_uncached(self)
   2006         raise
   2007       self._current_name_scope = current_name_scope
-> 2008       unique_default_name = _get_unique_variable_scope(self._default_name)
   2009       pure_variable_scope = _pure_variable_scope(
   2010           unique_default_name,

/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py in _get_unique_variable_scope(prefix)
   1690   var_store = _get_default_variable_store()
   1691   current_scope = get_variable_scope()
-> 1692   name = current_scope.name + "/" + prefix if current_scope.name else prefix
   1693   if var_store.variable_scope_count(name) == 0:
   1694     return prefix

TypeError: Can't convert 'numpy.int64' object to str implicitly

我的猜测是,这在没有调用 numpy 作为简单示例的情况下有效。

现在我已经调用了 numpy,每个 int 都是一个 int64,似乎 tensorflow 尝试非常简单地将一个 int 转换为字符串。

但是由于将 int64 转换为字符串并不是那么简单,所以它失败了,因为现在所有 int 默认都是 int64。

但是我有一些问题要在这里找到哪个 int 有问题。

笔记本在这里: https://www.dropbox.com/s/rx8v5aap3zhoshm/NewML.html?dl=1 泡菜predf在这里: https://www.dropbox.com/s/wd831906jq3o1jl/preDF.p?dl=1

【问题讨论】:

如果您尝试在此处创建minimal reproducible example,将会很有帮助。这意味着尝试从您的数据和代码中删除显示相同问题的内容(增量),直到您无法再删除任何内容。删除东西并不一定意味着只是删除行,而是用“存根数据”和/或常量替换不必要的调用。通常,脚本和数据不超过 10-20 行,您会得到更好的答案(或任何答案),而且问题对未来的访问者也更有帮助。 您笔记本中的第二个单元格缺少变量?你能提供一个有效的笔记本吗?还可以尝试在第一个单元格中加载数据框后添加df.columns = df.columns.astype(str),看看它是否解决了问题 【参考方案1】:

将列名更新为字符串并尝试。

df.columns = df.columns.astype(str)

【讨论】:

是的,谢谢 :) 我自己解决了这个问题,但承诺会提供赏金,所以请尽情享受并感谢 :)

以上是关于Tensorflow TypeError:无法将'numpy.int64'对象隐式转换为str的主要内容,如果未能解决你的问题,请参考以下文章

在 tensorflow 2.4 中使用 sampled_softmax 时无法将符号 Keras 输入/输出转换为 numpy 数组 TypeError

TensorFlow:创建混淆矩阵时无法将图像转换为浮点数

Tensorflow - ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型浮点数)

pickle.dump(模型,pickle_out)| TypeError:无法腌制 _thread._local 对象

构建神经网络 [TensorFlow 2.0] 模型子类化 - ValueError/TypeError

导入 TensorFlow 时出错。 TypeError:预期字节,找到描述符