gpt4 book ai didi

python - tensorflow 类型错误 : Can't convert 'numpy.int64' object to str implicitly

转载 作者:行者123 更新时间:2023-11-30 22:17:30 25 4
gpt4 key购买 nike

这是我的 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

最佳答案

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

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

关于python - tensorflow 类型错误 : Can't convert 'numpy.int64' object to str implicitly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49632451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com