# A first attempt at machine learning

Today, I’ve generated my first results using a machine learning algorithm. I don’t understand the theory behind the model well enough yet to describe it, but will be working on that later this week, so watch this space.

One of the first challenges for anyone wanting to adopt machine learning is the huge choice of algorithms. Probably the most famous are neural networks but there are numerous others with names such as Support Vector Machines and Reducing Kernel Hilbert Space methods (a personal favourite, just for the impenetrability of the name). The family of methods that I’ve been trying to understand are known as Gaussian Processes. I’ll talk more about what these are and why I’ve chosen them in a later blog, but for now let’s just look at what they predict, returning to my stress/extension data:

You’ll notice that the curve is smoother than the smoothed spline I used previously. Even better is that the output from this model is predictive rather than descriptive, in the sense that it is straightforward to provide the algorithm with new extension values and ask for the most probable stress. That is how I generated the curve above.

Gaussian process models are available in Matlab as part of their Regression Learner apps, I’ve found them helpful as a starting point, but a little too restrictive in terms of understanding what is happening underneath the hood of the engine. I assume that other mathematical software packages also have Gaussian Process capabilities. If you use Matlab, then feel free to play with the code that I’ve written. You can download the zip file with the .mlx live script function that does the training and testing, the two .mat files with the training data and the .m GP model.

I’ve started to use GPy which is a package within Python. It is more powerful than the Matlab version of Gaussian Processes, as well as free. Here is the GPy prediction based on the same training data:

You will notice differences between the predictions of the Matlab and the GPy models. The GPy version seems more faithful to the data but doesn’t predict a stress of zero with an extension ratio of one. There is no particular reason it should. As far as machine learning is concerned the (0,1) data point has no more meaning than any of the other data points and it doesn’t know anything about the underlying physics, so I’m actually surprised that the Matlab version does pass through (0,1). I’ll try to figure out why this is the case! One of the reasons I like the GPy version is that it is easier to explore what is happening within the model, which gives me greater confidence. As an example, I can look at what it predicts before I optimise:

One of the nice features of the GPy package is that blue shaded region indicates the range of confidence in the predicted stress values. As you would expect the range of confidence is much narrower for the optimised case. Again, feel free to download the .py file and the data.

In both cases, I encourage you to replace the stress and extension data with any x, y dataset of your own and build your own machine learning Gaussian Process! Perhaps you can upload results in the comments section of the blog?

# Machine learning vs physics learning. A physicist’s view of the machine learning approach.

Cubic splines.

Still reading? Good. In the second of my two part blog I’ll introduce non-parametric learning. The most important thing to understand about non-parametric learning is that it is not non-parametric. Now that we’ve cleared that up …

Thanks to those of you who voted following my previous blog. You’ll find the results at the end of this blog. So what’s going on in the three figures I posted earlier?

Figure 1 is the simplest and, as, probably, you’ve already guessed, each data point is joined to its neighbours by straight lines. From experience we tend to think that this it is not very likely that if we took measurements between the existing data that they would fall on these lines. We would expect the data to vary more smoothly.

Figure 3 is generated from the best fit of the relationship between stress and extension that arises from some simple assumptions and application of the idea of entropy, as I mentioned here. In terms of learning about physics, this representation could be considered to provide the most learning, albeit that we are learning that our model is too simplistic. By just comparing the shapes of the data with the curve, we can infer that we need a model with more parameters to describe physics not included in our simple model. This is parametric physics learning.

If we aren’t attempting to fit a physics relationship but believe that our data is representative of an underlying trend, what options are there?

Figure 2 is generated using a “smoothing spline”. This is a neat way of attempting to find a way to interpolate data based on the data alone rather than any beliefs about what might cause a particular relation between extension and stress. A smoothing spline is an extension of the cubic splines which is a type of non-parametric learning, The cubic spline describes locally the curve at each data point as a cubic equation. In this case, in contrast to the physics approach, we do not impose a global relationship. This means that knowing the value of the data of the first point tells us nothing about the value at, say, the 10th point of measurement. The physics approach would enable us to make this inference, but as we can see from the figure 2, in some cases, it wouldn’t be a very good prediction!

You may be wondering how we can define a cubic for each individual data point. A cubic equation has four parameters, so we have four unknowns and only one data point. To find the other unknowns, we add assumptions such as the slope and curvature of the curve connecting adjacent points are equal, which guarantees the appearance of smoothness. The math behind this is cumbersome to write down and involves a great deal of repetitious calculations, which is why it only became popular with the advent of computers.

A smoothing cubic spline extends the idea of a cubic spline so it can deal with noisy data, that is data that varies about the expected mean. Without this, the cubic spline can become quite spiky when the data is noisy, so a smoothed spline relaxes the demand that the curve pass through every data point and instead looks for a compromise curve that is smooth but never too far from the data. This requires the introduction of another (non?) parameter, unsurprisingly called the smoothing parameter. When this is zero it reproduces a cubic spline fit that passes through all the data points, when it is one it fits a straight line through the data. The best choice of smoothing parameter requires us to introduce some arbitrary measure of what good looks like, but statisticians have come up with ways of quantifying and measuring this, a topic of a future blog.

In what way, then, is this approach non-parametric? In parametric learning, the number of parameters is dictated by the model we have chosen to fit our data. For the stress-extension entropy model, we have just one parameter. We believe that more data will either improve the fit to the model or further support our view that the model needs to be more sophisticated, but we do not believe that more data will necessarily require more parameters. In non-parametric learning the number of parameters is determined by the amount of data and how much we wish to smooth the data. The parameters should be viewed as having no meaning outside of their use in describing how the data behaves. In other words, we cannot extract any physical meaning from the parameters.

