【发布时间】:2017-09-15 07:38:28
【问题描述】:
简介:
我正在尝试用稀疏数据训练 tensorflow svm 估计器tensorflow.contrib.learn.python.learn.estimators.svm。在tensorflow/contrib/learn/python/learn/estimators/svm_test.py#L167 的github repo 中使用稀疏数据的示例(我不允许发布更多链接,所以这里是相对路径)。
支持向量机估计器期望作为参数example_id_column 和feature_columns,其中特征列应该派生自类FeatureColumn,例如tf.contrib.layers.feature_column.sparse_column_with_hash_bucket。请参阅tensorflow/contrib/learn/python/learn/estimators/svm.py#L85 上的 Github 存储库和python/contrib.layers#Feature_columns 上 tensorflow.org 上的文档。
问题:
- 如何设置输入管道以格式化稀疏数据,以便我可以使用 tf.contrib.layers 特征列之一作为 svm 估计器的输入。
- 具有许多特征的密集输入函数会是什么样子?
背景
我使用的数据是来自LIBSVM website 的a1a 数据集。该数据集有 123 个特征(如果数据密集,则对应于 123 个特征列)。我编写了一个用户操作来读取tf.decode_csv() 之类的数据,但使用的是 LIBSVM 格式。该操作将标签作为密集张量返回,将特征作为稀疏张量返回。我的输入管道:
NUM_FEATURES = 123
batch_size = 200
# my op to parse the libsvm data
decode_libsvm_module = tf.load_op_library('./libsvm.so')
def input_pipeline(filename_queue, batch_size):
with tf.name_scope('input'):
reader = tf.TextLineReader(name="TextLineReader_")
_, libsvm_row = reader.read(filename_queue, name="libsvm_row_")
min_after_dequeue = 1000
capacity = min_after_dequeue + 3 * batch_size
batch = tf.train.shuffle_batch([libsvm_row], batch_size=batch_size,
capacity=capacity,
min_after_dequeue=min_after_dequeue,
name="text_line_batch_")
labels, sp_indices, sp_values, sp_shape = \
decode_libsvm_module.decode_libsvm(records=batch,
num_features=123,
OUT_TYPE=tf.int64,
name="Libsvm_decoded_")
# Return the features as sparse tensor and the labels as dense
return tf.SparseTensor(sp_indices, sp_values, sp_shape), labels
Here 是带有batch_size = 5 的示例批处理。
def input_fn(dataset_name):
maybe_download()
filename_queue_train = tf.train.string_input_producer([dataset_name],
name="queue_t_")
features, labels = input_pipeline(filename_queue_train, batch_size)
return {
'example_id': tf.as_string(tf.range(1,123,1,dtype=tf.int64)),
'features': features
}, labels
这是我迄今为止尝试过的:
with tf.Session().as_default() as sess:
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
feature_column = tf.contrib.layers.sparse_column_with_hash_bucket(
'features', hash_bucket_size=1000, dtype=tf.int64)
svm_classifier = svm.SVM(feature_columns=[feature_column],
example_id_column='example_id',
l1_regularization=0.0,
l2_regularization=1.0)
svm_classifier.fit(input_fn=lambda: input_fn(TRAIN),
steps=30)
accuracy = svm_classifier.evaluate(
input_fn= lambda: input_fn(features, labels),
steps=1)['accuracy']
print(accuracy)
coord.request_stop()
coord.join(threads)
sess.close()
【问题讨论】:
-
看来您的方向是正确的。你有整数特征,对吗? sparse_column_with_integerized_feature 对你有用吗?
-
是的,事实上如果有一个值它就是一个'1',所以基本上是布尔特征。使用 sparse_column_with_integerized_feature 我得到一个形状错误
batch_size=200:InvalidArgumentError: Expected shape [200,4] for example_state_data, got [122,4](full traceback)
标签: tensorflow classification svm sparse-matrix