美文网首页
tensorflow 实现简单线性回归/非线性回归

tensorflow 实现简单线性回归/非线性回归

作者: 菜囧爱学习 | 来源:发表于2019-02-13 22:23 被阅读0次
    import numpy as np
    import matplotlib.pyplot as plt
    
    DATA_FILE =  "boston_housing.csv"
    BATCH_SIZE = 10
    NUM_FEATURES = 14
    
    #归一化
    def nolmalize(X):
        mean = np.mean(X)
        std  = np.std(X)   #矩阵标准差
        X    = (X-mean)/std
        return X
    #线性回归#非线性回归
    x_data=np.linspace(-1,1,1000)[:,None]
    noise=np.random.normal(0,0.01,x_data.shape)#生成干扰,形状和x_data一样
    y_data =(x_data)+noise
    y_data1 = np.square(x_data)+noise
    print(x_data.shape)
    
    x = tf.placeholder(tf.float32,[None,1])
    y = tf.placeholder(tf.float32,[None,1])
    
    weight = tf.Variable(tf.random_normal([1,1]))
    bias   = tf.Variable(tf.random_normal([1,1]))
    
    prediction = tf.matmul(x,weight)+bias
    
    loss  = tf.reduce_mean(tf.square(y-prediction))
    train = tf.train.AdagradOptimizer(0.01).minimize(loss)
    
    weight1 = tf.Variable(tf.random_normal([1,5]))
    bias1   = tf.Variable(tf.random_normal([1,5]))
    prediction1 = tf.nn.tanh(tf.matmul(x,weight1)+bias1)
    
    weight2 = tf.Variable(tf.random_normal([5,1]))
    bias2   = tf.Variable(tf.random_normal([1,1]))
    prediction2 = tf.nn.tanh(tf.matmul(prediction1,weight2)+bias2)
    
    
    loss1  = tf.reduce_mean(tf.square(y-prediction2))
    train1 = tf.train.AdagradOptimizer(0.01).minimize(loss1)
    
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        for i in range(10000):
            if i%100==0:
                print(i)
            sess.run(train,feed_dict={x:x_data,y:y_data})
            sess.run(train1,feed_dict={x:x_data,y:y_data1})
        
        prediction_value=sess.run(prediction,feed_dict={x:x_data})
        prediction_value1=sess.run(prediction2,feed_dict={x:x_data})
        
        plt.figure()
        plt.scatter(x_data,y_data)
        plt.plot(x_data,prediction_value,'r')
        plt.show()
        
        plt.figure()
        plt.scatter(x_data,y_data1)
        plt.plot(x_data,prediction_value1,'r')
        plt.show()
    

    相关文章

      网友评论

          本文标题:tensorflow 实现简单线性回归/非线性回归

          本文链接:https://www.haomeiwen.com/subject/cmpjeqtx.html