# 深度学习——练习

1、导入数据

m = length(y);

x = [ones(m,1),x];

#### 具体解决过程

>> theta = zeros(size(x(1,:)))';
>> theta
theta =
0
0
>> MAX_ITR = 1500;
>> alpha = 0.07;
>> for num_iterations = 1:MAX_ITR
grad = (1/m).* x' * ((x * theta) - y);
theta = theta - alpha .* grad;
end
>> theta
theta =
0.7502
0.0639
>> plot(x(:,2),y,'o');
>> hold on
>> plot(x(:,2), x*theta, '-')
>> legend('Training data', 'Linear regression')


>> predict1 = [1,3.5] * theta

predict1 =

0.9737

>> predict2 = [1, 7] * theta

predict2 =

1.1973

>> J_vals = zeros(100, 100); % initialize Jvals to 100x100 matrix of 0's
>> theta0_vals = linspace(-3, 3, 100);
>> theta1_vals = linspace(-1, 1, 100);
>> 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
>> J_vals = J_vals';
>>figure;
>> surf(theta0_vals, theta1_vals, J_vals);
>>xlabel('\theta_0');
>> ylabel('\theta_1')


contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15))

xlabel('\theta_0'); ylabel('\theta_1')

posted @ 2014-05-08 17:17 越影&逐日而行 阅读(...) 评论(...) 编辑 收藏