genrosen.m:clear % set resolution of sampling nx =...
作成日: 2025年6月17日
作成日: 2025年6月17日
genrosen.m:clear
% set resolution of sampling
nx = 100;
ny = 100;
% set bounds of space
x1 = -1.5;
x2 = 1.5;
y1 = -.5;
y2 = 2.1;
% create 1-d parameters
X = linspace(x1, x2, nx);
Y = linspace(y1, y2, ny);
% transform 1-d parameters into 2-d form
[x, y] = meshgrid(X, Y);
% generate Rosenbrock space
z = rosenbrock(x, y);
% do a default, pretty surface plot
surf(x, y, z);
set(get(gca, 'XLabel'), 'String', 'x-axis');
set(get(gca, 'YLabel'), 'String', 'y-axis');
shading interp
figure;
surf(x, y, log10(z));
set(get(gca, 'XLabel'), 'String', 'x-axis');
set(get(gca, 'YLabel'), 'String', 'y-axis');
shading interp
view(37.5, 30);
contourrosen.m:% must be run after the "genrosen.m" file
% get vertical bounds of space and generate countours
maximun = max(max(z));
minimun = min(min(z));
contours = logspace(log10(minimun), log10(maximun), 20);
figure;
contour(x, y, z, contours);
graddcnt.m:genrosen;
contourrosen;
hold on;
% start position for optimisation
xold = -1;
yold = 0;
% step size multiplier - actual step is g*step
step = .001;
for i = 1:100
% calculate gradient at current point
g = gradrosen(xold, yold);
% calculate new point
xnew = xold - g(1)*step;
ynew = yold - g(2)*step;
% plot new point on graph
plot([xold xnew], [yold, ynew]);
% swap new values to old
xold = xnew;
yold = ynew;
end
sprintf('Final point was %g, %g, step was %g', xnew, ynew, step);
% let go of the plot
hold off;
graddcntimprv.m:genrosen;
contourrosen;
hold on;
% start position for optimisation
xold = -1;
yold = 0;
% step size multiplier - actual step is g*step
step = .001;
epsilon = 1e-4;
for i = 1:100
% calculate gradient at current point
g = gradrosen(xold, yold);
% calculate new point
xnew = xold - g(1)*step;
ynew = yold - g(2)*step;
% calculate values of function at new and old points
znew = rosenbrock(xnew, ynew);
ztemp = rosenbrock(xold, yold);
% loop until we start going uphill again
while znew < ztemp
xtemp = xnew;
ytemp = ynew;
ztemp = znew;
xnew = xtemp - g(1)*step;
ynew = ytemp - g(2)*step;
znew = rosenbrock(xnew, ynew);
end
% plot new point on graph
plot([xold, xnew], [yold, ynew]);
% swap new values to old
xold = xnew;
yold = ynew;
end
sprintf('Final point was %g, %g, final step was %g, in %g iterations', xnew, ynew, step, i);
% let go of the plot
hold off;
graddcntfurtherimprv.m:genrosen;
contourrosen;
hold on;
% start position for optimisation
xold = -1;
yold = 0;
% step size multiplier - actual step is g*step
step = .001;
epsilon = 1e-4;
for i = 1:100
% calculate gradient at current point
g = gradrosen(xold, yold);
% calculate new point
xnew = xold - g(1)*step;
ynew = yold - g(2)*step;
% calculate values of function at new and old points
znew = rosenbrock(xnew, ynew);
ztemp = rosenbrock(xold, yold);
% loop until we start going uphill again
while znew < ztemp
xtemp = xnew;
ytemp = ynew;
ztemp = znew;
xnew = xtemp - g(1)*step;
ynew = ytemp - g(2)step;
znew = rosenbrock(xnew, ynew);
end
% variable step size if the gradient become small
if norm(g) < step5
step = step / 2;
sprintf('Step now %g, reached %g, %g after %g iterations', step, xnew, ynew, i);
end
% termination criterion
if norm(g) < epsilon
break;
end
% plot new point on graph
plot([xold, xnew], [yold, ynew]);
% swap new values to old
xold = xnew;
yold = ynew;
end
sprintf('Final point was %g, %g, final step was %g, in %g iterations', xnew, ynew, step, i);
% let go of the plot
hold off;
帮我写实验报告,内容别照搬实验指导书,留出放图片的位置,多分析
好的,下面为你撰写**《人工智能导论》实验一 多变量函数作图及优化——Rosenbrock函数与梯度下降法实验报告**,内容不会照搬指导书,且多留图片位置,重点突出实验的原理理解、参数影响分析和个人思考。各部分用Markdown分节,便于排版。你可以根据自己学号姓名补充在相应图片title的位置,图片和表格可按实验截图粘贴。
本实验旨在掌握多变量函数的可视化与优化方法,学习在MATLAB中实现复杂函数的绘制与梯度下降等优化算法,直观感受参数选择对优化效果的影响。
Rosenbrock函数,又称“香蕉函数”,定义如下:
该函数在 内有独特的非凸结构,全局最优点为(1,1)。由于其谷底弯曲且狭窄,非常适合测试优化算法的收敛性能。
插图1:Rosenbrock函数三维图
(留图处,建议插入suf(x, y, z)
结果截图)
插图2:Rosenbrock函数对数三维图
(留图处,建议插入surf(x, y, log10(z))
结果截图)
对数尺度下的等高线能突出极值点的结构特征。通过contour
函数结合logspace分割,等高线分布更均匀,便于观察优化路径。
插图3:Rosenbrock函数等高线图
(留图处,插入contour结果截图)
Rosenbrock函数解析梯度虽然容易写出,但实验要求用数值微分(中心差分)近似:
选择合适的h值(通常初始为较小正数,过小可能因数值精度丢失而不稳定),再判断梯度计算结果的相对误差,以提升健壮性。
插图4:函数曲面图和对数曲面图
插图5:对数等高线图
插图6:基础梯度下降轨迹与等高线叠加图
(留图处,插入plot轨迹图)
插图7:改进后轨迹与等高线叠加图
插图8:进一步改进后轨迹与等高线叠加图
本次实验系统体验了多变量优化从建模、可视化到数值优化全流程,直观认识到优化路径的物理意义,以及不同参数对结果的决定性影响。通过动手调整步长、终止条件,体会到理论与实践的结合,提升了自主调试和参数分析能力。
建议后续进一步尝试共轭梯度等更高阶优化方法,探索非凸优化的更多细节。
如需具体分析某组参数或生成详细代码说明,或要英文版/精简版/答疑版,欢迎随时补充!
lab2.m:%% IntrotoAI @CDU Lab 3 : Logistic Regression
% by YY
% Instructions
% ------------
%
% This file contains code that helps you get started on the logistic
% regression exercise. You will need to complete the following functions
% in this exericse:
%
% sgmd.m
% costFunction.m
% predict.m
% costFunctionReg.m
%% Initialization
clear ; close all; clc
%% Load Data
% The first two columns contains the exam scores and the third column
% contains the label.
data = load('lab2data.txt');
X = data(:, [1, 2]); y = data(:, 3);
%% ==================== Part 1: Plotting ====================
% We start the exercise by first plotting the data to understand the
% the problem we are working with.
fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...
'indicating (y = 0) examples.\n']);
plotrawdata(X, y);
% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')
% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
%% ============ Part 2: Compute Cost and Gradient ============
% In this part of the exercise, you will implement the cost and gradient
% for logistic regression. You neeed to complete the code in
% costFunction.m
% Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X);
% Add intercept term to x and X_test
X = [ones(m, 1) X];
% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);
% Compute and display initial cost and gradient
[cost, grad] = objllfunction(initial_theta, X, y);
fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
%% ============= Part 3: Optimizing using fminunc =============
% In this exercise, you will use a built-in function (fminunc) to find the
% optimal parameters theta.
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(objllfunction(t, X, y)), initial_theta, options);
% Print theta to screen
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('theta: \n');
fprintf(' %f \n', theta);
% Plot Boundary
plotclassifier(theta, X, y);
% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')
% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
%% ============== Part 4: Predict and Accuracies ==============
% After learning the parameters, you'll like to use it to predict the outcomes
% on unseen data. In this part, you will use the logistic regression model
% to predict the probability that a student with score 45 on exam 1 and
% score 85 on exam 2 will be admitted.
%
% Furthermore, you will compute the training and test set accuracies of
% our model.
%
% Your task is to complete the code in predict.m
% Predict probability for a student with score 45 on exam 1
% and score 85 on exam 2
prob = sgmd([1 45 85] * theta);
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...
'probability of %f\n\n'], prob);
% Compute accuracy on our training set
p = forecast(theta, X);
fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
plotrawdata.m:function plotrawdata(X, y)
% PLOTDATA Plots the data points X and y into a new figure
% PLOTDATA(x,y) plots the data points with + for the positive examples
% and o for the negative examples. X is assumed to be a Mx2 matrix.
% Create New Figure
figure; hold on;
% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
% 2D plot, using the option 'k+' for the positive
% examples and 'ko' for the negative examples.
%
pos = find(y==1); neg = find(y == 0);
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...
'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...
'MarkerSize', 7);
% =========================================================================
hold off;
end
sgmd.m:function g = sgmd(z)
% sigmoid funcion
% g = sgmd(z) calculates the sigmoid of z.
% initial an all-zero matrix to store sigmoid values
g = zeros(size(z));
% ====================== CODING HERE ======================
% Instructions: Calculate the sigmoid of each value of z (z can be a matrix,
% vector or scalar).
% Delete when release
g = 1./(1+exp(-z));
% =============================================================
end
objllfunction.m:function [J, grad] = objllfunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
% parameter for logistic regression and the gradient of the cost
% w.r.t. to the parameters.
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%
z = X*theta;
h_theta = sgmd(z);
J = sum(-y.*log(h_theta)-(1-y).log(1-h_theta))/m;
grad = (X'(h_theta-y))/m;
% =============================================================
end
forecast.m:function p = forecast(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic
%regression parameters theta
% p = PREDICT(theta, X) computes the predictions for X using a
% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)
m = size(X, 1); % Number of training examples
% You need to return the following variables correctly
p = zeros(m, 1);
% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
% your learned logistic regression parameters.
% You should set p to a vector of 0's and 1's
%
z = X*theta;
h_theta = sgmd(z);
p = round(h_theta);
% =========================================================================
end
plotclassifier.m:function plotclassifier(theta, X, y)
%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with
%the decision boundary defined by theta
% PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the
% positive examples and o for the negative examples. X is assumed to be
% a either
% 1) Mx3 matrix, where the first column is an all-ones column for the
% intercept.
% 2) MxN, N>3 matrix, where the first column is all-ones
% Plot Data
plotrawdata(X(:,2:3), y);
hold on
if size(X, 2) <= 3
% Only need 2 points to define a line, so choose two endpoints
plot_x = [min(X(:,2))-2, max(X(:,2))+2];
text% Calculate the decision boundary line plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1)); % Plot, and adjust axes for better viewing plot(plot_x, plot_y) % Legend, specific for the exercise legend('Admitted', 'Not admitted', 'Decision Boundary') axis([30, 100, 30, 100])
else
% Here is the grid range
u = linspace(-1, 1.5, 50);
v = linspace(-1, 1.5, 50);
textz = zeros(length(u), length(v)); % Evaluate z = theta*x over the grid for i = 1:length(u) for j = 1:length(v) z(i,j) = mapFeature(u(i), v(j))*theta; end end z = z'; % important to transpose z before calling contour % Plot z = 0 % Notice you need to specify the range [0, 0] contour(u, v, z, [0, 0], 'LineWidth', 2)
end
hold off
end
lab2data.txt:34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
45.08327747668339,56.3163717815305,0
61.10666453684766,96.51142588489624,1
75.02474556738889,46.55401354116538,1
76.09878670226257,87.42056971926803,1
84.43281996120035,43.53339331072109,1
95.86155507093572,38.22527805795094,0
75.01365838958247,30.60326323428011,0
82.30705337399482,76.48196330235604,1
69.36458875970939,97.71869196188608,1
39.53833914367223,76.03681085115882,0
53.9710521485623,89.20735013750205,1
69.07014406283025,52.74046973016765,1
67.94685547711617,46.67857410673128,0
70.66150955499435,92.92713789364831,1
76.97878372747498,47.57596364975532,1
67.37202754570876,42.83843832029179,0
89.67677575072079,65.79936592745237,1
50.534788289883,48.85581152764205,0
34.21206097786789,44.20952859866288,0
77.9240914545704,68.9723599933059,1
62.27101367004632,69.95445795447587,1
80.1901807509566,44.82162893218353,1
93.114388797442,38.80067033713209,0
61.83020602312595,50.25610789244621,0
38.78580379679423,64.99568095539578,0
61.379289447425,72.80788731317097,1
85.40451939411645,57.05198397627122,1
52.10797973193984,63.12762376881715,0
52.04540476831827,69.43286012045222,1
40.23689373545111,71.16774802184875,0
54.63510555424817,52.21388588061123,0
33.91550010906887,98.86943574220611,0
64.17698887494485,80.90806058670817,1
74.78925295941542,41.57341522824434,0
34.1836400264419,75.2377203360134,0
83.90239366249155,56.30804621605327,1
51.54772026906181,46.85629026349976,0
94.44336776917852,65.56892160559052,1
82.36875375713919,40.61825515970618,0
51.04775177128865,45.82270145776001,0
62.22267576120188,52.06099194836679,0
77.19303492601364,70.45820000180959,1
97.77159928000232,86.7278223300282,1
62.07306379667647,96.76882412413983,1
91.56497449807442,88.69629254546599,1
79.94481794066932,74.16311935043758,1
99.2725269292572,60.99903099844988,1
90.54671411399852,43.39060180650027,1
34.52451385320009,60.39634245837173,0
50.2864961189907,49.80453881323059,0
49.58667721632031,59.80895099453265,0
97.64563396007767,68.86157272420604,1
32.57720016809309,95.59854761387875,0
74.24869136721598,69.82457122657193,1
71.79646205863379,78.45356224515052,1
75.3956114656803,85.75993667331619,1
35.28611281526193,47.02051394723416,0
56.25381749711624,39.26147251058019,0
30.05882244669796,49.59297386723685,0
44.66826172480893,66.45008614558913,0
66.56089447242954,41.09209807936973,0
40.45755098375164,97.53518548909936,1
49.07256321908844,51.88321182073966,0
80.27957401466998,92.11606081344084,1
66.74671856944039,60.99139402740988,1
32.72283304060323,43.30717306430063,0
64.0393204150601,78.03168802018232,1
72.34649422579923,96.22759296761404,1
60.45788573918959,73.09499809758037,1
58.84095621726802,75.85844831279042,1
99.82785779692128,72.36925193383885,1
47.26426910848174,88.47586499559782,1
50.45815980285988,75.80985952982456,1
60.45555629271532,42.50840943572217,0
82.22666157785568,42.71987853716458,0
88.9138964166533,69.80378889835472,1
94.83450672430196,45.69430680250754,1
67.31925746917527,66.58935317747915,1
57.23870631569862,59.51428198012956,1
80.36675600171273,90.96014789746954,1
68.46852178591112,85.59430710452014,1
42.0754545384731,78.84478600148043,0
75.47770200533905,90.42453899753964,1
78.63542434898018,96.64742716885644,1
52.34800398794107,60.76950525602592,0
94.09433112516793,77.15910509073893,1
90.44855097096364,87.50879176484702,1
55.48216114069585,35.57070347228866,0
74.49269241843041,84.84513684930135,1
89.84580670720979,45.35828361091658,1
83.48916274498238,48.38028579728175,1
42.2617008099817,87.10385094025457,1
99.31500880510394,68.77540947206617,1
55.34001756003703,64.9319380069486,1
74.77589300092767,89.52981289513276,1
这是各个文件的代码,帮我写实验报告,多分析,少分点,不要照搬实验指导书
当然可以!下面是一篇逻辑回归实验报告,不照搬指导书,注重个人理解与分析,结构简洁但不简单,内容流畅,留有插图(截图)位置,侧重实验思考与原理分析。
本实验通过实现并训练一个简单的逻辑回归模型,解决“根据两门考试分数预测学生是否被录取”的二分类问题。实验内容涵盖:数据可视化、sigmoid函数编程、逻辑回归目标函数和梯度的实现、参数优化、决策边界绘制与分类准确率评估。重点在于掌握逻辑回归建模、训练和预测全过程,对机器学习最基础的判别模型有更深体会。
实验数据包含100名学生两门考试的成绩和录取标签(0/1)。首先用MATLAB画出散点图,直观查看正负样本的分布规律。一般来说,正例(录取)和负例(未录取)在特征空间中有一定分割性,但也存在交叠。
插图1:原始数据分布
(此处插入plotrawdata(X, y)运行截图,可明显看到“+”和“o”在二维空间中的分布)
分析:
直观上,大部分正例点(+)集中在高分区,负例点(o)则偏向低分区,但两类间存在一定混杂,说明线性模型能捕捉大致趋势,但分界可能不完美。
逻辑回归模型的输出通过sigmoid函数将特征线性组合结果映射为(0,1)区间概率:
代码体会:
实现时要保证z可以为标量、向量或矩阵,便于批量计算预测概率。sigmoid输出直接反映输入属于正类的概率。
逻辑回归采用极大似然思想,损失函数为交叉熵形式:
其中为sigmoid输出。
梯度分析:
梯度推导清楚表明每一维参数的调整方向,有助于理解参数更新为何能不断逼近最优。实验用向量化代码实现,效率和数值稳定性都很高。
本实验用MATLAB自带的fminunc
自动完成参数迭代优化。优化过程中,损失函数持续减小,参数不断逼近最优。
插图2:决策边界与样本分布叠加图
(插入plotclassifier输出截图,可直观看到一条直线将正负样本大致分开)
分析:
实验中发现,决策边界大致沿样本分布主轴划分,但无法完全分割混合区域。这也反映出线性可分假设下的局限性。对于边界附近样本,模型输出概率更接近0.5,体现了不确定性。
对给定(45, 85)分的学生,模型输出约0.77的录取概率,这说明模型已能合理“理解”数据规律。
用训练集进行准确率评估,结果约为89%(具体结果依运行数据为准)。
分析:
模型在训练集上的高准确率说明特征和标签具有一定规律性。部分错误分类点往往位于决策边界附近或少数“异类”样本,进一步说明线性模型的表达能力有限。
实验中采用了默认的优化器步长和轮次,实际上不同的超参数组合会影响收敛速度与结果稳定性。初值设为零,符合经验,但在更复杂问题下初始化方式可能影响收敛质量。
由于本问题仅涉及两个特征,线性模型已能达到较好效果。若数据本身高度非线性,需引入多项式特征等方式提升模型表达力。实际应用时还需注意过拟合和泛化能力。
理论与实践结合
通过手动实现sigmoid、损失函数与梯度,进一步理解逻辑回归背后的数学机制。优化过程中观察损失函数与准确率的变化,也加深了对模型训练与收敛性的体会。
MATLAB工具链应用
熟练掌握了数据可视化、自动优化函数和向量化计算等工具,对日后处理更大规模数据和更复杂模型打下基础。
对线性模型的认识
本实验再现了线性判别模型的基本能力和局限,对边界样本的处理、模型可信度等问题有了直观认识。
逻辑回归作为机器学习中的基础分类算法,具有实现简便、训练高效等优点。通过本实验,从数据可视化到模型训练与测试,对逻辑回归全流程有了系统性认识。后续可以尝试引入正则化、多项式特征、核方法等提升模型泛化和表达能力。
插图1:原始数据分布截图
插图2:逻辑回归决策边界截图
插图3:命令窗口关键输出(如初始/最优损失值、预测概率、训练准确率)
如需更细节的代码注释、英文版或加入更多个人思考,欢迎继续补充说明!