使用Tensorflow构建SVM [英] Building an SVM with Tensorflow

查看:73
本文介绍了使用Tensorflow构建SVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个numpy数组:

I currently have two numpy arrays:

  • X -(157,128)-157组128个功能
  • Y -(157)-功能集的分类
  • X - (157, 128) - 157 sets of 128 features
  • Y - (157) - classifications of the feature sets

这是我为构建这些功能的线性分类模型而编写的代码.

This is the code I have written to attempt to build a linear classification model of these features.

首先,我将数组调整为Tensorflow数据集:

First of all I adapted the arrays to a Tensorflow dataset:

train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": X},
    y=Y,
    num_epochs=None,
    shuffle=True)

然后,我尝试拟合一个SVM模型:

I then tried to fit an SVM model:

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: ''

我在做什么错了?

推荐答案

这是一个不引发错误的SVM使用示例:

Here's an SVM usage example which does not throw an error:

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估计器的示例需要字符串ID .您可能可以用 infer_real_valued_columns_from_input 代替,但是您需要将其传递给字典,以便它为列选择正确的名称.在这种情况下,从概念上讲,只需要自己构造功能列就更简单了.

Examples passed to the SVM Estimator need string IDs. You can probably substitute back infer_real_valued_columns_from_input, but you would need to pass it a dictionary so it picks up the right name for the column. In this case it's conceptually simpler to just construct the feature column yourself.

这篇关于使用Tensorflow构建SVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