简体   繁体   English

向Tensorflow添加自定义正则化

[英]Add Custom Regularization to Tensorflow

I am using tensorflow to optimize a simple least squares objective function like the following: 我正在使用tensorflow来优化简单的最小二乘目标函数,如下所示:

在此处输入图片说明

Here, Y is the target vector , X is the input matrix and vector w represents the weights to be learned. 在此, Y是目标向量, X是输入矩阵,向量w表示要学习的权重。

Example Scenario: 示例场景:

在此处输入图片说明 , 在此处输入图片说明 , 在此处输入图片说明

If I wanted to augment the initial objective function to impose an additional constraint on w1 (the first scalar value in the tensorflow Variable w and X1 represents the first column of the feature matrix X ), how would I achieve this in tensorflow? 如果我想增加初始目标函数以对w1施加其他约束(张量流变量wX1的第一个标量值表示特征矩阵X的第一列),我将如何在张量流中实现呢?

在此处输入图片说明

One solution I can think of is to use tf.slice to index the first value of $w$ and add this in addition to the original cost term but I am not convinced that it will have the desired effect on the weights. 我能想到的一种解决方案是使用tf.slice为$ w $的第一个值建立索引,并在原始成本项之外添加该值,但我不相信它将对权重产生理想的影响。

I would appreciate inputs on whether something like this is possible in tensorflow and if so, what the best ways to implement this might be? 我希望在张量流中是否可以进行这样的输入,如果可以,那么最好的实现方法是什么?

An alternate option would be to add weight constraints, and do it using an augmented Lagrangian objective but I would first like to explore the regularization option before going the Lagrangian route. 另一种选择是增加权重约束,并使用增强的拉格朗日目标进行,但是我首先想在走拉格朗日路线之前探索正则化选项。

The current code I have for the initial objective function without additional regularization is the following: 我没有附加正则化的初始目标函数的当前代码如下:

train_x ,train_y are the training data, training targets respectively.
test_x  , test_y are the testing data, testing targets respectively.

#Sum of Squared Errs. Cost.
def costfunc(predicted,actual):
    return tf.reduce_sum(tf.square(predicted - actual))

#Mean Squared Error Calc.
def prediction(sess,X,y_,test_x,test_y):
    pred_y = sess.run(y_,feed_dict={X:test_x})
    mymse = tf.reduce_mean(tf.square(pred_y - test_y))
    mseval=sess.run(mymse)

    return mseval,pred_y


with tf.Session() as sess:

    X = tf.placeholder(tf.float32,[None,num_feat])  #Training Data 
    Y = tf.placeholder(tf.float32,[None,1]) #  Target Values 
    W = tf.Variable(tf.ones([num_feat,1]),name="weights")  

    init = tf.global_variables_initializer()

    sess.run(init)

    #Tensorflow ops and cost function definitions.
    y_ = tf.matmul(X,W) 
    cost_history = np.empty(shape=[1],dtype=float)
    out_of_sample_cost_history = np.empty(shape=[1],dtype=float)
    cost=costfunc(y_,Y) 
    learning_rate = 0.000001
    training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)


    for epoch in range(training_epochs):
        sess.run(training_step,feed_dict={X:train_x,Y:train_y})
        cost_history = np.append(cost_history,sess.run(cost,feed_dict={X: train_x,Y: train_y}))
        out_of_sample_cost_history = np.append(out_of_sample_cost_history,sess.run(cost,feed_dict={X:test_x,Y:test_y}))


    MSETest,pred_test = prediction(sess,X,y_,test_x,test_y) #Predict on full testing set.

tf.slice will do. tf.slice可以。 And during optimization, the gradients to w1 will be added (because gradients add up at forks). 在优化过程中,将添加到w1的渐变(因为渐变会在分叉处累加)。 Also, please check the graph on Tensorboard (the link on how to use it). 另外,请检查Tensorboard上的图形(有关使用方法的链接 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM