当前位置: 首页 > article >正文

面试手撕笔记ML/DL

数据集

数据集的批处理迭代器

Deep-ML | Batch Iterator for Dataset

实现一个批量可迭代函数,该函数在numpy数组X和可选numpy数组y中进行采样。该函数应该生成指定大小的批量。如果提供了y,则该函数应生成(X, y)对的批次;否则,它应该只产生X批次。

Example:

Input:

X = np.array([[1, 2], 
                  [3, 4], 
                  [5, 6], 
                  [7, 8], 
                  [9, 10]])
    y = np.array([1, 2, 3, 4, 5])
    batch_size = 2
    batch_iterator(X, y, batch_size)

Output:

[[[[1, 2], [3, 4]], [1, 2]],
     [[[5, 6], [7, 8]], [3, 4]],
     [[[9, 10]], [5]]]
import numpy as np

def batch_iterator(X, y=None, batch_size=64):
	n_samples = X.shape[0]
	batches = []
	for i in range(0, n_samples, batch_size):
		begin, end = i, min(i + batch_size, n_samples)
		if y is not None:
			batches.append([X[begin:end], y[begin:end]])
		else:
			batches.append(X[begin:end])
	return batches

激活函数

sigmoid

题解


import math
def sigmoid(z: float) -> float:
   result = 1 / (1 + math.exp(-z))
   return round(result, 4)

梯度下降

使用梯度下降的线性回归(MSE)

题解


import numpy as np
def linear_regression_gradient_descent(X: np.ndarray, y: np.ndarray, alpha: float, iterations: int) -> np.ndarray:
    m, n = X.shape
    theta = np.zeros((n, 1))
    for _ in range(iterations):
        predictions = X @ theta
        errors = predictions - y.reshape(-1, 1)
        updates = X.T @ errors / m
        theta -= alpha * updates
    return np.round(theta.flatten(), 4)

MSE 损失的多种梯度下降

 题解

import numpy as np

def gradient_descent(X, y, weights, learning_rate, n_iterations, batch_size=1, method='batch'):
    m = len(y)
    
    for _ in range(n_iterations):
        if method == 'batch':
            # Calculate the gradient using all data points
            predictions = X.dot(weights)
            errors = predictions - y
            gradient = 2 * X.T.dot(errors) / m
            weights = weights - learning_rate * gradient
        
        elif method == 'stochastic':
            # Update weights for each data point individually
            for i in range(m):
                prediction = X[i].dot(weights)
                error = prediction - y[i]
                gradient = 2 * X[i].T.dot(error)
                weights = weights - learning_rate * gradient
        
        elif method == 'mini_batch':
            # Update weights using sequential batches of data points without shuffling
            for i in range(0, m, batch_size):
                X_batch = X[i:i+batch_size]
                y_batch = y[i:i+batch_size]
                predictions = X_batch.dot(weights)
                errors = predictions - y_batch
                gradient = 2 * X_batch.T.dot(errors) / batch_size
                weights = weights - learning_rate * gradient
                
    return weights


http://www.kler.cn/a/465503.html

相关文章:

  • 约瑟夫问题
  • 【项目开发】C#环境配置及VScode运行C#教程(学生管理系统)
  • vue3 vite 动态加载路由遇到的问题
  • 计算机网络基础(7)中科大郑铨老师笔记
  • zsh 配置备忘
  • 【踩坑记录】uni-app 微信小程序调试不更新问题解决指南
  • 教程:从pycharm基于anaconda构建机器学习环境并运行第一个 Python 文件
  • 【UE5 C++课程系列笔记】19——通过GConfig读写.ini文件
  • 网络原理之TCP和UDP
  • Java-多种方法实现多线程卖票
  • 在mac上通过Vundle安装YouCompleteMe(YCM)
  • LeetCode题练习与总结:超级洗衣机--517
  • vue,使用unplugin-auto-import避免反复import,按需自动引入
  • Dpath之详解(Detailed Explanation of Dpath)
  • 借助 FinClip 跨端技术探索鸿蒙原生应用开发之旅
  • spring boot IDEA启动两个端口服务nginx负载
  • 如何使用Python自动化发送消息:用pynput库批量输入并发送文本
  • 网络安全:交换机技术
  • leetcode 面试经典 150 题:多数元素
  • 工信部电子标准院计算机视觉证书报考指南!
  • 项目引入MybatisPlus
  • npm提示Install fail! Error_ EBUSY_ resource busy or
  • STM32G431收发CAN
  • python的urllib模块和http模块
  • stm32f103zet6 ds18b20
  • openbmc sdk09.03 适配(一)