So which one is preferable? This comes down to asking ourselves the questions: what do we want to learn and what do we want to do with our new knowledge. If our goal is to determine the physical laws that govern rubber elasticity and how those laws can be most elegantly represented mathematically, figure 2 is an important step. If our goal is to predict what will happen, but don’t care why, for extension values that we have not measured then figure 3 is preferable. This is the essence of the difference between machine learning and physics. Machine learning works on the basis that the data is everything and we learn everything we need to know from the data itself. Physics on the other hand is continually searching for an underlying description of the universe based on laws that we discover guided by observations.

As for your votes, the view was unanimous: Figure 2. Is this telling us that machines have learnt to think like humans, or that we have biased their learning outcomes with our own preconceived notions?

# The appeal of data

I’m working on my next blog on non-parametric learning. I hope to publish early next week. In the meantime, take a look at the three graphs depicting the same stress/extension data for a rubber below and tell me which you prefer via this link. There is no correct answer, each represents a different view of learning. I’ll describe how I generated each curve in my next blog. Don’t worry if you are not familiar with stress/extension data, you are not alone! The point of graphics is to convey information to widest possible audience in the most straightforward way possible, so your opinion is still valuable to me. These different ways of graphically describing the same data challenge us to think about what is it we are hoping to know that we didn’t know before, and, then, to ask what can we do differently now that we know it?

Graph number 1:

Graph number 2:

Graph number 3:

# Machine learning vs physics learning: a physicist’s view.

In this two-part blog, I’ll introduce the two different approaches to machine learning, both of which can be applied to either classification or regression problems, discussed in my previous blog here. It seems to me, at least at this early stage of trying to make sense of AI, the difference between parametric and non-parametric learning strikes at the heart of the difference between machine learners and theoretical physicists. Understanding this difference so we can start to think about how to bring the two worlds together is one of my aims.

In this first part, I’ll provide a personal view of parametric learning from a physics rather than machine learning perspective and touch on how this approach, that we feel so comfortable with, starts to have drawbacks.

I’ll be using one of my favourite examples to illustrate. If you want to know more about rubber band thermodynamics, there are plenty of resources that are easy to find. The rubber band heat engine is one that I’ve used in my class, and there is a lovely, pre-internet era half page paper that describes in the most concise terms I’ve ever come across, simple experiments for discovering entropy in rubber bands

In theoretical physics, we start with laws that have proven themselves to be robust. My personal favourite is the second law of thermodynamics, which tells us that entropy, or disorder, of an isolated system always increases or stays the same but cannot decrease, a seemingly abstract statement with enormous predictive consequences. Why does a rubber band resist being stretched more when you heat it up? Entropy. You might wonder why I’m using this example rather than something from a more exotic area of physics. I could talk about the weird and wonderful world of Bose -Einstein Condensates or even the Higgs Boson, but I still marvel that entropy can be found in the behaviour of something so commonplace.

Anyway, back to physics. On top of laws we usually have to add some assumptions. This might be something like: inside our rubber band, the long chain molecules can be described as a series of connected rods where the joints between each rod can rotate freely. This apparently rather crude description reducing some complicated molecular structure to something so simple turns out to be surprisingly insightful, in part because it simplifies the math significantly. Our assumptions lead to parameters, in this case the number, or density, of crosslinks which are the points inside our rubber where the long chain molecules are chemically tied to other long chain molecules. Our prediction about how the rubber responds when we stretch it, known as the stress/strain relation, depends on the crosslink density. If we measure the stress/strain relation and fit the prediction, it becomes a fitting parameter. This is parametric learning and it is pretty much how physicists like to construct and test their theories.

For data such as stress vs strain we really only need a computer to automate the process of fitting a parametric line of best fit. As I mentioned earlier, this is a primitive form of machine learning. Although this insight gave me the confidence to explore the subject further, it would be doing all the brilliant minds working on the subject a great dis-service to imply that AI is simply ever increasingly complex versions of y = mx + c. But before I start talking about what it is that these brilliant minds are doing, let me finish with a few more words on parametric models.

Parametric models are well-defined but suffer from the fact that in choosing a model, we impose an expected structure on our data, so all the algorithm can do is determine which set of parameters provides the best fit for that particular model. Take for example the simplest theory for how a rubber responds in an experiment when we apply a force and measure how much the rubber is stretched. It predicts that the stress, which we usually give the symbol σ and is found from the force by dividing it by the cross sectional area, depends on the extension, λ:

Here k is Boltzmann’s constant, which always crops up in models built on the second law of thermodynamics, T is the temperature of our measurement and ρ is the crosslink density. We can use linear regression by plotting σ on the y-axis and

on the x-axis, in which case our slope is kTρ, so we can determine the crosslink density in this way. We can compare this with what we expect based on knowledge of how we made the rubber. For many rubbers this relation turns out to be too simplistic. It works well at low extensions, overestimates the stress at intermediate and overestimates it at higher extensions, but the algorithm will find a line of best fit even if it isn’t very good. We will probably guide the fit so that it gives more weight to the region of data where we expect it to work best. This is known as using prior information, an important concept in Bayesian machine learning, to improve the quality of our learning. What the algorithm cannot tell us is whether the equation we are using just needs refining or we need to start with a completely different model. It can tell us how good the fit is, if we impose arbitrary choices about what constitutes a good fit. Where the mismatches between theory and data occur often provides clues about how to build a better description and we go back to the theoretical drawing board to figure out where we went wrong.

If our model is a very poor fit we are unable to use our data fitting to make useful new predictions, but there are plenty of situations where we need to make predictions and we cannot wait for someone to find the right model. The theory for rubber elasticity was first proposed in the 1940s by Hubert James and Eugene Guth. Over 70 years later, theoreticians are still attempting to refine their model! In the second part, I’ll explore non-parametric machine learning, a different way of describing and predicting data.