运用随机梯度下降, 小批量梯度下降,批量梯度下降的不同影响

# %%


# %%
# %%
import numpy as np 


# %%
X = 2*np.random.rand(100, 1)
y = 4 + 3 * X + np.random.rand(100, 1)

# %%
import matplotlib.pyplot as plt
plt.plot(X, y, 'b.')
plt.xlabel('x')
plt.xlabel('y')
plt.legend()
plt.axis([0, 2, 0, 15])
plt.show()

# %%
X_b = np.c_[np.ones((100, 1)), X]

# %%
theta = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)

# %%
X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new]
y_pre = X_new_b.dot(theta)
y_pre

# %%
plt.plot(X_new, y_pre, 'r--')
plt.plot(X, y, 'b.')
plt.show()

# %%
from sklearn.linear_model import LinearRegression

# %%
linear_reg = LinearRegression()
linear_reg.fit(X, y)
print(linear_reg.coef_)
print(linear_reg.intercept_)

# %%
theta = np.random.randn(2, 1)
m = 100
iterations = 100
rate = 0.01
for ite in range(iterations):
    gradit = 2/m * X_b.T.dot(X_b.dot(theta) - y)
    theta -= gradit * rate

# %%
X_new_b.dot(theta)

# %%
theta_path_bgd = []
def plot_gradient_descent(theta, eta, theta_path = None):
    m = len(X_b)
    plt.plot(X, y, 'b.')
    n_itera = 1000
    for i in range(n_itera):
        y_pre = X_new_b.dot(theta)
        plt.plot(X_new, y_pre, 'b-')
        gradit = 2/m * X_b.T.dot(X_b.dot(theta) - y)
        theta -= gradit * rate
        if theta_path:
            theta_path.append(theta)
    plt.xlabel("X_1")
    plt.axis([0,2, 0,15])
    plt.title('eta = {}'.format(eta))


# %%
theta = np.random.randn(2,1)
plt.figure(figsize=(10, 4))
plt.subplot(131)
plot_gradient_descent(theta, eta = 0.02)
plt.subplot(132)
plot_gradient_descent(theta, eta=0.1)
plt.subplot(133)
plot_gradient_descent(theta, eta = 0.4)
plt.show()

# %%





# %%
n = np.random.randn(5, 1)
b = np.random.randn(1, 5)

# %%
theta = np.random.randn(2,1)
plt.figure(figsize=(10, 4))
plt.subplot(131)
plot_gradient_descent(theta, eta = 0.02)
theta = np.random.randn(2,1)
print(theta)
plt.subplot(132)
plot_gradient_descent(theta, eta=0.1)
theta = np.random.randn(2,1)
print(theta)
plt.subplot(133)
plot_gradient_descent(theta, eta = 0.8)
plt.show()

# %%
X = 2*np.random.rand(100, 1)
y = 4 + 3 * X + np.random.rand(100, 1)
X_new_b = np.c_[np.ones((100, 1)), X]

# %%
def rand_degrit(sample_num, rate, train_num, theta):
    
    for i in range(train_num):
        # 随机选择多少个样本数
        y_pre = X_new_b.dot(theta)
        start = np.random.randint(0, len(X_new_b) - sample_num - 1)
        train_x = X_new_b[start: start+ sample_num]
        train_y = y[start: start + sample_num]
        plt.plot(np.delete(train_x, 0, axis= 1), train_y, 'b.')
        plt.plot(X, y_pre, 'r-')
        # 计算损失 2/m * X_b.T.dot(X_b.dot(theta) - y)
        delta = 1/sample_num* train_x.T.dot(train_x.dot(theta) - train_y)
        # 更新
        theta = theta - delta * rate
    plt.xlabel('x_1')
    plt.title('samples{}'.format(sample_num))
theta = np.random.randn(2,1)
plt.figure(figsize=(10, 4))
# plt.subplot(131)
rand_degrit(1, 0.1, 10, theta)

# %%
plt.plot(X_new_b, y, 'b.')

# %%
X_new_b


# %%




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东哥爱编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值