Building an SVM with Tensorflow

后端 未结 2 1078
既然无缘
既然无缘 2021-01-01 17:03

I currently have two numpy arrays:

  • X - (157, 128) - 157 sets of 128 features
  • Y - (157) - classifications of the feature sets
2条回答
  •  被撕碎了的回忆
    2021-01-01 17:37

    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)
    

    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.

提交回复
热议问题