利用tensorfolw优化器做梯度下降求方程系数和截距最优解(d代码详细步骤说明)以及matlab实现对比

import tensorflow as tf

import numpy as np

#生成模拟数据

inputX = np.random.rand(100)  #随机定义100个x值

inputY = np.multiply(3,inputX) + 1#标签值y=3x+1

#定义方程组  

x = tf.placeholder("float32")

weight = tf.Variable(0.25)#初始化系数(真实系数为3) 类似matlab定义:syms weight,并且给初值为0.25

bias = tf.Variable(0.25)#初始化截距(真实值为1) 类似matlab定义:syms bias,并且给初值为0.25

y = np.dot(weight,x) + bias #等同于y=wx+b,表示计算出的预测值

y_ = tf.placeholder("float32") #定义真实值,类似matlab定义:syms y

loss = tf.reduce_sum(tf.pow((y-y_),2)) #loss=累加(y(i)-y_(i))^2 ,100个样本点的误差值

train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss) #优化器做梯度下降,计算 weight和bias精确值

#以下步骤,把数据带入上述方程

sess = tf.Session()

init = tf.global_variables_initializer()

sess.run(init)

for _ in range(20000):

    sess.run(train_step,feed_dict = {x:inputX,y_:inputY})

    if _ % 20 == 0:

        print("W的值为:",weight.eval(session=sess),";bias的值为:",bias.eval(session=sess))

       

#总结 最后训练出来的W的值为: 2.9999917 ;bias的值为: 1.0000043 与真实值3,1很接近    

输出结果:

W的值为: 0.49709255 ;bias的值为: 0.6647657
W的值为: 1.6060523 ;bias的值为: 1.7156062
W的值为: 1.9460768 ;bias的值为: 1.5449618
W的值为: 2.2019558 ;bias的值为: 1.4126645
W的值为: 2.3957067 ;bias的值为: 1.3124769
W的值为: 2.5424187 ;bias的值为: 1.2366129
W的值为: 2.6535113 ;bias的值为: 1.1791674
W的值为: 2.7376325 ;bias的值为: 1.1356689
W的值为: 2.8013306 ;bias的值为: 1.1027309
W的值为: 2.8495638 ;bias的值为: 1.0777898
W的值为: 2.8860872 ;bias的值为: 1.0589037
W的值为: 2.9137442 ;bias的值为: 1.0446026
W的值为: 2.9346855 ;bias的值为: 1.0337738
W的值为: 2.9505427 ;bias的值为: 1.0255742
W的值为: 2.9625497 ;bias的值为: 1.0193652
W的值为: 2.9716418 ;bias的值为: 1.0146639
W的值为: 2.9785266 ;bias的值为: 1.0111039
W的值为: 2.9837399 ;bias的值为: 1.008408
W的值为: 2.9876873 ;bias的值为: 1.0063667
W的值为: 2.9906769 ;bias的值为: 1.0048211
W的值为: 2.9929402 ;bias的值为: 1.0036505
W的值为: 2.9946542 ;bias的值为: 1.0027642
W的值为: 2.995952 ;bias的值为: 1.0020932
W的值为: 2.996935 ;bias的值为: 1.001585
W的值为: 2.9976788 ;bias的值为: 1.0012001
W的值为: 2.9982426 ;bias的值为: 1.0009087
W的值为: 2.9986691 ;bias的值为: 1.0006881
W的值为: 2.9989922 ;bias的值为: 1.0005211
W的值为: 2.9992368 ;bias的值为: 1.0003946
W的值为: 2.9994218 ;bias的值为: 1.0002989
W的值为: 2.9995625 ;bias的值为: 1.0002263
W的值为: 2.9996686 ;bias的值为: 1.0001714
W的值为: 2.999749 ;bias的值为: 1.0001299
W的值为: 2.9998095 ;bias的值为: 1.0000985
W的值为: 2.9998558 ;bias的值为: 1.0000746
W的值为: 2.9998906 ;bias的值为: 1.0000565
W的值为: 2.9999168 ;bias的值为: 1.000043
W的值为: 2.999937 ;bias的值为: 1.0000327
W的值为: 2.9999518 ;bias的值为: 1.000025
W的值为: 2.999963 ;bias的值为: 1.0000191
W的值为: 2.9999726 ;bias的值为: 1.0000143
W的值为: 2.9999783 ;bias的值为: 1.0000112
W的值为: 2.999983 ;bias的值为: 1.0000088
W的值为: 2.9999878 ;bias的值为: 1.0000064
W的值为: 2.9999917 ;bias的值为: 1.0000044
W的值为: 2.9999917 ;bias的值为: 1.0000043
W的值为: 2.9999917 ;bias的值为: 1.0000043
W的值为: 2.9999917 ;bias的值为: 1.0000043
W的值为: 2.9999917 ;bias的值为: 1.0000043
W的值为: 2.9999917 ;bias的值为: 1.0000043

matlab代码实现:

clear
clc
% training sample data;
% p0=1;
% p1=3;
% x=1:100;
% y=p0+p1*x;
x=randperm(100,100) ;
y=3*x+1;
num_sample=size(y,2);

% gradient descending process
% initial values of parameters
theta0=0.25;
theta1=0.25;

%learning rate
% alpha=z(:,n);
% if alpha is too large, the final error will be much large.

% if alpha is too small, the convergence will be slow
epoch=10000;
alpha=0.001;
%调整学习率调整次数
 for n=1:9
  alpha=alpha+0.001;
    for k=1:epoch

        v_k=k;
        h_theta_x=theta0+theta1*x;% hypothesis function
        Jcost1(k)=((h_theta_x(1)-y(1))^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2+(h_theta_x(4)-y(4))^2)/(2*num_sample);
        temp0=theta0-alpha*((h_theta_x(1)-y(1))+(h_theta_x(2)-y(2))+(h_theta_x(3)-y(3))+(h_theta_x(4)-y(4)))/num_sample;
       temp1=theta1-alpha*((h_theta_x(1)-y(1))*x(1)+(h_theta_x(2)-y(2))*x(2)+(h_theta_x(3)-y(3))*x(3)+(h_theta_x(4)-y(4))*x(4))/num_sample;
       theta0=temp0;
       theta0
       theta1=temp1;
       theta1

    end

 end

Original: https://blog.csdn.net/YXC_GG/article/details/123785467
Author: YXC_GG
Title: 利用tensorfolw优化器做梯度下降求方程系数和截距最优解(d代码详细步骤说明)以及matlab实现对比

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509195/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球