美文网首页机器学习
深度学习-tensorflow-梯度计算和mes激活函数

深度学习-tensorflow-梯度计算和mes激活函数

作者: 老生住长亭 | 来源:发表于2018-07-31 00:44 被阅读13次
    import tensorflow as tf
    import numpy as np
    from sklearn.datasets import fetch_california_housing
    from sklearn.preprocessing import StandardScaler
    
    epochs = 1000000
    learn_rate = 0.03
    
    house = fetch_california_housing(data_home="/home/botter/data_sets", download_if_missing=True)
    m, n = house.data.shape
    # m row data, n column data
    print(m, n)
    print("**************************house.data:")
    print(house.data)
    print("**************************house.target:")
    print(house.target)
    #  add one column of value is 1
    house_data_plus_bias = np.c_[np.ones((m, 1)), house.data]
    
    # standardScaler
    scaler = StandardScaler().fit(house_data_plus_bias)
    print("**************************scaler:")
    print(scaler)
    scaler_house_data_plus_bias = scaler.transform(house_data_plus_bias)
    print("**************************scaler_house_data_plus_bias:")
    print(scaler_house_data_plus_bias)
    
    X = tf.constant(scaler_house_data_plus_bias, dtype=tf.float32, name="X")
    y = tf.constant(house.target.reshape(-1, 1), dtype=tf.float32, name="y")
    print("**************************X:")
    print(X)
    print("**************************y")
    print(y)
    theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0), name="theta")
    print("***************************theta:")
    print(theta)
    y_predict = tf.matmul(X, theta, name="predictions")
    print("***************************predictions")
    print(y_predict)
    error = y_predict - y
    print("***************************error")
    print(error)
    mse = tf.reduce_mean(tf.square(error), name="mse")
    print("***************************mes")
    print(mse)
    # 梯度的公式:(y_pred - y) * xj
    gradients = 2/m * tf.matmul(tf.transpose(X), error)
    training_op = tf.assign(theta, theta - learn_rate * gradients)
    
    init = tf.global_variables_initializer()
    
    with tf.Session() as sess:
        sess.run(init)
    
        for epoch in range(epochs):
            if epoch % 100 == 0:
                print("Epoch", epoch, "MSE = ", mse.eval())
            sess.run(training_op)
    
        best_theta = theta.eval()
        print(best_theta)
    

    相关文章

      网友评论

        本文标题:深度学习-tensorflow-梯度计算和mes激活函数

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