Python Decorators: A complete guide to decorators in Python

Quick answer: A Python decorator is a function that takes another function, wraps it with extra behavior, and returns the new version without changing the original code. Writing @timer above a function is shorthand for func = timer(func). Decorators work because Python functions are first-class objects (they can be passed and returned like any value) and because inner functions form closures that remember the original function. A well-written decorator uses *args and **kwargs to accept any arguments, returns the wrapped function’s result, and applies @functools.wraps to keep the original name and docstring. To pass settings, such as @timer(unit="ms"), you add an outer layer called a decorator factory. Multiple decorators stack bottom-up, so @a above @b means a(b(func)). Decorators can also be classes with a __call__ method (useful for keeping state), can wrap methods and whole classes, need an async wrapper for async functions, and power built-ins like @property, @staticmethod, @classmethod, and @functools.lru_cache. This guide explains each concept with one running example: a slow weather-fetching function that we time, log, count, and cache.

Read More

Fighting hallucinations with 15 lines of code using Self-Correcting RAG

The recent advancement in Generative AI and release of ChatGPT shook the world to its core, inciting both excitement and fear for the future. The advancement in artificial intelligence and capabilities of LLMs have made headlines ever since. But, soon after its release, people began exposing its flaws. Often, the 175 billion parameter natural language processing model would fail provide its users with relevant responses. It frequently provided incorrect answers to questions even a kid could solve. Furthermore, it was much easier to trigger an incorrect response. For instance, if you asked the model “What is 3 + 2?” and the model responded with 5 as the answer, you could just tell the model it is 6 and the model would agree. Misinformation is another problem with these large language models(LLMs). Recently, Air Canada had to pay a hefty fine because its chatbot provided false information regarding its refund policy to its traveller. This phenomenon where a LLM generates nonsensical or inaccurate information to user query is called hallucination.

Read More

Imbalanced dataset in machine learning

Data is crucial to train machine learning(ML) models. An ML model is only as good as the data it feeds on. However, data used to train models may have several deformities, for example:

Read More

ResNet - Why residual units work?

In a convolutional neural network, convolutional layers learn hiearchical features. The lower layers learn low-level features like edges, corners, etc. and higher layers high level features like mouth, ears, tail,etc. Increasing the network depth, as demonstrated by InceptionNet, allows the network to learn better representations and improves the performance of the network. However, optimizing such deep networks is not that easy.

Read More

You're up and running!

Next you can update your site name, avatar and other options using the _config.yml file in the root of your repository (shown below).

Read More