Nic*_*eph 16 python tensorflow
有一个关于加载稀疏数据的小片段,但我不知道如何使用它.
SparseTensors与队列不兼容.如果使用SparseTensors,则必须在批处理后使用tf.parse_example解码字符串记录(而不是在批处理之前使用tf.parse_single_example).
我想我真的不知道如何加载数据.
我想加载的数据是SVM Light格式
我想到的方法是将训练集转换为TFRecords文件格式,然后用tensorflow加载这个转换后的数据.问题是我不知道我应该如何格式化我的数据,以便tensorflow将其解析为sparseTensors.
下面是从选取的摘录一个 GitHub上可用的实施例:
def convert_to(images, labels, name):
num_examples = labels.shape[0]
if images.shape[0] != num_examples:
raise ValueError("Images size %d does not match label size %d." %
(images.shape[0], num_examples))
rows = images.shape[1]
cols = images.shape[2]
depth = images.shape[3]
filename = os.path.join(FLAGS.directory, name + '.tfrecords')
print('Writing', filename)
writer = tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
image_raw = images[index].tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(rows),
'width': _int64_feature(cols),
'depth': _int64_feature(depth),
'label': _int64_feature(int(labels[index])),
'image_raw': _bytes_feature(image_raw)}))
writer.write(example.SerializeToString())
writer.close()
Run Code Online (Sandbox Code Playgroud)
它将图像数据编码为一个大blob.与我的数据的不同之处在于并非每个功能都已填充.我可以以相同的方式保持我的数据,但我不确定这是使用这些功能的方式.
这可能无关紧要,因为另一方面我会解码一些东西但是有更好的方法来处理稀疏数据吗?
至于读数,这里是读取密集张量数据的一个例子.
我得到了,我是想交换tf.parse_single_example与tf.parse_example和配料后做.
但是,如何告诉tensorflow我的数据稀疏?如何将我的特征索引与张量中的特征值相关联?如何在加载数据之前进行批处理?
编辑1:
这是我试过的,我收到一个ValueError: Shape () must have rank 1错误:
from tqdm import *
def convert_to_tensor_file(path, out_file_name):
feature_set = set()
filename = os.path.join(FLAGS.directory, out_file_name + '.tfrecords')
writer = tf.python_io.TFRecordWriter(filename)
with open(path, 'r') as f:
for line in tqdm(f):
data = line.strip().split(' ')
features = {
"label": _int64_feature(int(data[0]))
}
for feature in data[1:]:
index, value = feature.split(':')
feature_set.add(index)
features[index] = _int64_feature(int(value))
example = tf.train.Example(features=tf.train.Features(feature=features))
writer.write(example.SerializeToString())
writer.close()
return feature_set
feature_set = convert_to_tensor_file(TRAIN, 'train')
def load_tensor_file(name):
filename = os.path.join(FLAGS.directory, name + '.tfrecords')
features = {
'label': tf.FixedLenFeature([], tf.int64),
}
for feature in feature_set:
features[feature] = tf.VarLenFeature(tf.int64)
with tf.name_scope('input'):
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_example(serialized_example, features=features)
load_tensor_file('train')
Run Code Online (Sandbox Code Playgroud)
谢谢,
ilb*_*gon 17
首先,解释一下该文档的含义:
对于密集数据,通常你正在做:
序列化示例(来自读者) - > parse_single_example- > batch queue- >使用它.
对于您目前需要执行的稀疏数据:
序列化示例(来自读者) - > batch queue- > parse_example- >使用它.
一个例子是:
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
batch_serialized_examples = tf.shuffle_batch([serialized_example], batch_size)
feature_to_type = {
'label': tf.FixedLenFeature([1], dtype=tf.int64),
'sparse_feature': tf.VarLenFeature(dtype=tf.int64)
}
features = tf.parse_example(batch_serialized_examples, feature_to_type)
Run Code Online (Sandbox Code Playgroud)
注意,shuffle_batch需要一系列字符串并返回一批字符串.label应该从你的例子中修复等级== 1的len.
将索引和值存储在TFRecords示例中,然后解析SparseFeature.例如,存储和加载稀疏表示:
[[0, 0, 0, 0, 0, 7],
[0, 5, 0, 0, 0, 0],
[0, 0, 0, 0, 9, 0],
[0, 0, 0, 0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)
这会创建一个TFRecords示例:
my_example = tf.train.Example(features=tf.train.Features(feature={
'index_0': tf.train.Feature(int64_list=tf.train.Int64List(value=[0, 1, 2])),
'index_1': tf.train.Feature(int64_list=tf.train.Int64List(value=[5, 1, 4])),
'values': tf.train.Feature(int64_list=tf.train.Int64List(value=[7, 5, 9]))
}))
my_example_str = my_example.SerializeToString()
Run Code Online (Sandbox Code Playgroud)
这解析它SparseFeature:
my_example_features = {'sparse': tf.SparseFeature(index_key=['index_0', 'index_1'],
value_key='values',
dtype=tf.int64,
size=[4, 6])}
serialized = tf.placeholder(tf.string)
parsed = tf.parse_single_example(serialized, features=my_example_features)
session.run(parsed, feed_dict={serialized: my_example_str})
## {'sparse': SparseTensorValue(indices=array([[0, 5], [1, 1], [2, 4]]),
## values=array([7, 5, 9]),
## dense_shape=array([4, 6]))}
Run Code Online (Sandbox Code Playgroud)
更多阐述:稀疏张量和TFRecords
| 归档时间: |
|
| 查看次数: |
10751 次 |
| 最近记录: |