Building an SVM with Tensorflow

后端 未结 2 1054
既然无缘
既然无缘 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.

    0 讨论(0)
  • 2021-01-01 17:44
    • The key self.name is not present in column_to_tensors dictionary that's what the error says and the value of self.name is an empty string
    • I think you messed up in giving the arguments to tf.estimator.inputs.numpy_input_fn
    • The solution might be changing train_input_fn line to

      train_input_fn = tf.estimator.inputs.numpy_input_fn(x=X,
                                                          y=Y,
                                                          num_epochs=None,
                                                          shuffle=True)
      
    • I think the x argument has to be a numpy array and you are giving it a dictionary

    • I will stick to their tutorial and do not do any fancy

      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)
      

    ===============UPDATED==============

    • My updated answer will be specific to your error
    • As the error says self.name is an empty string and that empty string is not present in your dictionary that you are passing to infer_real_valued_columns_from_input that creates _RealValuedColumn object
    • So What I found by debugging the error is that the tf.contrib.learn.infer_real_valued_columns_from_input(X) the X that you pass has to be a dictionary so that the self.name of _RealValuedColumn object is initialized by the key of the dictionary that you pass
    • So this is what I did

      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)
      
    • Now this removes the above error an but it gives a new error TypeError: Input 'input' of 'SdcaFprint' Op has type int64 that does not match expected type of string.

    • Hopefully you remove your down-vote
    0 讨论(0)
提交回复
热议问题