代码之家  ›  专栏  ›  技术社区  ›  Harshit

Tensorflow-线性回归:不能正确绘制

  •  0
  • Harshit  · 技术社区  · 8 年前

    我一直在用张量流研究线性回归问题。我有一条曲线 pred_y 平的。我应该如何用观察的训练例子来拟合我的曲线?

    这是我的tensorflow代码:

    # coding: utf-8
    
    # In[146]:
    
    
    import numpy as np
    import matplotlib.pyplot as plt
    import tensorflow as tf
    import pandas as pd
    
    
    # In[147]:
    
    
    train_features = pd.read_csv("training_set_X.csv", delimiter=',').as_matrix()
    train_observations = pd.read_csv("training_set_Y.csv", delimiter=',').as_matrix()
    
    print("Training features: ")
    train_features
    
    
    # In[148]:
    
    
    print("Training observations: ")
    train_observations
    
    
    # In[149]:
    
    
    print("Shape of training features = ", train_features.shape)
    print("Shape of training observations = ", train_observations.shape)
    
    
    # In[150]:
    
    
    # Normalization of training data.
    train_features_stddev_arr = np.std(train_features, axis=0)
    train_features_mean_arr = np.mean(train_features, axis=0)
    normalized_train_features = (train_features - train_features_mean_arr) / train_features_stddev_arr
    
    
    # In[151]:
    
    
    print("Training features: Standard deviation....")
    train_features_stddev_arr
    
    
    # In[152]:
    
    
    print("Training featues: Mean....")
    train_features_mean_arr
    
    
    # In[153]:
    
    
    print("Normalized training features....")
    normalized_train_features
    
    
    # In[154]:
    
    
    # Layer parameters.
    n_nodes_h11 = 5
    n_nodes_h12 = 5
    n_nodes_h13 = 3
    no_features = 17
    learning_rate = 0.01
    epochs = 200
    
    
    # In[155]:
    
    
    cost_history = []
    
    
    # In[156]:
    
    
    X = tf.placeholder(tf.float32, name='X')
    Y = tf.placeholder(tf.float32, name='Y')
    
    
    # In[157]:
    
    
    # Defining weights and biases.
    first_weight = tf.Variable(tf.random_normal([no_features, n_nodes_h11], stddev=np.sqrt(2/no_features)))
    second_weight = tf.Variable(tf.random_normal([n_nodes_h11, n_nodes_h12], stddev=np.sqrt(2/n_nodes_h11)))
    third_weight = tf.Variable(tf.random_normal([n_nodes_h12, n_nodes_h13], stddev=np.sqrt(2/n_nodes_h12)))
    output_weight = tf.Variable(tf.random_normal([n_nodes_h13, 1], stddev=np.sqrt(2/n_nodes_h13)))
    
    
    # In[158]:
    
    
    first_bias = tf.Variable(tf.random_uniform([n_nodes_h11], -1.0, 1.0))
    second_bias = tf.Variable(tf.random_uniform([n_nodes_h12], -1.0, 1.0))
    third_bias = tf.Variable(tf.random_uniform([n_nodes_h13], -1.0, 1.0))
    output_bias = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
    
    
    # In[159]:
    
    
    # Defining activations of each layer.
    first = tf.sigmoid(tf.matmul(X, first_weight) + first_bias)
    second = tf.sigmoid(tf.matmul(first, second_weight) + second_bias)
    third = tf.sigmoid(tf.matmul(second, third_weight) + third_bias)
    output = tf.matmul(third, output_weight) + output_bias
    
    
    # In[182]:
    
    
    # Using Mean Squared Error
    cost = tf.reduce_mean(tf.pow(output - Y, 2)) / (2 * train_features.shape[0])
    
    
    # In[183]:
    
    
    # Using Gradient Descent algorithm
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
    
    
    # In[184]:
    
    
    init = tf.global_variables_initializer()
    
    
    # In[194]:
    
    
    # Running the network.
    with tf.Session() as sess:
        sess.run(init)
    
        for step in np.arange(epochs):
            sess.run(optimizer, feed_dict={X:normalized_train_features, Y:train_observations})
            cost_history.append(sess.run(cost, feed_dict={X:normalized_train_features, Y:train_observations}))
    
        pred_y = sess.run(output, feed_dict={X:normalized_train_features})
        plt.plot(range(len(pred_y)), pred_y)
        plt.plot(range(len(train_observations)), train_observations)
    
    
    # In[195]:
    
    
    plt.show()
    

    训练特征形状=(967,17)和训练观察形状=(967,1)

    我所观察到的直线(pred_y)是由于pred_y值作为大负片产生的。列车观测值已经是正值。

    如果有人能帮我解决这个问题,那就太好了。我不想让predúy线那么直。我想我做错了什么。如果有人能指出我的错误,那就太好了。谢谢!

    解决方案1。

    你有一个17维的特征,因此很难绘制一个有意义的曲线没有一些维度的减少。因此,不能期望代码有意义的绘图。

    解决方案2。

    由@lincr解决

    1 回复  |  直到 8 年前
        1
  •  1
  •   lincr    8 年前

    你在这里使用了错误的损失函数。

    你想用的是 mean squared error ,应该是

    tf.reduce_sum(tf.pow(output - Y, 2)/train_features.shape[0])

    如果你想用 tf.reduce_mean ,应该是

    tf.reduece_mean(tf.squared_difference(output, Y))

    请注意,在 reduce_sum 已执行平均(平均)操作。

    推荐文章