smooth l1用于线性回归预测简单例子

import random

import matplotlib.pyplot as plt
import tensorflow as tf

def smooth_L1_loss_function(pred,target,gamma=0.5):
    pred=tf.cast(pred,tf.float32)
    target=tf.cast(target,tf.float32)
    diff = pred - target
    abs_diff = tf.abs(diff)
    smoothL1_sign = tf.stop_gradient(tf.to_float(tf.less(abs_diff, gamma)))
    smoothL1 = tf.pow(diff, 2) * gamma * smoothL1_sign + (abs_diff - gamma) * (1. - smoothL1_sign)
    return tf.reduce_mean(smoothL1)
def create_data(for_train=False):
    w = 5.33
    b = -23.26
    x = random.random() * 30
    y = w * x + b

    if for_train:
        noise = (random.random() - 0.5) * 10
        y += noise

    return x, y


def draw():
    x_data, y_data = [], []
    for _ in range(100):
        x, y = create_data(True)
        x_data.append(x)
        y_data.append(y)
    plt.figure()
    plt.scatter(x_data, y_data)
    plt.show()


def run():
    LOSS=[]
    STEP=[]
    PRED=[]
    TRUTH=[]
    X = tf.placeholder(tf.float32)
    Y = tf.placeholder(tf.float32)

    W = tf.Variable(tf.zeros([1]))
    B = tf.Variable(tf.zeros([1]))
    OUT = X * W + B

    loss = smooth_L1_loss_function(OUT,Y)
    optimizer = tf.train.AdamOptimizer(0.005).minimize(loss)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(5000):
            x_data, y_data = create_data(True)
            _, _loss, _w, _b, pred_y, truth_y= sess.run([optimizer, loss, W, B, OUT, Y], feed_dict={X: x_data, Y: y_data})
            if 5000 >= epoch and 0 == epoch % 30:
                LOSS.append(_loss)
                STEP.append(epoch)
                # PRED.append(pred_y)
                # TRUTH.append(truth_y)
            if 0 == epoch % 5000:
                print("epoch=",epoch,"_loss=", _loss, "_w=", _w, "_n=", _b)

        print("")

        for step in range(30):
            x_data, y_data = create_data(False)
            prediction_value = sess.run(OUT, feed_dict={X: x_data})
            print("x=", x_data, "y_prediction=", prediction_value, "y_reality=", y_data)
            PRED.append(prediction_value)
            TRUTH.append(y_data)

        # plt.scatter(x_data, y_data)
    plt.figure()
    plt.plot(PRED, color='red', label="prediction")
    plt.plot(TRUTH, color='blue', label="truth")
    # plt.plot(data_DGCNN, color='green', label="DGCNN")
    plt.xlabel("number")
    plt.ylabel("value")
    plt.legend(loc="compare")
    plt.grid(True, color='black', linestyle=':', linewidth=0.5)
    # plt.savefig('curb detection accuracy.png')
    # plt.figure()
    # plt.scatter(STEP,PRED)
    plt.show()

if __name__ == "__main__":
    # draw()
    run()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值