我正在尝试使用我的一维数据集重新生成tensorflow教程代码 N_of_features = 3000 N_of_samples = 4500 它作为Matlab文件存在 应用此代码后,我收到了错误 我正在寻找解决这个问题的任何建议, 谢谢 问题主要存在于训练分类器
中from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import tensorflow as tf
import scipy.io
import pandas as pd
import tensorflow as tf
import numpy as np
file = "train.mat"
myfile1 = scipy.io.loadmat(file)
train=myfile1['train']
file = "test.mat"
myfile1 = scipy.io.loadmat(file)
test=myfile1['test']
train_np = np.asarray(train, np.float32)
test_np = np.asarray(test, np.float32)
test_np
n = train_np.shape[0]
p = train_np.shape[1]
train = dict(enumerate(train_np, 1))
test = dict(enumerate(test_np, 1))
train=pd.DataFrame.from_dict(train,orient='index')
test=pd.DataFrame.from_dict(test,orient='index')
def load_data(y_name=p-1):
train_x, train_y = train, train.pop(y_name)
test_x, test_y = test, test.pop(y_name)
train_y=train_y.astype(np.int64)
test_y = test_y.astype(np.int64)
return (train_x, train_y), (test_x, test_y)
def train_input_fn(features, labels, batch_size):
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
dataset = dataset.shuffle(1000).repeat().batch(batch_size)
return dataset
def eval_input_fn(features, labels, batch_size):
features=dict(features)
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
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', default=100, type=int, help='batch size')
parser.add_argument('--train_steps', default=1000, type=int,
help='number of training steps')
def main(argv):
args = parser.parse_args(argv[1:])
(train_x, train_y), (test_x, test_y) = load_data()
# 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=[10, 10],
# The model must choose between 2 classes.
n_classes=2)
# Train the Model.
classifier.train(
input_fn=lambda:train_input_fn(train_x, train_y,
args.batch_size),
steps=args.train_steps)
# Evaluate the model.
eval_result = classifier.evaluate(
input_fn=lambda:eval_input_fn(test_x, test_y,
args.batch_size))
print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(main)
我收到了这个错误
File "C:/Users/77781455/AppData/Local/Programs/Python/Python36/Scripts/models/samples /core/get_started/update_pre.py", line 138, in <module>
tf.app.run(main)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\platform\app.py", line 124, in run
_sys.exit(main(argv))
File "C:/Users/77781455/AppData/Local/Programs/Python/Python36/Scripts/models/samples/core/get_started/update_pre.py", line 124, in main
steps=args.train_steps)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 314, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 743, in _train_model
features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 725, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\dnn.py", line 324, in _model_fn
config=config)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\dnn.py", line 176, in _dnn_model_fn
logits = logit_fn(features=features, mode=mode)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\dnn.py", line 90, in dnn_logit_fn
features=features, feature_columns=feature_columns)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 280, in input_layer
trainable, cols_to_vars)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 193, in _internal_input_layer
None, default_name=column._var_scope_name): # pylint: disable=protected-access
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1929, in __enter__
unique_default_name = _get_unique_variable_scope(self._default_name)
File "C:\Users\77781455\AppData\Local\Programs\Python\Python36\lib\site- packages\tensorflow\python\ops\variable_scope.py", line 1656, in _get_unique_variable_scope
name = current_scope.name + "/" + prefix if current_scope.name else prefix
TypeError: must be str, not int
答案 0 :(得分:0)
对我来说这个问题的答案是将列名设置为字符串,因为它们默认为整数:
df.columns = df.columns.astype(str)
此处讨论:Tensorflow TypeError: Can't convert 'numpy.int64' object to str implicitly