Hello World! for Machine Learning

栏目: IT技术 · 发布时间: 3年前

内容简介:The most common question that keeps I keep getting asked is “I hope you know a little python which we will be using to code our model. If not here is a great place for you to start:Machine Learning provides systems with the ability to automatically learn a

Get started with Machine Learning, by building your first model from scratch!

Hello World! for Machine Learning

Photo by VisionPic .net from Pexels

The most common question that keeps I keep getting asked is “ How do I get started with Machine Learning? ”. I know it can be quite overwhelming- with all the different tools and resources available online and you have no idea where to start. Believe me, I have been there. So in this article, I will try to get you started with building Machine Learning models and familiarise you with the practices that are being used in the industry.

I hope you know a little python which we will be using to code our model. If not here is a great place for you to start: https://www.w3schools.com/python/

What is Machine Learning?

Machine Learning provides systems with the ability to automatically learn and improve from without being explicitly programmed.

“Geez! that seems complicated…”

To put it simply, Machine Learning is just pattern recognition. That’s it!

Consider a baby playing with a toy. He has to put the blocks in the correct slots or it won’t fit. He tries to stick the square block to the circular hole. It doesn’t work! He tries it a few more times and eventually fits the square block in the square hole. Now, every time he sees a square block, he would know to fit it into the square hole!

Hello World! for Machine Learning

Photo by Tatiana Syrikova from Pexels

This is a very simple idea of how Machine Learning works. Machine Learning is (obviously) more complicated but let us start simple.

In Machine Learning, instead of trying to define the rules and express them in a programming language, you provide the answers (typically called labels) along with the data, and the machine will infer the rules that determine the relationship between the answers and the data.

Hello World! for Machine Learning

The data and labels are used to create Machine Learning Algorithms (The “Rules”) which are typically called Models .

Using this model, when the Machine gets new data, it can predict or correctly label them.

Hello World! for Machine Learning

For example, if we train the model with labelled images of Cats and Dogs, the model would be able to predict when a new image is shown whether it is a Cat or a Dog.

Hello World! for Machine Learning

How training a model works!

Now that we have a basic understanding, let us get coding!

Creating your first Machine Learning Model

Consider the following set of numbers. Are you able to figure out the relationship between them?

As you look at them you might notice that the X value is increasing by 1 as you read left to right, and the corresponding Y value is increasing by 2. So you probably think Y=2X plus or minus something. Then you’d probably look at the zero on X and see that Y = 1, and you’d come up with the relationship Y=2X+1 .

Now if we are given a value of 6 for X, we can accurately predict the value of Y to be 2*6 + 1 = 13.

It must have been pretty easy to figure this out for you. Now let’s try to get a computer to figure this out. Put on your coding hats because we are about to code our first Machine Learning model!

Setting up the environment

We will use Google Colab for writing our code.

So what is Google Colab?

It’s an incredible online browser-based platform that allows us to train our models on machines for free! Sounds too good to be true, but thanks to Google, we can now work with large datasets, build complex models, and even share our work seamlessly with others.

So basically it would be where we would train and use our model. You would need a Google Account for using Colab. Once that is done create a new notebook. Voila! You have your first notebook.

Hello World! for Machine Learning

Hello World! for Machine Learning

Check out this tutorial for learning how to use Google Colab if you have not used it before.

Now we write code for real!

Let’s get coding

The complete notebook is available here and here(GitHub) .

Imports

We are importing TensorFlow and calling it tf for ease of use.

Next, we import a library called numpy, which helps us to represent our data as lists easily and quickly.

The framework for defining a model as a set of sequential layers is called keras, so we import that too.

Create the data set

As shown earlier, we have 7 Xs and 7 Ys and we found the relationship between them to be Y = 2X + 1 .

A python library called numpy provides lots of array type data structures that are a defacto standard way of doing it. We declare that we want to use these by specifying the values as an array in numpy using np.array[]

Define the model

Next, we will create the simplest possible neural network. It has 1 layer, and that layer has 1 neuron, and the input shape to it is just 1 value.

You know that in the function, the relationship between the numbers is y=2x+1 .

When the computer is trying to ‘learn’ that, it makes a guess…maybe y=10x+10 . The loss function measures the guessed answers against the known correct answers and measures how well or how badly it did.

Next, the model uses the optimiser function to make another guess. Based on the loss function’s result, it will try to minimise the loss. At this point maybe it will come up with something like y=5x+5 . While this is still pretty bad, it’s closer to the correct result (i.e. the loss is lower).

The model will repeat this for the number of epochs which you will see shortly.

But first, here’s how we tell it to use mean squared error for the loss and stochastic gradient descent (sgd) for the optimiser. You don’t need to understand the maths for these yet, but you can see that they work! :)

Over time you will learn the different and appropriate loss and optimiser functions for different scenarios.

Training the model

Training is the process where the model learns as we said about before. model.fit function is used for fitting the training data that we have created to the model.

When you run this code, you’ll see the loss will be printed out for each epoch.

Hello World! for Machine Learning

Hello World! for Machine Learning

You can see that for the first few epochs, the loss value is quite large and with each step it is getting smaller.

Using the trained model to make predictions

Finally! Our model has been trained and is ready to face the real world. Let’s see how well our model can predict the value of Y given X.

We use the model.predict method to have it figure out Y for any value of X.

So, if we take the value of X as 8, then we know the value of Y is 2*8 + 1 = 17 . Let’s see if our model can get it right.

[[17.00325]]

That ended up a little over than the value that we would expect.

Machine Learning deals with probabilities, so given the data that we fed the model with, it calculated that there is a very high probability that the relationship between X and Y is Y=2X+1 , but with only 7 data points we can’t know for sure. As a result, the result for 8 is very close to 17, but not necessarily 17.

That is it! You have covered the core concepts of Machine Learning that you could use in different scenarios.

Hello World! for Machine Learning

Photo by pettergra from Meme Economy

The process/steps we have used here is what you would do when you build complex models.

If you have built your first model, how about stepping up a level and use your new skills to build a Computer Vision model?

Happy coding!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机程序设计艺术:第4卷 第4册(双语版)

计算机程序设计艺术:第4卷 第4册(双语版)

Donald E.Knuth / 苏运霖 / 机械工业出版社 / 2007-4 / 42.00元

关于算法分析的这多卷论著已经长期被公认为经典计算机科学的定义性描述。迄今已出版的完整的三卷组成了程序设计理论和实践的惟一的珍贵源泉,无数读者都赞扬Knuth的著作对个人的深远影响。科学家们为他的分析的美丽和优雅所惊叹,而从事实践的程序员们已经成功地应用他的“菜谱式”的解到日常问题上,所有人都由于Knuth在书中所表现出的博学、清晰、精确和高度幽默而对他无比敬仰。   为开始后续各卷的写作并更......一起来看看 《计算机程序设计艺术:第4卷 第4册(双语版)》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试