微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Octave 中将梯度函数修改为二阶多项式函数

如何解决在 Octave 中将梯度函数修改为二阶多项式函数

我完全不知道下一步该做什么。我的任务是 1) 改变假设函数,现在是 h(x) = t2x^2 + t1x + t0 2)并更改代码,以便绘制所有三个参数调整值。 这是我到目前为止所拥有的。现在我只需要将假设修改为 h(x) = t2x^2 + t1x + t0。

M = 100;
x = 5*randn(1,M)
y = x.^2 + 10*randn(1,M);



# Initial values for t0,t1 and t2
t1 = randn(1);
t0 = randn(1);
alkuarvaus_t1 = t1;
alkuarvaus_t0 = t0;


# recording parameters
adj_t1 = t1;
adj_t0 = t0;


# Thaching Rounds times

Rounds = 20;
rate = 0.001;

for I = 1:Rounds
   grad_t1 = 0;  
   grad_t0 = 0; 
                
                 
   for J = 1:M   
      grad_t1 = grad_t1 + (( t1*x(J) + t0) - y(J) ) * x(J);
      grad_t0 = grad_t0 + ( t1*x(J) + t0) - y(J);
   endfor

   t1 = t1 - (rate/M)*grad_t1;
   t0 = t0 - (rate/M)*grad_t0;
   adj_t1 = [adj_t1 t1];
   adj_t0 = [adj_t0 t0];
   
   xakseli = -10:0.5:10;
   hold on
   figure(5);plot( xakseli,xakseli*t1 + t0,'-',x,y,'*');title('adjustment ongoing');
endfor
   hold off
   xakseli = -10:0.5:10;
   yakseli = xakseli.*alkuarvaus_t1 + alkuarvaus_t0;
   figure(1);plot( xakseli,yakseli,'*');title('hypotesis in the beginning');
   figure(2);stem(adj_t1);
   figure(3);stem(adj_t0);
   figure(4);plot( xakseli,'*');title('hypotesis in the end');

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。