Skip to content

Instantly share code, notes, and snippets.

View AtomicVar's full-sized avatar
🇨🇳

AtomicVar AtomicVar

🇨🇳
  • Zhejiang University
  • Hangzhou, China
  • 16:25 (UTC +08:00)
View GitHub Profile
@AtomicVar
AtomicVar / ln-from-bn.md
Last active February 15, 2024 10:46
使用 Batch Normalization 实现 Layer Normalization

使用 Batch Normalization 实现 Layer Normalization

Batch Normalization 和 Layer Normalization 是深度学习中常用的两种归一化方法,都可以用以下公式表示:

$$ y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta $$

其主要区别就在于 $\mathrm{E}[x]$$\mathrm{Var}[x]$ 是怎么计算的。

@AtomicVar
AtomicVar / call-py-from-cpp.cpp
Created December 17, 2023 14:13
Call Python from C++
/**
* @file main.cpp
* @brief 演示如何在 C++ 中将数据传递给 Python,然后在 Python 中用 NumPy 进行计算,最后将结果返回给
* C++。
* @author Guo Shuai (gs0801@foxmail.com)
*/
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/numpy.h>
@AtomicVar
AtomicVar / opencv-inpaint.ipynb
Last active December 6, 2023 08:52
演示如何使用 OpenCV 图像修复功能填补缺失值
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@AtomicVar
AtomicVar / format-from-dali.md
Created December 3, 2023 09:32
一些用于格式化字符串的工具函数(来自 NVIDIA DALI)

使用 make_string()make_string_delim() 将对象格式化为字符串(加分隔符)再拼接起来,类似 Python 里的 str.join()

/**
 * @brief Prints args to a string, without any delimiter
 */
template <typename... Args>
std::string make_string(const Args &... args) {
  std::stringstream ss;
  print(ss, args...);
 return ss.str();
@AtomicVar
AtomicVar / zlib-demo.c
Created November 26, 2023 18:03
zlib 极简示例:压缩 100 个 float
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#define BUFFER_SIZE 100
int main() {
float buffer[BUFFER_SIZE]; // Assuming you have a buffer of 100 floats
// Fill the buffer with some data (for demonstration purposes)
@AtomicVar
AtomicVar / HumanReadable.hpp
Created November 26, 2023 10:00
将字节数打印为人类易读的格式(KB/MB/GB)
struct HumanReadable
{
std::uintmax_t size{};
private:
friend std::ostream& operator<<(std::ostream& os, HumanReadable hr)
{
int o{};
double mantissa = hr.size;
for (; mantissa >= 1024.; mantissa /= 1024., ++o);
@AtomicVar
AtomicVar / LRUCache.hpp
Last active February 29, 2024 06:59
A simple LRU Cache in C++ STL.
#include <list>
#include <unordered_map>
#include <utility>
using namespace std;
class LRUCache {
size_t capacity;
list<pair<int, int>> dlist;
@AtomicVar
AtomicVar / conv1d-bmm.py
Created October 30, 2023 07:00
批次矩阵乘法 等价于一种特殊的 Conv1D
"""
表明:批次矩阵乘法 等价于一种特殊的 Conv1D
"""
import torch
N = 2
Cin = 3
Lin = 4
Cout = 5
@AtomicVar
AtomicVar / fuck-matplotlib.py
Created September 26, 2023 13:29
FUCK MATPLOTLIB CHINESE DISPLAY
""" 如何让 Matplotlib 正确显示中文 """
""" 第一步:通过字体路径创建一个字体属性对象 """
from matplotlib.font_manager import FontProperties
my_font = FontProperties(fname='/Path/To/Your/Font.ttf')
""" 第二步:在所有需要显示中文的绘图命令中增加 fontproperties=my_font 或 prop=my_font 参数 """
plt.xlabel('线程池线程数', fontproperties=my_font)
@AtomicVar
AtomicVar / showOpenCVMat.mm
Created August 20, 2023 09:02
Display a OpenCV Mat using a UIImageView.
void showOpenCVMat(cv::Mat img, UIImageView* imageView) {
cv::Mat converted;
img.convertTo(converted, CV_8U);
// convert the cv::Mat to UIImage
NSData *data = [NSData dataWithBytes:converted.data length:converted.elemSize() * converted.total()];
CGColorSpaceRef colorSpace =
CGColorSpaceCreateDeviceGray(); // CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider =
CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
CGImageRef imageRef = CGImageCreate(