Pydantic

Pydantic Why do we have multiple classes for the same model? For example: from pydantic import BaseModel class UserBase(BaseModel): name: str email: str class UserCreate(UserBase): password: str class User(UserBase): id: int class Config: orm_mode = True Here’s the response from copilot: In Pydantic, we often define multiple schemas like UserBase, UserCreate, and User to handle different use cases and ensure clear separation of concerns. Here’s why we do this: ...

March 3, 2025

Precommit Hook

pre-commit hooks are a way to add hooks that run before the commit process. They are a great way to make sure your code is ready to be committed. Installation Follow platform-specific instructions for installing pre-commit. Or if you have pip installed in your machine. Run: pip install pre-commit Initialization Run the following command to initialize the git hook: pre-commit install Configuration Add a .pre-commit-config.yaml config file in the root folder. Here’s an example: ...

March 3, 2025

Format

There are many tools for linting and formatting Python codes: package usage isort sort imports black format code pylint lint code pep8 lint code autoflake lint code ruff lint and format code Ruff Basic configuration: [tool.ruff] line-length = 120 [tool.ruff.lint] select = ["E501", "I"] [tool.ruff.format] docstring-code-format = true docstring-code-line-length = 88 Since Ruff format doesn’t support sorting import currently, we need to run the linter to sort import: ruff check --select I --fix ruff format

March 3, 2025

FastAPI

Overview FastAPI is a modern, fast web framework for building APIs with Python. It can provide high performance on par with Node.js and Go. Starlette & Uvicorn Both Starlette and Uvicorn are essential components in the FastAPI ecosystem. Starlette starlette is a lightweight ASGI (Async Server Gateway Interface) framework for building web apps FastAPI is built on top of starlette and pydantic, and it abstracts most of the complexity of starlette starlette provides core functionality for handling web requests, responses, routing, middleware and more. Uvicorn: ...

March 3, 2025

Poetry

Overview Poetry is a dependency management and packaging tool for Python. I have been using it for a while and I really love it since it help simplify the process of managing dependencies and virtual environments. Installation If you have pip installed in your machine. Run the following command to install Poetry. Otherwise, follow this guide. pip install poetry Check if Poetry is installed successfully: $ poetry --version Poetry (version 1.8.2) Create New Project To create a new project: ...

March 3, 2025

Hugo Infopage

Info Page To customize the info page for PaperMod based website: params: homeInfoParams: Title: "👋Hello World!" Content: >- Welcome to Huijing Huang's tech blogs! Social Icons To add social icons: params: socialIcons: - name: "github" url: "https://github.com/rewrlution" - name: "linkedin" url: "https://www.linkedin.com/in/huijing-huang/" - name: "email" url: "mailto:sssjtuhuang@gmail.com" Here’s the full list of social icons. Tags To enable the tags mode for the PaperMod theme in Hugo, add tags to the post’s front matter: date: "2025-02-28T14:16:39-08:00" draft: false title: "Hugo Infopage" tags: ["hugo", "PaperMod", "customization"] Configure the config.yaml to include the taxonomies settings: ...

February 28, 2025

XCS330 PS2 Part 1 - Data Processing

Introduction In this blog, I will talk about some tricks that I learned when I worked on the data processing part for assignment 2 of XCS300. Here’s the link to the assignment. This is how the data looks like: Understand Dataset In this assignment, we use the Omniglot dataset that has 1623 hand-written characters from 50 different languages. Each character has 20 (28 x 28) images. Running both the grader.py or main.py will download the dataset to local filesystem. A folder named omniglot_resized will be generated under the src directory, and this is the structure of the directory: ...

February 28, 2025

XCS330 PS2 Part 2 - MANN Architecture

Introduction In this blog, I will talk about the model part for assignment 2 of XCS300. Here’s the link to the assignment. This is the MANN (memory-augmented neural network) architecture: LSTM Here are some useful reading materials for understanding LSTM and RNN: What is LSTM (Long Short Term Memory)? PyTorch Tutorial - RNN & LSTM & GRU PyTorch Tutorial - Name Classification Using A RNN Here’s a nice diagram of LSTM from Understanding LSTM Networks: ...

February 28, 2025

Hugo Math Typsesetting

Display Math Equations To display math equations in Hugo, we need to add math typesettings. I followed the PaperMod - Math Typesetting tutorial since I use it as the template. The tutorial uses KaTex as the library. Step 1: Math.html Create a partial under /layouts/partials/math.html. I use the Auto-render Extension to render the math inside it, and I include the reference to the script: <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.21/dist/katex.min.css" integrity="sha384-zh0CIslj+VczCZtlzBcjt5ppRcsAmDnRem7ESsYwWwg3m/OaJ2l4x7YBZl9Kxxib" crossorigin="anonymous" /> <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.21/dist/katex.min.js" integrity="sha384-Rma6DA2IPUwhNxmrB/7S3Tno0YY7sFu9WSYMCuulLhIqYSGZ2gKCJWIqhBWqMQfh" crossorigin="anonymous" ></script> <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.21/dist/contrib/auto-render.min.js" integrity="sha384-hCXGrW6PitJEwbkoStFjeJxv+fSOOQKOPbJxSfM6G5sWZjAyWhXiTIIAmQqnlLlh" crossorigin="anonymous" onload="renderMathInElement(document.body);" ></script> Step 2: Extend Head.html Then we need to include this partial in extend_head.html. ...

February 23, 2025

Cross Entropy

Cross Entropy In machine learning, the cross-entropy loss is a very popular loss function. Here’s the math definiton of it: $$ H(P^* | P) = - \sum_{i} P^*(i) log P(i) $$ I have been using cross entropy for decades without truly understanding this loss function. Recently, I watched a few YouTube videos and want to share my most recent understanding of it. Essentially, cross entropy is very useful for measuring the difference between two distributions. ...

February 23, 2025