# AI

## How to call OpenAI’s ChatGPT API

Here is how you can call OpenAI’s ChatGPT API, given that you have an API key. Follow these instructions to get one. import openai   openai.api_key = ‘<your API key>’   openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who won the world series in 2020?"}, {"role": "assistant", "content": …

## Pyro’s introduction to probabilistic programming

https://pyro.ai/examples/intro_long.html

## How to sort numbers with an evolutionary algorithm (CMA-ES)

Yes, this is clearly nonsense. Sorting is not a hard problem and standard algorithms such as quicksort and mergesort have O(x^2) and O(n log(n)) complexity. But let me scratch this itch of sorting numbers using an evolutionary algorithm, specifically Covariance matrix adaptation evolution strategy (CMA-ES). Technically, we will use what I think is the original …

## How to compute and plot Bollinger BandsĀ® in Python

The aim is to produce a plot like this. The orange line is your data, the green line is the upper "bollinger" band, the blue line is the lower "bollinger" band. The red dots indicate where your data is either above or below the bands. Copy-paste this code: import pandas as pd import numpy as …

## How to scrape images from the web

I’m interested in object detection and other computer vision tasks. For example, I’m working on a teddy-bear detector with my son. So, how do you quickly download images for a certain category? You can use this approach that I learned from a course on Udemy. In the above example, the crawler will find images in …

## How to sample from softmax with temperature

Here is how to sample from a softmax probability vector at different temperatures. import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import seaborn as sns   mpl.rcParams[’figure.dpi’]= 144   trials = 1000 softmax = [0.1, 0.3, 0.6]   def sample(softmax, temperature): EPSILON = 10e-16 # to avoid taking the log of …

## How to do backpropagation in Numpy

I have adapted an example neural net written in Python to illustrate how the back-propagation algorithm works on a small toy example. My modifications include printing, a learning rate and using the leaky ReLU activation function instead of sigmoid. import numpy as np   # seed random numbers to make calculation # deterministic (just a …