日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 循环神经网络 >内容正文

循环神经网络

线性回归代码matlab

發布時間:2025/3/21 循环神经网络 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线性回归代码matlab 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

???? 看了很多的線性回歸代碼,感覺都沒有寫出算法的核心來,因此重新寫了線性回歸代碼。下面的代碼中缺少迭代退出機制,也就是 abs(ypredict - yobserver) < eps, 如果滿足這個條件,就是求出了最優的theta值,就不需要繼續迭代了。

??? 還有問題,就是如何理解等高線的梯度下降?這個歡迎大家討論。


???

代碼見下:

% Exercise 2 Linear Regression

% Data is roughly based on 2000 CDC growth figures
% for boys
%
% x refers to a boy's age
% y is a boy's height in meters
%

clear all; close all; clc
x = load('ex2x.dat'); y = load('ex2y.dat');

m = length(y); % number of training examples


% Plot the training data
figure; % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel('Age in years')

% Gradient descent
x = [ones(m, 1) x]; % Add a column of ones to x
theta = zeros(size(x(1,:)))'; % initialize fitting parameters
MAX_ITR = 1500;
alpha = 0.07;

for num_iterations = 1:MAX_ITR
%???? This is a vectorized version of the
%???? gradient descent update formula
%???? It's also fine to use the summation formula from the videos
???
??? %Here is the gradient
%??? grad = (1/m).* x' * ((x * theta) - y);
%????
%??? % Here is the actual update
%??? theta = theta - alpha .* grad;
?%theta
???
??? % Sequential update: The wrong way to do gradient descent
????? grad1 = (1/m).* x(:,1)' * ((x * theta) - y);
%????? theta(1) = theta(1) + alpha*grad1;
???? grad2 = (1/m).* x(:,2)' * ((x * theta) - y);
%????? theta(2) = theta(2) + alpha*grad2;
???? grad=[grad1,grad2]';
???? theta = theta - alpha .* grad;
end
% print theta to screen
theta

% Plot the linear fit
hold on; % keep previous plot visible
plot(x(:,2), x*theta, '-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure

% Closed form solution for reference
% You will learn about this method in future videos
exact_theta = (x' * x)\x' * y

% Predict values for age 3.5 and 7
predict1 = [1, 3.5] *theta
predict2 = [1, 7] * theta


% Calculate J matrix

% Grid over which we will calculate J
theta0_vals = linspace(-3, 3, 100);
theta1_vals = linspace(-1, 1, 100);

% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));

for i = 1:length(theta0_vals)
?? for j = 1:length(theta1_vals)
?? t = [theta0_vals(i); theta1_vals(j)];???
?? J_vals(i,j) = (0.5/m) .* (x * t - y)' * (x * t - y);
??? end
end

% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');

% Contour plot
figure;
% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15))
xlabel('\theta_0'); ylabel('\theta_1');



總結

以上是生活随笔為你收集整理的线性回归代码matlab的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。