使用 TensorFlow 构建 SVM
Posted
技术标签:
【中文标题】使用 TensorFlow 构建 SVM【英文标题】:Building an SVM with Tensorflow 【发布时间】:2018-09-03 02:07:32 【问题描述】:我目前有两个 numpy 数组:
X
- (157, 128) - 157组128个特征
Y
- (157) - 特征集的分类
这是我为尝试构建这些特征的线性分类模型而编写的代码。
首先,我将数组调整为 Tensorflow 数据集:
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x="x": X,
y=Y,
num_epochs=None,
shuffle=True)
然后我尝试fit
一个 SVM 模型:
svm = tf.contrib.learn.SVM(
example_id_column='example_id', # not sure why this is necessary
feature_columns=tf.contrib.learn.infer_real_valued_columns_from_input(X), # create feature columns (not sure why this is necessary)
l2_regularization=0.1)
svm.fit(input_fn=train_input_fn, steps=10)
但这只是返回错误:
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpf1mwlR
WARNING:tensorflow:tf.variable_op_scope(values, name, default_name) is deprecated, use tf.variable_scope(name, default_name, values)
Traceback (most recent call last):
File "/var/www/idmy.team/python/train/classifier.py", line 59, in <module>
svm.fit(input_fn=train_input_fn, steps=10)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 316, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 480, in fit
loss = self._train_model(input_fn=input_fn, hooks=hooks)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 985, in _train_model
model_fn_ops = self._get_train_ops(features, labels)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1201, in _get_train_ops
return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.TRAIN)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1165, in _call_model_fn
model_fn_results = self._model_fn(features, labels, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/linear.py", line 244, in sdca_model_fn
features.update(layers.transform_features(features, feature_columns))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 656, in transform_features
transformer.transform(column)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 847, in transform
feature_column.insert_transformed_feature(self._columns_to_tensors)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column.py", line 1816, in insert_transformed_feature
input_tensor = self._normalized_input_tensor(columns_to_tensors[self.name])
KeyError: ''
我做错了什么?
【问题讨论】:
您能否编辑您的问题并发布错误的完整回溯? @NiayeshIsky 更新 【参考方案1】:这是一个不会引发错误的 SVM 使用示例:
import numpy
import tensorflow as tf
X = numpy.zeros([157, 128])
Y = numpy.zeros([157], dtype=numpy.int32)
example_id = numpy.array(['%d' % i for i in range(len(Y))])
x_column_name = 'x'
example_id_column_name = 'example_id'
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x=x_column_name: X, example_id_column_name: example_id,
y=Y,
num_epochs=None,
shuffle=True)
svm = tf.contrib.learn.SVM(
example_id_column=example_id_column_name,
feature_columns=(tf.contrib.layers.real_valued_column(
column_name=x_column_name, dimension=128),),
l2_regularization=0.1)
svm.fit(input_fn=train_input_fn, steps=10)
传递给 SVM 估计器 need string IDs 的示例。您可能可以替换回infer_real_valued_columns_from_input
,但您需要将字典传递给它,以便它为该列选择正确的名称。在这种情况下,自己构建特征列在概念上更简单。
【讨论】:
【参考方案2】:column_to_tensors
字典中不存在键 self.name
,这就是错误所说的,self.name
的值是一个空字符串
我认为你在向tf.estimator.inputs.numpy_input_fn
提供论据时搞砸了
解决方案可能是将 train_input_fn 行更改为
train_input_fn = tf.estimator.inputs.numpy_input_fn(x=X,
y=Y,
num_epochs=None,
shuffle=True)
我认为 x
参数必须是一个 numpy 数组,并且你给它一个字典
我会坚持他们的tutorial,不花哨
real_feature_column = real_valued_column(...)
sparse_feature_column = sparse_column_with_hash_bucket(...)
estimator = SVM(
example_id_column='example_id',
feature_columns=[real_feature_column, sparse_feature_column],
l2_regularization=10.0)
# Input builders
def input_fn_train: # returns x, y
...
def input_fn_eval: # returns x, y
...
estimator.fit(input_fn=input_fn_train)
estimator.evaluate(input_fn=input_fn_eval)
estimator.predict(x=x)
===============已更新==============
我的更新答案将针对您的错误 正如错误所说,self.name
是一个空字符串,并且您传递给创建 _RealValuedColumn
对象的 infer_real_valued_columns_from_input
的字典中不存在该空字符串
所以我通过调试错误发现是你传递的tf.contrib.learn.infer_real_valued_columns_from_input(X)
必须是一个字典,这样_RealValuedColumn
对象的self.name
就被你传递的字典的key初始化了
这就是我所做的
import tensorflow as tf
import numpy as np
X = np.array([[1], [0], [0], [1]])
Y = np.array([[1], [0], [0], [1]])
dic = "x": X
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x=dic,
y=Y,
num_epochs=None,
shuffle=True)
svm = tf.contrib.learn.SVM(example_id_column='x', feature_columns=tf.contrib.learn.infer_real_valued_columns_from_input(dic), l2_regularization=0.1)
svm.fit(input_fn=train_input_fn, steps=10)
现在这消除了上述错误,但它给出了一个新错误TypeError: Input 'input' of 'SdcaFprint' Op has type int64 that does not match expected type of string.
【讨论】:
感谢您的回复。虽然它似乎只是说明我的问题是什么,然后是来自 tensorflow 的代码 sn-p。 老兄正确地看到我的答案....当它需要 numpy 数组时,您正在给 x 一本字典 很遗憾,这不是真的。以上是关于使用 TensorFlow 构建 SVM的主要内容,如果未能解决你的问题,请参考以下文章
TensorFlow Recommenders 简介 3 使用 TensorFlow 构建推荐系统 Intro to TensorFlow Recommenders