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()
smooth l1用于线性回归预测简单例子
最新推荐文章于 2023-12-28 16:15:31 发布