<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>python on Tom Roth</title><link>https://tomroth.dev/tags/python/</link><description>Recent content in python on Tom Roth</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Thu, 16 Feb 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://tomroth.dev/tags/python/index.xml" rel="self" type="application/rss+xml"/><item><title>Building a computational graph: part 3</title><link>https://tomroth.dev/compgraph3/</link><pubDate>Thu, 16 Feb 2023 00:00:00 +0000</pubDate><guid>https://tomroth.dev/compgraph3/</guid><description>This is the third part of a series on creating a computational graph in Python. You can go back to part one and part two.
There&amp;rsquo;s a bit of code in this post. Here is the final code in np_wrapping.py. The module for numpy_autograd is here.
In the last post we created computational graphs for a function, but it was a bit hard to use. We also had these problems:</description></item><item><title>Building a computational graph: part 2</title><link>https://tomroth.dev/compgraph2/</link><pubDate>Wed, 15 Feb 2023 00:00:00 +0000</pubDate><guid>https://tomroth.dev/compgraph2/</guid><description>This is the second post in a series on computational graphs. You can go back to the previous post or ahead to the next post.
If you&amp;rsquo;d like to see what we are working towards in these posts, here is the Github link:
Last time we
looked at computational graphs and their use in autodiff packages. looked at the autodiff problem and the structure of the grad function in autograd showed how Python breaks down expressions to create computational graphs created a simple graph manually using a simplified Node class How do we automatically create a computational graph for a function?</description></item><item><title>Building a computational graph: part 1</title><link>https://tomroth.dev/compgraph1/</link><pubDate>Tue, 14 Feb 2023 00:00:00 +0000</pubDate><guid>https://tomroth.dev/compgraph1/</guid><description>This is the first post in a series on computational graphs. You can go to the next post or skip ahead to the third post.
If you&amp;rsquo;d like to see what we are working towards in these posts, here is the Github link:
Computation graphs have many uses. Here I&amp;rsquo;ll be presenting it as they used for autodifferentiation.
For this post, I&amp;rsquo;ll assume you have some familiarity with backpropagation and autodifferentiation (also known as autodiff).</description></item><item><title>A simple CNN with Pytorch</title><link>https://tomroth.dev/pytorch-cnn/</link><pubDate>Wed, 14 Apr 2021 00:00:00 +0000</pubDate><guid>https://tomroth.dev/pytorch-cnn/</guid><description>Find the code for this blog post here: https://github.com/puzzler10/simple_pytorch_cnn
We will build a classifier on CIFAR10 to predict the class of each image, using PyTorch along the way.
This is basically following along with the official Pytorch tutorial except I add rough notes to explain things as I go. There were a lot of things I didn&amp;rsquo;t find straightforward, so hopefully this piece can help someone else out there. If you&amp;rsquo;re reading this, I recommend having both this article and the Pytorch tutorial open.</description></item><item><title>Python decorators and examples</title><link>https://tomroth.dev/decorators/</link><pubDate>Tue, 11 Feb 2020 00:00:00 +0000</pubDate><guid>https://tomroth.dev/decorators/</guid><description>Decorators without arguments Link to heading Decorators modify functions. They can also modify classes, but they&amp;rsquo;re mainly used with functions.
Decorators are just a way to simplify syntax. Here&amp;rsquo;s an example of a decorator at work.
@some_decorator def add(x,y): return x + y This is the same thing as
def add(x,y): return x + y add = some_decorator(add) Without the decorator, we&amp;rsquo;d call some_decorator on add and assign the answer back to add.</description></item><item><title>How to enable Beta features for Google Speech-to-Text with Python</title><link>https://tomroth.dev/speech-beta/</link><pubDate>Sun, 26 May 2019 00:00:00 +0000</pubDate><guid>https://tomroth.dev/speech-beta/</guid><description>It&amp;rsquo;s a straightforward two-step process.
One: make sure you have version 1.0.0 or later of google-cloud-speech (use !pip freeze | grep google-cloud-speech)
Two: Instead of importing the client libraries like
from google.cloud import speech from google.cloud.speech import enums, types import them like
from google.cloud import speech_v1p1beta1 as speech, storage from google.cloud.speech_v1p1beta1 import enums, types The beta features should now be available for you to use.</description></item><item><title>Change your plot theme for plotly or plotly_express</title><link>https://tomroth.dev/plotly-theme/</link><pubDate>Thu, 18 Apr 2019 00:00:00 +0000</pubDate><guid>https://tomroth.dev/plotly-theme/</guid><description>I made a scatter plot with plotly_express.
It didn&amp;rsquo;t look good. So I wondered how to change the theme.
It took me way too long to figure out.
First I looked through the documentation here. I didn&amp;rsquo;t seem to see any relevant source code. Instead was a bunch of stuff about creating a new theme. Not useful.
Finally I found code like fig.layout.template = 'plotly_dark' buried in an image. Turns out that&amp;rsquo;s how you do it.</description></item><item><title>Let's build a DQN: neural network architectures</title><link>https://tomroth.dev/dqn-nnet/</link><pubDate>Thu, 01 Nov 2018 00:00:00 +0000</pubDate><guid>https://tomroth.dev/dqn-nnet/</guid><description>After building the basic DQN, I started trying some different neural network architectures. I found vastly different results between them.
Spoiler: more complex doesn&amp;rsquo;t equal better. Almost the opposite.
Here are some results for the rewards obtained in each episode. I trained these models on cartpole-v1. Each was trained for 1000 episodes.
Disclaimer: I did only one run with each architectures. I kept other hyperparameters the same as the previous post, but there are likely some interaction effects unaccounted for.</description></item><item><title>Let's build a DQN: simple implementation</title><link>https://tomroth.dev/dqn-simple/</link><pubDate>Wed, 31 Oct 2018 00:00:00 +0000</pubDate><guid>https://tomroth.dev/dqn-simple/</guid><description>Click the image above to see the source code.
Last time, we established some theory for our DQN. Now it’s time to implement it.
We won’t build a full DQN from the DeepMind paper, not yet. Instead we’ll build a simplified version. I call it the basic DQN.
The basic DQN is the same as the full DQN, but missing a target network and reward clipping. We’ll get to that in the next post.</description></item><item><title>Let's build a DQN: basics</title><link>https://tomroth.dev/dqn-basics/</link><pubDate>Tue, 23 Oct 2018 00:00:00 +0000</pubDate><guid>https://tomroth.dev/dqn-basics/</guid><description>In February 2015, Google Brain published a paper combining deep neural networks and reinforcement learning for the first time - called DQN (deep q-network). It was a landmark moment.
DQN was the first algorithm that could successfully play a wide range of Atari games. Other algorithms at the time could perform well at a single game but couldn’t generalise across games. Impressively, DQN was able to perform above human level at a range of Atari games using only the screen pixels as input.</description></item><item><title>How to play the OpenAI Gym environments yourself</title><link>https://tomroth.dev/gym-play/</link><pubDate>Sat, 13 Oct 2018 00:00:00 +0000</pubDate><guid>https://tomroth.dev/gym-play/</guid><description>Ever wanted to play the games that your reinforcement agents are learning?
OpenAI have a keyboard agent to let you try out the gym environments yourself. Here&amp;rsquo;s how to set it up.
First, if you haven&amp;rsquo;t already, install the gym package with pip install gym, or by following the instructions here.
Then:
Go to this link. Click &amp;ldquo;raw&amp;rdquo;. Copy the text and save it to a file called keyboard_agent.py Open up a shell prompt and navigate to where you saved the file.</description></item><item><title>Sarsa, expected sarsa and Q-learning on the OpenAI taxi environment</title><link>https://tomroth.dev/sarsa-qlearning/</link><pubDate>Mon, 08 Oct 2018 00:00:00 +0000</pubDate><guid>https://tomroth.dev/sarsa-qlearning/</guid><description>In this post, we&amp;rsquo;ll see how three commonly-used reinforcement algorithms - sarsa, expected sarsa and q-learning - stack up on the OpenAI Gym Taxi (v2) environment.
Note: this post assumes that the reader is familiar with basic RL concepts. A good resource for learning these is the textbook by Sutton and Barto (2018), which is freely available online.
Sarsa, Expected Sarsa and Q-Learning Link to heading So, what are these algorithms?</description></item><item><title>Exploring policy evaluation in reinforcement learning</title><link>https://tomroth.dev/policy-eval/</link><pubDate>Sun, 27 May 2018 00:00:00 +0000</pubDate><guid>https://tomroth.dev/policy-eval/</guid><description>Today, we explore policy evaluation. We&amp;rsquo;ll explain it by example.
Given a policy, policy evaluation aims to find the value of each state $ v_\pi (s) $. That&amp;rsquo;s what we&amp;rsquo;ll be doing with the equiprobable random policy.
The scenario Link to heading Taken from the textbook &amp;ldquo;An Introduction to Reinforcement Learning&amp;rdquo; (Sutton and Barto), Gridworld is the following scenario.
There is a grid with sixteen squares. The top left and the bottom right square are terminal squares.</description></item><item><title>The Keras functional API: five simple examples</title><link>https://tomroth.dev/keras/</link><pubDate>Mon, 06 Nov 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/keras/</guid><description>Building models in Keras is straightforward and easy. If you&amp;rsquo;re reading this, you&amp;rsquo;re likely familiar with the Sequential model and stacking layers together to form simple models. But what if you want to do something more complicated?
Enter the functional API. For complex models the functional API is really the only way to go - it can do all sorts of things that just aren&amp;rsquo;t possible with the Sequential model. Models with multiple inputs and outputs, models with shared layers - once you start designing architectures that need these things, you will have to use the functional API to build your model.</description></item><item><title>Building a nearest neighbour classifier in Python</title><link>https://tomroth.dev/nearest-neighbour/</link><pubDate>Sat, 26 Aug 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/nearest-neighbour/</guid><description>The nearest neighbour classifier is a very simple algorithm for image classification. While not used much in practice, it is simple to implement and it helps to gain a deeper understanding of the problems in image classification.
Just like other classifiers, if we give the nearest neighbour classifier an image, it&amp;rsquo;ll try and find its closest match. We &amp;ldquo;train&amp;rdquo; the classifier by giving it a large collection of images that it allowed to search through to find matches.</description></item><item><title>A quick overview of Seaborn</title><link>https://tomroth.dev/seaborn/</link><pubDate>Mon, 17 Jul 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/seaborn/</guid><description>Seaborn. A wrapper on top of matplotlib. Used to make plots, and to make them quicker, easier, and more beautiful.
Thank you for your service, matplotlib. Despite your flaws, you&amp;rsquo;ve guided us this far.
But it&amp;rsquo;s time to step aside.
Types of Seaborn plots Link to heading sns.boxplot() | generic boxplot sns.distplot() | histogram and kernel density estimate (KDE) plotted together sns.distplot(rug=True) | rugplot sns.kdeplot() | kernel density estimate plot sns.</description></item><item><title>How to install packages through Pip into a Conda environment</title><link>https://tomroth.dev/pip-conda/</link><pubDate>Fri, 14 Jul 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/pip-conda/</guid><description>Seemingly ubiquitous in the Python world, the distribution Anaconda comes with over 200 Python packages installed. Anaconda also comes with a package manager called conda.
Besides its utility for installing and managing packages, conda also possesses the ability to create virtual environments which make sharing and reproducing analyses much easier. These virtual environments are created without any Python packages preloaded into them.
Installing Python packages into the virtual environment is often straightforward.</description></item><item><title>Pandas functions. Lots of them.</title><link>https://tomroth.dev/pandas/</link><pubDate>Fri, 14 Jul 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/pandas/</guid><description>Pandas is your best friend for your data needs. It is the king of data manipulation in the Python empire.
Any data scientist intending to use Python as their tool of choice must master Pandas. It is compulsory, like learning to walk before you run.
So here is a quick reference list of functions, just for you.
But reading written material is no substitute for repeated practice. And hence, you should not expect to remember the functions below.</description></item><item><title>44 Numpy Functions</title><link>https://tomroth.dev/numpy/</link><pubDate>Sun, 25 Jun 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/numpy/</guid><description>Have you heard about the Python package numpy? Probably.
But maybe you&amp;rsquo;re looking for an easy reference of useful numpy functions. Maybe that&amp;rsquo;s why you&amp;rsquo;re here. Well, you&amp;rsquo;re in luck!
Get started by opening up a editor. First, we&amp;rsquo;ll import the numpy package: import numpy as np.
Now cast your eye over these functions. In no particular order:
np.__version__ | Return the version of numpy you have loaded. np.shape(x) | return the shape of an array x.</description></item><item><title>Understanding Kadane’s solution to the maximum subarray problem</title><link>https://tomroth.dev/kadane/</link><pubDate>Tue, 09 May 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/kadane/</guid><description>There is something called the maximum subarray problem, and it goes something like this.
You are given an array of numbers: something like 3,5,-2,-1,5,3,1,-4,-6,5. Your job is to come up with the subarray with the highest sum.
For this example it would be the subarray [3,5,-2,-1,5,3,1], which has a sum of 14. No other subarray gives a higher sum.
Let&amp;rsquo;s look at a few more:
The sequence -3,-3,-2,-4,-2,-5,-4 has the highest sum being -2, given by the subarray [-2].</description></item></channel></rss>