{"id":1243,"date":"2020-01-14T23:50:50","date_gmt":"2020-01-14T23:50:50","guid":{"rendered":"https:\/\/www.danielparente.net\/en\/2020\/01\/14\/smart-classroom-assistant-machine-learning-tutorial-the-magpi-magazine\/"},"modified":"2020-01-14T23:50:50","modified_gmt":"2020-01-14T23:50:50","slug":"smart-classroom-assistant-machine-learning-tutorial-the-magpi-magazine","status":"publish","type":"post","link":"https:\/\/www.danielparente.net\/en\/2020\/01\/14\/smart-classroom-assistant-machine-learning-tutorial-the-magpi-magazine\/","title":{"rendered":"Smart classroom assistant machine learning tutorial \u2014 The MagPi magazine"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div>\n<p>First, you\u2019ll create an assistant that uses a list of rules for understanding commands, and you\u2019ll learn why that approach isn\u2019t very good. Next, you will teach the assistant to recognise commands for different devices by training it using examples of each command.<\/p>\n<p><a href=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/1QupFsePhmC3zTxIrCNvA1\/01eefce9cf636d393b12d81883927249\/Smart-Classroom.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/1QupFsePhmC3zTxIrCNvA1\/01eefce9cf636d393b12d81883927249\/Smart-Classroom.png?w=800\" alt=\"Smart-Classroom: Use machine learning and Scratch to turn on a lamp and control a fan\"\/><\/a><\/p>\n<h2>1. Get started<\/h2>\n<p>Head to <a href=\"https:\/\/machinelearningforkids.co.uk\" target=\"_blank\" rel=\"noopener\">machinelearningforkids.co.uk<\/a> in a web browser. You\u2019ll then need to click on \u2018Get Started\u2019, and then click on \u2018Try it now\u2019.<\/p>\n<p><a href=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/5c7k6i6iT263Xump19HIor\/ea77bcf3d17e9600eb9b832b3bf055bf\/machine_learning_website.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/5c7k6i6iT263Xump19HIor\/ea77bcf3d17e9600eb9b832b3bf055bf\/machine_learning_website.png?w=800\" alt=\"The Machine Learning for Kids website helps you get started with AI\"\/><\/a><\/p>\n<h2>2. Create a project<\/h2>\n<p>Click on Projects in the menu bar at the top, and then click on the \u2018+ Add a new project\u2019 button. Name your project \u2018smart classroom\u2019 and set it to learn to recognise text, then click on Create. You should now see \u2018smart classroom\u2019 in the projects list; click on this project.<\/p>\n<p><a href=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/5ToC4wPQ1NwvVJyboJujuy\/a885294a7bf64d5dd55b62070117d469\/smartclassroom3.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/5ToC4wPQ1NwvVJyboJujuy\/a885294a7bf64d5dd55b62070117d469\/smartclassroom3.png?w=800\" alt=\"Select Project templates and name your new project 'smart classroom'\"\/><\/a><\/p>\n<h2>3. Prepare the project<\/h2>\n<p>Now we need to get a project ready in Scratch. Click on Make, click on Scratch 3, then click on \u2018Scratch by itself\u2019. The page then warns you that you haven\u2019t done any machine learning yet. Ignore this and click on \u2018Scratch by itself\u2019 to launch Scratch. Finally, click on \u2018Project templates\u2019 and then click on the \u2018Smart Classroom\u2019 template.<\/p>\n<h2>4. Add a list of rules<\/h2>\n<p><a href=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/19LPrbrOFukhYliJlkmAWp\/3d90c2458960c9395ef00da33341afb5\/Figure_1_smartclassroom5.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/19LPrbrOFukhYliJlkmAWp\/3d90c2458960c9395ef00da33341afb5\/Figure_1_smartclassroom5.png?w=800\" alt=\"Figure 1: click on 'classroom' in the Scratch sprites pane\"\/><\/a><\/p>\n<p>In this step, you will edit the project to include a list of rules to activate and deactivate the fan and the lamp. Click the classroom sprite to select it, as shown in <b>Figure 1<\/b>. Click on the Code tab and create the script shown in <b>Figure 2<\/b>. Once you\u2019ve done that, click on File and then on \u2018Save to your computer\u2019, and save the program to a file.<\/p>\n<p><a href=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/1n92KK64qSZtYwtHrCtmZF\/4780c0e2d5fbfe02a1bb748039c4482f\/Figure_2_smartclassroom6.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/1n92KK64qSZtYwtHrCtmZF\/4780c0e2d5fbfe02a1bb748039c4482f\/Figure_2_smartclassroom6.png?w=800\" alt=\"Figure 2: The Scratch code for a rules-based approach\"\/><\/a><\/p>\n<h2>5. First tests<\/h2>\n<p>Click on the green flag to test your program, and then type in a command and watch the program react! The following commands should all work:<\/p>\n<p>Turn on the lamp<br \/>\nTurn off the lamp<br \/>\nTurn on the fan<br \/>\nTurn off the fan<\/p>\n<p>Type in anything else and your program does nothing! Even if you make a small spelling mistake, the program does not react.<\/p>\n<h2>6. Beyond rules<\/h2>\n<p>You\u2019re telling your virtual classroom assistant to react to commands using a simple rules-based approach. But if you wanted your program to understand commands that are phrased differently, you would need to add extra \u2018if\u2019 blocks.<\/p>\n<p>The problem with this rules-based approach is that you need to exactly predict all the commands the smart classroom assistant will understand. Listing every possible command would take a very, very long time. Next, you will try a better approach: teaching the computer to recognise commands by itself.<\/p>\n<h2>7. Examples for training<\/h2>\n<p>Close the Scratch window and go back to the Training tool, then click on the \u2018&lt; Back to project\u2019 link. Click on the Train button because you need to collect some examples so that you can train the computer. To collect different examples, you need to create \u2018buckets\u2019 to put the examples in.<\/p>\n<p><a href=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/7KSq39zoQOsmAMkK4gXHoV\/9ec37cd42be9b6e101e64a7af240a566\/smartclassroom8.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/7KSq39zoQOsmAMkK4gXHoV\/9ec37cd42be9b6e101e64a7af240a566\/smartclassroom8.png?w=800\" alt=\"Train your computer to recognise commands by adding text examples to the project. Make sure you assign each command to the correct 'bucket' so it results in the correct action\"\/><\/a><\/p>\n<p>To create a bucket, click on \u2018+ Add new label\u2019 and call the bucket \u2018fan on\u2019. Click on \u2018+ Add new label\u2019 again and create a second bucket called \u2018fan off\u2019. Create a third and a fourth bucket called \u2018lamp on\u2019 and \u2018lamp off\u2019.<\/p>\n<p>Click on the \u2018Add example\u2019 button in the \u2018fan on\u2019 bucket, and type in a command asking for the fan to be turned on. For example, you could type \u2018Please can you switch on the fan\u2019. For the \u2018fan off\u2019 bucket, you\u2019ll need to click \u2018Add example\u2019 again and then use something like \u2018I want the fan off now\u2019. Do the same for the \u2018lamp on\u2019 and \u2018lamp off\u2019 buckets.<\/p>\n<p><a href=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/790GAYO2RUbZpiQijycROd\/bff1dc2db95462e470c0bda8f5f4e1f3\/smartclassroom10.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/790GAYO2RUbZpiQijycROd\/bff1dc2db95462e470c0bda8f5f4e1f3\/smartclassroom10.png?w=800\" alt=\"Add as many and as varied example phrases as you can for each command\"\/><\/a><\/p>\n<h2>8. More examples for more training<\/h2>\n<p>Continue to add examples until you have at least six examples in each bucket. Be imaginative! Try to think of lots of different ways to ask each command. <\/p>\n<p>For example:<\/p>\n<p>For \u2018fan on\u2019, you could complain that you\u2019re too\u00a0hot.<br \/>\nFor \u2018fan off\u2019, you could complain that it\u2019s too\u00a0breezy.<br \/>\nFor \u2018lamp on\u2019, you could complain that you can\u2019t\u00a0see.<br \/>\nFor \u2018lamp off\u2019, you could complain that it\u2019s too\u00a0bright.<\/p>\n<p>More is good: the more examples you give your program, the better the program should get at recognising your commands.<\/p>\n<p>Use equal numbers: add roughly the same number of examples for each command. If you have a lot of examples for one command and not the others, this can affect the way that the program learns to recognise commands.<\/p>\n<p>Make the examples really different from each other: try to come up with lots of different types of examples. For instance, make sure that you include some long examples and some very short ones.<\/p>\n<h2>9. Start the training<\/h2>\n<p>You will now train the program using the examples, and then test it. The program will learn from patterns in the examples you give it, such as the choice of words and the way sentences are structured. Then, based on the patterns the program finds, it can interpret new commands.<\/p>\n<p>Click on the \u2018&lt; Back to project\u2019 link, then click on \u2018Learn &amp; Test\u2019. Click on the \u2018Train new machine learning model\u2019 button. If you have enough examples, the program should start to learn how to recognise commands from these examples.<\/p>\n<h2>10. Test the training<\/h2>\n<p>Wait for the training to complete. This might take a minute or two but once the training has completed, a test box appears. Test your machine learning model to see what it has learned by typing in one of the commands you added to a bucket, and then press ENTER. The command should be recognised.<\/p>\n<p>Now type in commands that are not in the buckets. If you\u2019re not happy with how the computer recognises the commands, go back to the\u00a0previous step and add some more examples. Then select the \u2018Train new machine learning model\u2019\u00a0button again.<\/p>\n<p>Instead of writing rules for the program, you are giving the program examples. The program uses the examples to train a machine learning model. Because you are supervising the program\u2019s training by giving examples, this machine learning approach is called supervised learning.<\/p>\n<h2>11. Use it in Scratch<\/h2>\n<p>Now update your Scratch program to include\u00a0your machine learning model instead of the rules-based approach. Click on the \u2018&lt; Back to project\u2019 link, click on Make, then Scratch 3. Here\u00a0you can read the instructions on the page to learn how to use machine learning blocks in\u00a0Scratch.<\/p>\n<p>Click on Open in Scratch 3, then on File and \u2018Load from your computer\u2019, and select the Scratch\u00a0project you saved earlier. When Scratch asks you whether to replace the current project, click on OK.<\/p>\n<p>Click on the Code tab, and update your Scratch code (<b>Figure 3<\/b>) to use your machine learning model instead of the rules you first added. The \u2018recognise text\u2019 block is a new block added by your project. This new block can receive a message and return one of the four labels, based on the machine learning model you have trained.<\/p>\n<p><a href=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/7fgmfqDeIxFjgTixOnasJA\/e5e326677a571650d60994ff78fd367b\/Figure_3_smartclassroom14.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/7fgmfqDeIxFjgTixOnasJA\/e5e326677a571650d60994ff78fd367b\/Figure_3_smartclassroom14.png?w=800\" alt=\"Figure 3: Revised for a machine learning approach, the code features \u2018recognise text\u2019 blocks\"\/><\/a><\/p>\n<h2>12. Scratch AI<\/h2>\n<p>Click the green flag to test your new code. Test your project by typing a command and pressing ENTER on your keyboard. The fan or lamp should react to your command.<\/p>\n<p>Make sure you test that this works even for commands that you didn\u2019t include as examples in the buckets.<\/p>\n<p>Save your project as before. Your Scratch smart virtual classroom now uses a machine learning model instead of a rules-based approach. Using machine learning is better than using rules, because training a program to recognise commands for itself is much quicker than trying to make a list of every possible command.<\/p>\n<h2>Top tip: machine learning<\/h2>\n<p>You need to tell an AI what to learn. The more you give it to learn with, the better it will be. The more examples you use to train the machine learning model, the better your program should get at recognising commands.<\/p>\n<p>To learn about how to can improve the\u00a0model with \u2018confidence scores\u2019, head to <a href=\"https:\/\/machinelearningforkids.co.uk\" target=\"_blank\" rel=\"noopener\">magpi.cc\/smartclassroom<\/a>.<\/p>\n<h2>Top tip: Go further<\/h2>\n<p>Can you get the model to tell you the weather or date? Give it a go!<\/p>\n<h2>Top tip: Bring more projects to life<\/h2>\n<p>Want to discover more great &#8216;makes&#8217;? You can find this project and others on the <a href=\"https:\/\/projects.raspberrypi.org\/en\/\" target=\"_blank\" rel=\"noopener\">Raspberry Pi projects website<\/a>. <\/p>\n<p><a href=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/7IMmK4LyR6bEQF9vOoh3ur\/c7a31ea8c6198f20dfb4e523111153e7\/projects_landing_page.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/images.ctfassets.net\/tvfg2m04ppj4\/7IMmK4LyR6bEQF9vOoh3ur\/c7a31ea8c6198f20dfb4e523111153e7\/projects_landing_page.png?w=800\" alt=\"Head to Raspberry Pi's dedicated Projects website for more great 'makes'\"\/><\/a><\/p><\/div>\n<p>[ad_2]<br \/>\n<br \/><a href=\"https:\/\/magpi.raspberrypi.org\/articles\/smart-classroom-assistant-machine-learning-tutorial\" target=\"_blank\" rel=\"noopener\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] First, you\u2019ll create an assistant that uses a list of rules for understanding commands, and you\u2019ll learn why that approach isn\u2019t very good. Next, you will teach the assistant to recognise commands for different devices by training it using examples of each command. 1. Get started Head to machinelearningforkids.co.uk in a web browser. You\u2019ll [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1244,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[94,92,98],"tags":[],"class_list":["post-1243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence","category-data-science","category-machine-learning"],"blocksy_meta":[],"jetpack_featured_media_url":"https:\/\/e928cfdc7rs.exactdn.com\/info\/uploads\/sites\/3\/2020\/01\/Smart-classroom-assistant-machine-learning-tutorial-\u2014-The-MagPi-magazine.png?strip=all","jetpack_shortlink":"https:\/\/wp.me\/p2TFCd-k3","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts\/1243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/comments?post=1243"}],"version-history":[{"count":0,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts\/1243\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/media\/1244"}],"wp:attachment":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/media?parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/categories?post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/tags?post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}