我的梯度下降有什么问题?

时间:2018-01-09 19:33:56

标签: python tensorflow linear-regression

我正在进行简单的线性回归,但尝试使用tensorflow的梯度下降优化器来优化MSE。但是,张量流代码不是优化MSE。以下是我尝试使用GDOptimizer回归一行的代码:

tf.reset_default_graph()

#calculating MSE & beta_vec using stochastic gradient descent
X=tf.placeholder(dtype=tf.float64, name="X")
Y=tf.placeholder(dtype=tf.float64, name="Y")
X_test=tf.placeholder(dtype=tf.float64, name ="X_test")
Y_test=tf.placeholder(dtype=tf.float64, name ="Y_test")

beta_vec=tf.Variable(tf.random_uniform(shape=(9,1), dtype=tf.float64), dtype=tf.float64)
Y_pred = tf.matmul(X_test, beta_vec)
MSE = tf.reduce_mean(tf.square(tf.subtract(Y_pred, Y_test)))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)

def training_op():
    optimizer.minimize(MSE)
    return MSE

init = tf.initialize_all_variables()

with tf.Session() as sess:
    def fetch_rbatch():
        a=np.random.randint(0,len(traintarget))
        b=np.random.randint(0, len(traintarget))
        rbatch_trdata = traindata[min(a,b):max(a,b)]
        rbatch_trtarget=traintarget[min(a,b):max(a,b)]
        return rbatch_trdata, rbatch_trtarget
sess.run(init)
MSE, beta =sess.run((training_op(),beta_vec) ,feed_dict={X:fetch_rbatch()[0], Y:fetch_rbatch()[1], X_test:testdata, Y_test:testtarget})
print (MSE)
print (beta)

我为MSEbeta获得的结果是:

500900.504705
[[ 0.0637637 ]
 [ 0.71051187]
 [ 0.8872875 ]
 [ 0.11578163]
 [ 0.40620434]
 [ 0.76101558]
 [ 0.17046824]
 [ 0.59776537]
 [ 0.43706391]]

MSE只是使用统一随机生成的beta_vec进行tensorflow操作返回的值。根本没有任何关于MSE的GDOptimization。你能告诉我这里我做错了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

有点重构,但仍然没有关于你的traintarget和co:

的线索
def fetch_rbatch():
    a = np.random.randint(0, len(traintarget))
    b = np.random.randint(0, len(traintarget))
    rbatch_trdata = traindata[min(a, b):max(a, b)]
    rbatch_trtarget = traintarget[min(a, b):max(a, b)]
    return rbatch_trdata, rbatch_trtarget


def main():
    X = tf.placeholder(dtype=tf.float64, name="X")
    Y = tf.placeholder(dtype=tf.float64, name="Y")
    X_test = tf.placeholder(dtype=tf.float64, name="X_test")
    Y_test = tf.placeholder(dtype=tf.float64, name="Y_test")

    beta_vec = tf.Variable(tf.random_uniform(shape=(9, 1), dtype=tf.float64), dtype=tf.float64)
    loss = tf.reduce_mean(tf.square(tf.subtract(X, Y)))  # aka MSE, note that loss calculated on train data
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss)  # merge with minimizer
    # Y_pred = tf.matmul(X_test, beta_vec)
    # todo for @mkdar: make accuracy op

    with tf.Session() as sess:
        tf.global_variables_initializer().run()  # init vars
        for i in range(1000):  # number of steps
            # note underscore in loss, and that we pass optimiser in order to train stuff
            loss_, beta, _ = sess.run([loss, beta_vec, optimizer],
                                      feed_dict={
                                        X: fetch_rbatch()[0],
                                        Y: fetch_rbatch()[1],
                                        X_test: testdata,  # useless coz we do nothing
                                        Y_test: testtarget})  # this too
            if i % 20 == 19:
                # print every 20 step
                print(loss_)
                print(beta)

如果您是新手,也请查看official tutorials