Skip to content

Instantly share code, notes, and snippets.

View hzhu212's full-sized avatar

Henry Zhu hzhu212

  • Beijing, China
View GitHub Profile
@hzhu212
hzhu212 / positional_encoding.md
Created March 18, 2025 02:37
位置编码-Positional Encoding

记录 Transformer 模型中几种典型位置编码的实现。

1. 默认位置编码

2. 旋转位置编码

class RotaryPositionalEncoding(nn.Module):

在分类问题中,FocalLoss 是一种解决标签分布不均的方法,并使模型专注于学习困难样本。例如,在医学影像检测中,负样本数量显著大于正样本。FocalLoss 原始论文中只讨论了二分类任务,缺少针对多分类问题的相应实现,这里记录一下实现方法。

方法1

class MultiClassFocalLoss(nn.Module):
    def __init__(self, gamma: float = 2.0, alpha: torch.Tensor = None, reduction: str = 'mean', ignore_index: int = -100):
        """
        Args:
            gamma (float): 调制因子越大越关注困难样本 (γ >= 0)。
@hzhu212
hzhu212 / multi_task_learning.md
Last active March 18, 2025 02:05
多目标学习/multi-task learning

在多目标学习中,其中一个难题是如何平衡不同目标之间的 loss 及其学习速率,从而避免模型被其中一个目标所主导,导致其他目标不能得到充分学习。这里简要介绍几种方法。

1. 简单平均法

直接将多个 Loss 加权平均,权重为超参数(通常初始化为等权重)。

公式

$$\mathcal{L}{\text{total}} = \sum{i=1}^N w_i \mathcal{L}_i \quad \text{(默认 } w_i=1.0 \text{)}$$

@hzhu212
hzhu212 / coderun.json
Last active April 22, 2024 06:51
coderun-echarts
{"config":{"codeTheme":"OneDarkPro","pageThemeSyncCodeTheme":true,"openAlmightyConsole":true,"autoRun":true,"layout":"edit","keepPreviousLogs":true,"codeFontSize":14},"title":"coderun-echarts","code":{"HTML":{"language":"html","content":"<!-- 为ECharts准备一个具备大小(宽高)的Dom -->\n<div id=\"main\" style=\"width: 800px; height:600px;\"></div>","resources":[]},"CSS":{"language":"css","content":"","resources":[]},"JS":{"language":"javascript","content":"// 基于准备好的dom,初始化echarts实例\nvar myChart = echarts.init(document.getElementById(\"main\"));\n\n// 指定图表的配置项和数据\n// prettier-ignore\nconst hours = [\n '0','1星','2星','3星','4星','5星','6'\n];\n\n// prettier-ignore\nconst days = [\n '智驾车型覆盖度',\t'自动泊车',\t'记忆泊车',\t'高速LCC',\t'城区LCC',\t'高速领航辅助驾驶',\t'城区领航辅助驾驶'\n];\n\n// prettier-ignore\nconst data = [[0, 3, 1], [0, 4, 4], [0, 5, 1], [1, 4, 6], [2, 3, 1], [2, 5, 3], [3, 5, 6], [4, 4, 3], [4, 5, 3], [5, 3, 1], [5, 4, 2], [5, 5, 3], [6, 3, 1], [6, 4, 2], [6, 5, 2]];\nconst title = [];\nconst singleAxis = [];\nconst series = [];\ncon
@hzhu212
hzhu212 / competitive_parallel.py
Last active April 23, 2024 03:54
Python competitive parallel / race concurrent / ThreadPool / ProcessPool
from concurrent.futures import as_completed
from concurrent.futures.process import ProcessPoolExecutor
import os
import psutil
import time
def kill_child_processes(parent_pid):
try:
parent = psutil.Process(parent_pid)
package com.example.udf;
import com.google.common.base.Throwables;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
@hzhu212
hzhu212 / python_shell.md
Created May 12, 2021 12:50
Wrap shell command with subprocess

Wrap shell command with subprocess

create a simple HDFS client for example:

import logging
import subprocess
from typing import List, Tuple, Union, TextIO, BinaryIO
@hzhu212
hzhu212 / python_retry.md
Last active May 12, 2021 12:23
A simple Python retrying decorator

A simple Python retrying decorator

Define:

import functools
import logging

logger = logging.getLogger(__name__)
@hzhu212
hzhu212 / jupyter.md
Last active December 7, 2023 01:59
一些 Jupyter 技巧

任意一个 cell 出现异常执行 callback

# this function will be called on exceptions in any cell
def when_exception(shell, etype, evalue, tb, tb_offset=None):
    # still show the error within the notebook, don't just swallow it
    shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset)

    err_msg = f'Unexpected exception occured: {repr(evalue)}'
 logger.error(err_msg)
@hzhu212
hzhu212 / python_protobuf.md
Last active December 11, 2020 08:21
python protobuf wrapper

A wrapper for Python protobuf object:

class PbMessage(object):
    """protobuf 消息的包装类,封装一些通用方法"""

    def __init__(self, pb_class):
        self._pb_class = pb_class
        self._pb_obj = None
        self._raw_msg = None