0

I try to use svm in tensorflow, and I found a code for svm when calculating the cross entrophy in loss function.Does this code work for svm?

def calculate_loss(logits_list, labels_list, regularizer_rate):
    weight_coefficients = [1]

    # xent_mean_list = []
    # for i,(logits,labels,weight) in enumerate(zip(logits_list,labels_list,weight_coefficients)):
    #     if i == 0:
    #         xent = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=labels)
    #         xent_mean = tf.reduce_mean(xent)
    #         xent_mean_list.append(weight*xent_mean)
    # if regularizer_rate != 0:
    #     loss = reduce(tf.add,xent_mean_list) + tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
    # else:
    #     loss = reduce(tf.add, xent_mean_list)
    # return loss

    xent_mean_list = []
    for i, (logits, labels, weight) in enumerate(zip(logits_list, labels_list, weight_coefficients)):
        if i == 0:
            cross_entropy = tf.square(tf.maximum(tf.zeros([train_batch_size, 2]), 1 - logits * labels))
            xent_mean = tf.reduce_mean(tf.square(logits_list)) + tf.reduce_mean(cross_entropy)
            xent_mean_list.append(weight * xent_mean)
    if regularizer_rate != 0:
        loss = reduce(tf.add, xent_mean_list) + tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
    else:
        loss = reduce(tf.add, xent_mean_list)
    return loss

The loss function did work, but I don't know whether it's just common cross entrophy loss not including svm. Excuse my poor maths.

  • 2
    I wonder why you want to build an SVM in Tensorflow, which is specially used for deep learning applications? You could always use scikit-learn and similar Machine Learning Libraries. Keras and Tensorflow, in my opinion, are specifically suited for Deep Learning applications. You can build a simple SVM using just numpy. Refer here https://stackoverflow.com/questions/49767638/want-genuine-suggestion-to-build-support-vector-machine-in-python-without-using. There are many resources available for writing an SVM classifier from Scratch. – JChat Aug 18 '19 at 09:04
  • Thx for answer! I want a SVM to classify the output of my CNN network built in tensorflow.So I can only build a SVM in tensorflow to get the output of NN. – gardenfield Aug 18 '19 at 12:41
  • you can get the output from your Tensorflow model and cast it into a Numpy array. Refer this https://stackoverflow.com/questions/34097281/how-can-i-convert-a-tensor-into-a-numpy-array-in-tensorflow. Once you have the numpy array, using SVM should be simple. – JChat Aug 18 '19 at 16:53

0 Answers0