/JB5_avatar.webp

Polymer Mechanics and Machine Learning

About this site
I have a PhD in computational mechanics from MIT. This site is where I work through machine learning projects myself and write up what I learn — the theory, the dead ends, and the results. Everything here is educational; all code is open source under the MIT license and available on GitHub.

CNN Cats vs Dogs Classification

Introduction In this example I will use the Kaggle cats and dogs dataset to train a CNN to classify if an image is a cat or a dog. The code below reads all images from a local PetImages folder and splits them into a training and a validation set. Before training, the images are preprocessed with simple data augmentation: random horizontal flips and small rotations are applied to make the model more robust, and the pixel values are rescaled to the range [0, 1].

Backpropagation Part 3: 1 Hidden Layer, N Perceptrons

[1 Input] + [1 Hidden Layer with N Perceptrons] + [1 Output] In this example I will extend my previous code to be able to handle a hidden layer with N perceptrons. As before, I will demonstrate the use of the neural network by fitting it to the following mathematical function: $y = 0.1 + 0.1 \cdot x^2$ over the range $x \in [0, 10]$. The C++ implementation for this example can be found in my github account.

Backpropagation Part 2: 1 Hidden Layer, 2 Perceptrons

[1 Input] + [1 Hidden Layer with 2 Perceptrons] + [1 Output] In this example I will create a simple neural network that has one input, one hidden layer with 2 perceptrons, and one output. I will then fit that neural network to the following mathematical function: $y = 0.1 + 0.1 \cdot x^2$ over the range $x \in [0,10]$. C++ code to solve this problem is listed below: // 2 Layers: 1 input, 2 hidden, 1 output #include <iostream> #include <fstream> #include <algorithm> #include <cassert> #include <vector> #include <cmath> #include <random> double activation(double x, int type) { if (type==1) return std::max(0.

Backpropagation Part 1: Single Perceptron

One Network Layer with 1 Input and 1 Output The simplest neural network architecture consists of a single perceptron. This network has only one input and one output, making it a highly streamlined model. While suitable for theoretical understanding, it is insufficient for real-world applications due to its limited capacity. However, it serves as a fundamental building block for studying the principles of backpropagation and training neural networks. Code Location You can find the source code for this example here.

Install llama.cpp and Gemma from Huggingface

Installation Learning how large language models (LLMs) like ChatGPT and Gemini work can be both fascinating and empowering. While using them through APIs is convenient, running one locally on your own computer unlocks deeper understanding and control. Fortunately, setting up your own LLM on Linux or Windows is surprisingly straightforward. In this article, I will walk you through the simple steps to run a popular LLM like Transformers on your own computer.

Can ChatGPT Predict Hammer Throw Distance?

Introduction In my previous articles in this series I have shown how to calculate how far spherical object will fly based on its initial velocity and angle. I used both physics and machine learning methods to solve the problem. In this article I will examine how much of this can be completely automated using a large language model like ChatGPT 4o. Prompt: I am interested in predicting the distance as a function of the angle and velocity.

Neural Network Analysis of Hammer Throw Distance using PyTorch

Summary In this example I have used a “Sequential” neural network model to solve the regression problem of how far a hammer will fly given an initial velocity and angle. A sequential model is linear stack of layers, and allows you to create a Neural Network (NN) by simply adding layers sequentially. Both PyTorch and Keras are popular frameworks for building these NN models. Compared to Kears, the PyTorch approach is exposes more details which makes it more flexible and suitable for complex and dynamic models.

Neural Network Analysis of Hammer Throw Distance using Scikit-Learn

Summary The MLPRegressor in scikit-learn is a powerful tool for performing regression tasks using a multi-layer perceptron (MLP), which is a type of artificial neural network. It is a supervised learning algorithm that learns a function that maps input data to continuous output values. It can model complex relationships between features and the target variable. Code Location You can find the code in this example here. import matplotlib.pyplot as plt import numpy as np from sklearn.

Neural Network Analysis of Hammer Throw Distance using TensorFlow Keras

Summary The Keras Sequential model is a simple and straightforward way to build neural networks in Keras, a high-level neural networks API running on top of TensorFlow. The Sequential model allows you to stack layers sequentially, meaning each layer has exactly one input tensor and one output tensor. You start by creating an instance of the Sequential model, then add layers to it one by one. Each layer, such as Dense (fully connected), Convolutional, or LSTM (Long Shor-Term Memory), is added using the add method.

Scikit-Learn Regression of Hammer Throw Distance

Linear Regression Linear regression is a fundamental statistical method used in machine learning to model the relationship between a dependent variable (often called the target or outcome) and one or more independent variables (often called features or predictors). In our example of hammer throwing we have 2 inputs (velocity and angle), and one output (distance). The regression equation therefore becomes: $d = b_0 + b_1 v + b_2 \alpha$. The goal of linear regression is to find the values of the coefficients ($b_i$) that minimize the difference between the predicted values and the actual values.