训练tfrecords文件,出现ValueError错误
- Python版本3.6.4,tensorflow版本1.6.0;
- 我自己直接用tensorflow的dataset读取csv文件拿来训练的话,不出现问题;
- 但是我尝试着将csv文件制作成tfrecords文件,然后读入tfrecords文件的话,会出现
ValueError: features should be a dictionary of Tensors. Given type: <class 'tensorflow.python.framework.ops.Tensor'>
错误; - 我谷歌了一整天,很多人说是tensorflow版本问题,但是我已经升级到了最新的版本。
所以现在请各位帮忙看看是不是有以下问题:
- csv文件制作成tfrecords文件错误;
- 读取制作完的tfrecords文件错误;
运行错误提示如下:
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': 'iris_model_2', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x120f15eb8>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
INFO:tensorflow:Calling model_fn.
Traceback (most recent call last):
File "/Users/huanghelin/Desktop/TFrecord/try2.py", line 45, in <module>
classifier.train(input_fn=lambda: my_input_fn(is_shuffle=True, repeat_count=100))
File "/Users/huanghelin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 352, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "/Users/huanghelin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 812, in _train_model
features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
File "/Users/huanghelin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 793, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "/Users/huanghelin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/estimator/canned/dnn.py", line 354, in _model_fn
config=config)
File "/Users/huanghelin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/estimator/canned/dnn.py", line 161, in _dnn_model_fn
'Given type: {}'.format(type(features)))
ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.framework.ops.Tensor'>
制作代码代码如下:
import tensorflow as tf
import pandas as pd
tf_name = 'csv.tfrecords'
csv = pd.read_csv("iris_test.csv").values
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _float_feature(value):
# 本来传入的就是个list,所以value不用加[]了
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
with tf.python_io.TFRecordWriter(tf_name) as writer:
for row in csv:
features, label = row[:-1], int(row[-1])
features = [float(f) for f in features]
example = tf.train.Example()
example = tf.train.Example(
features=tf.train.Features(
feature={
'label': _int64_feature(label),
'features': _float_feature(features)
}
)
)
writer.write(example.SerializeToString())
print('转化%s完成' % tf_name)
拿去训练的代码:
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)
feature_names = [
'SepalLength',
'SepalWidth',
'PetalLength',
'PetalWidth'
]
def my_input_fn(is_shuffle=False, repeat_count=1):
dataset = tf.data.TFRecordDataset(['csv.tfrecords']) # filename得是个list
def parser(record):
keys_to_features = {
'label': tf.FixedLenFeature((), dtype=tf.int64),
'features': tf.FixedLenFeature(shape=(4,), dtype=tf.float32),
}
parsed = tf.parse_single_example(record, keys_to_features)
return parsed['features'], parsed['label']
dataset = dataset.map(parser)
# 打乱顺序,buffer_size设置成一个大于数据集中样本数量的值来确保其充分打乱
if is_shuffle:
# Randomizes input using a window of 256 elements (read into memory)
dataset = dataset.shuffle(buffer_size=256)
# 打包成多少个进行输出
dataset = dataset.batch(32)
# 指定要遍历几遍整个数据集
dataset = dataset.repeat(repeat_count)
iterator = dataset.make_one_shot_iterator()
features, labels = iterator.get_next()
return features, labels
feature_columns = [tf.feature_column.numeric_column(k) for k in feature_names]
classifier = tf.estimator.DNNClassifier(
feature_columns=feature_columns, # The input features to our model
hidden_units=[10, 10], # Two layers, each with 10 neurons
n_classes=3,
model_dir='iris_model_2') # Path to where checkpoints etc are stored
classifier.train(input_fn=lambda: my_input_fn(is_shuffle=True, repeat_count=100))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论