我一直在听吴教授的讲座,并试图用tensorflow在我的jupyter笔记本上实现支持向量机。然而,我的模型似乎没有正确收敛。
我想我有错误的损失函数,这可能会不适合我的模型。
tf.reset_default_graph()
#training hyper parameters
learning_rate = 0.000001
C = 20
gamma = 50
X = tf.placeholder(tf.float32, shape=(None,2))
Y = tf.placeholder(tf.float32, shape=(None,1))
landmark = tf.placeholder(tf.float32, shape=(None,2))
W = tf.Variable(np.random.random((num_data)),dtype=tf.float32)
B = tf.Variable(np.random.random((1)),dtype=tf.float32)
batch_size = tf.shape(X)[0]
#RBF Kernel
tile = tf.tile(X, (1,num_data))
diff = tf.reshape( tile, (-1, num_data, 2)) - landmark
tile_shape = tf.shape(diff)
sq_diff = tf.square(diff)
sq_dist = tf.reduce_sum(sq_diff, axis=2)
F = tf.exp(tf.negative(sq_dist * gamma))
WF = tf.reduce_sum(W * F, axis=1) + B
condition = tf.greater_equal(WF, 0)
H = tf.where(condition, tf.ones_like(WF),tf.zeros_like(WF))
ERROR_LOSS = C * tf.reduce_sum(Y * tf.maximum(0.,1-WF) + (1-Y) * tf.maximum(0.,1+WF))
WEIGHT_LOSS = tf.reduce_sum(tf.square(W))/2
TOTAL_LOSS = ERROR_LOSS + WEIGHT_LOSS
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(TOTAL_LOSS)
我使用高斯核并将整个训练集作为里程碑。
我很确定我错过了什么。