【自用学习笔记】损失函数——求模型权重W

损失函数——求模型权重W

随机搜索

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split

# 加载数据集
digits = datasets.load_digits()
X = digits.data   # 特征
y = digits.target # 标签

# 将标签转换为二分类问题(例如:数字0 vs 非数字0)
y_binary = (y == 0).astype(int)

# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y_binary, test_size=0.2, random_state=42)

# 定义铰链损失函数
def hinge_loss(W, X, y):
    # 计算预测值
    scores = np.dot(X, W)
    # 计算损失
    loss = np.maximum(0, 1 - scores * y)
    return np.mean(loss)

# 随机搜索优化权重W
def random_search(train_data, train_labels, num_iterations=100):
    best_loss = float('inf')
    best_W = None
    for i in range(num_iterations):
        W = np.random.randn(train_data.shape[1])
        loss = hinge_loss(W, train_data, train_labels)
        if loss < best_loss:
            best_loss = loss
            best_W = W
        print(f"第{i}次,当前损失值为{loss},最小损失值为{best_loss}")
    return best_W

# 执行随机搜索
W_best = random_search(X_train, y_train)

# 评估模型
train_loss = hinge_loss(W_best, X_train, y_train)
test_loss = hinge_loss(W_best, X_test, y_test)
print("最优权重的训练损失:", train_loss)
print("最优权重的测试损失:", test_loss)

随机本地搜索

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split

# 加载数据集
digits = datasets.load_digits()
X = digits.data  # 特征
y = digits.target  # 标签

# 将标签转换为二分类问题(例如:数字0 vs 非数字0)
y_binary = (y == 0).astype(int) * 2 - 1  # 将标签转换为 +1/-1


# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(
    X, y_binary, test_size=0.2, random_state=42
)


# 定义铰链损失函数
def hinge_loss(W, X, y):
    # 计算预测值
    scores = np.dot(X, W)
    # 计算损失
    loss = np.maximum(0, 1 - scores * y)
    return np.mean(loss)


# 随机本地搜索优化权重W
def random_local_search(train_data, train_labels, num_iterations=1000, lr=0.1):
    best_W = np.random.randn(train_data.shape[1])
    best_loss = hinge_loss(best_W, train_data, train_labels)
    for i in range(num_iterations):
        new_W = best_W + np.random.randn(train_data.shape[1]) * lr
        loss_new = hinge_loss(new_W, train_data, train_labels)
        if loss_new < best_loss:
            best_loss = loss_new
            best_W = new_W
        print(f"第{i}次,当前损失值为{loss_new},最小损失值为{best_loss}")
    return best_W


# 执行随机搜索
W_best = random_local_search(X_train, y_train)

# 评估模型
train_loss = hinge_loss(W_best, X_train, y_train)
test_loss = hinge_loss(W_best, X_test, y_test)
print("最优权重的训练损失:", train_loss)
print("最优权重的测试损失:", test_loss)

梯度下降法(解析梯度)

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split

# 加载数据集
digits = datasets.load_digits()
X = digits.data  # 特征
y = digits.target  # 标签

# 将标签转换为二分类问题(例如:数字0 vs 非数字0)
y_binary = (y == 0).astype(int) * 2 - 1  # 将标签转换为 +1/-1


# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(
    X, y_binary, test_size=0.2, random_state=42
)


# 定义铰链损失函数
def hinge_loss(W, X, y):
    # 计算预测值
    scores = np.dot(X, W)
    # 计算损失
    loss = np.maximum(0, 1 - scores * y)
    return np.mean(loss)


# 随机本地搜索优化权重W
def random_local_search(train_data, train_labels, num_iterations=1000, lr=0.1):
    best_W = np.random.randn(train_data.shape[1])
    best_loss = hinge_loss(best_W, train_data, train_labels)
    for i in range(num_iterations):
        new_W = best_W + np.random.randn(train_data.shape[1]) * lr
        loss_new = hinge_loss(new_W, train_data, train_labels)
        if loss_new < best_loss:
            best_loss = loss_new
            best_W = new_W
        print(f"第{i}次,当前损失值为{loss_new},最小损失值为{best_loss}")
    return best_W


# 执行随机搜索
W_best = random_local_search(X_train, y_train)

# 评估模型
train_loss = hinge_loss(W_best, X_train, y_train)
test_loss = hinge_loss(W_best, X_test, y_test)
print("最优权重的训练损失:", train_loss)
print("最优权重的测试损失:", test_loss)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值