{"id":1258,"date":"2017-12-06T21:18:07","date_gmt":"2017-12-06T21:18:07","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=1258"},"modified":"2024-12-16T22:32:44","modified_gmt":"2024-12-16T19:32:44","slug":"simple-example-tensorflow","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/hi\/2017\/12\/06\/simple-example-tensorflow\/","title":{"rendered":"Simple TensorFlow Example"},"content":{"rendered":"<p>I present to your attention the simplest example of working with the framework for working with Deep Learning &#8211; TensorFlow. In this example, we will teach the neural network to determine positive, negative numbers and zero. I entrust the installation of <a href=\"https:\/\/www.tensorflow.org\/\" target=\"_blank\" rel=\"noopener\">TensorFlow<\/a> and <a href=\"https:\/\/developer.nvidia.com\/cuda-downloads\" target=\"_blank\" rel=\"noopener\">CUDA<\/a> to you, this task is really not easy)<\/p>\n<p>To solve classification problems, <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%97%D0%B0%D0%B4%D0%B0%D1%87%D0%B0_%D0%BA%D0%BB%D0%B0%D1%81%D1%81%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D0%B8\" target=\"_blank\" rel=\"noopener\">classifiers<\/a> are used. <a href=\"https:\/\/www.tensorflow.org\/\" target=\"_blank\" rel=\"noopener\">TensorFlow<\/a> has several ready-made high-level classifiers that require minimal configuration to work. First, we train the <a href=\"https:\/\/www.tensorflow.org\/versions\/master\/api_docs\/python\/tf\/estimator\/DNNClassifier\" target=\"_blank\" rel=\"noopener\">DNNClassifier<\/a> using a dataset with positive, negative, and zero numbers &#8211; with the correct &#8220;labels&#8221;. At a human level, the dataset is a set of numbers with the classification result (labels):<\/p>\n<p><strong><em>10 &#8211; positive<\/em><\/strong><br \/><strong><em>-22 &#8211; negative<\/em><\/strong><br \/><strong><em>0 &#8211; zero<\/em><\/strong><br \/><strong><em>42 &#8211; positive<br \/>&#8230; other numbers with classification<br \/><\/em><\/strong><br \/>Next, training starts, after which you can feed in numbers that weren&#8217;t even included in the dataset &#8211; the neural network must correctly identify them.<br \/>Below is the complete code for the classifier with the training dataset generator and input data:<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5 ; font-weight: bold;\">tensorflow<\/span><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">itertools<\/span><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">random<\/span><span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">time<\/span> <span style= \"color: #008800; font-weight: bold;\">import<\/span> time<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ClassifiedNumber<\/span>:__number <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>__classifiedAs <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">__init__<\/span>(<span style =\"color: #007020;\">self<\/span>, number):<span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>__number <span style=\"color: #333333;\">=<\/ span>number<span style=\"color: #008800; font-weight: bold;\">if<\/span> number <span style=\"color: #333333;\">==<\/span> <span style=\"color: # 0000dd; font-weight: bold;\">0<\/span>:<span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>__classifiedAs <span style=\"color: #333333;\">=<\/ span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> <span style=\"color: #888888;\"># zero<\/span><span style=\"color: #008800; font-weight: bold;\">elif<\/span> number <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: # 0000dd; font-weight: bold;\">0<\/span>:<span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>__classifiedAs <span style=\"color: #333333;\">=<\/ span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #888888;\"># positive<\/span><span style=\"color: #008800; font-weight: bold;\">elif<\/span> number <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: # 0000dd; font-weight: bold;\">0<\/span>:<span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>__classifiedAs <span style=\"color: #333333;\">=<\/ span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span> <span style=\"color: #888888;\"># negative<\/span><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">number<\/span>(<span style =\"color: #007020;\">self<\/span>):<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333; \">.<\/span>__number<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">classifiedAs<\/span>(<span style =\"color: #007020;\">self<\/span>):<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333; \">.<\/span>__classifiedAs<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">classifiedAsString<\/span>(classifiedAs):<span style=\"color: #008800; font-weight: bold;\">if<\/span> classifiedAs <span style=\"color: #333333;\">==<\/span> <span style=\"color: # 0000dd; font-weight: bold;\">0<\/span>:<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"Zero\"<\/span><span style=\"color: #008800; font-weight: bold;\">elif<\/span> classifiedAs <span style=\"color: #333333;\">==<\/span> <span style=\"color: # 0000dd; font-weight: bold;\">1<\/span>:<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"Positive\"<\/span><span style=\"color: #008800; font-weight: bold;\">elif<\/span> classifiedAs <span style=\"color: #333333;\">==<\/span> <span style=\"color: # 0000dd; font-weight: bold;\">2<\/span>:<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"Negative\"<\/span><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">trainDatasetFunction<\/span>():trainNumbers <span style=\"color: #333333;\">=<\/span> []trainNumberLabels <span style=\"color: #333333;\">=<\/span> []<span style=\"color: #008800; font-weight: bold;\">for<\/span> i <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style =\"color: #007020;\">range<\/span>(<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1000<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1001<\/span>):number <span style=\"color: #333333;\">=<\/span> ClassifiedNumber(i)trainNumbers<span style=\"color: #333333;\">.<\/span>append(number<span style=\"color: #333333;\">.<\/span>number())trainNumberLabels<span style=\"color: #333333;\">.<\/span>append(number<span style=\"color: #333333;\">.<\/span>classifiedAs())<span style=\"color: #008800; font-weight: bold;\">return<\/span> ( {<span style=\"background-color: #fff0f0;\">\"number\"<\/span> : trainNumbers } , trainNumberLabels)<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">inputDatasetFunction<\/span>():<span style=\"color: #008800; font-weight: bold;\">global<\/span> randomSeedrandom<span style=\"color: #333333;\">.<\/span>seed(randomSeed) <span style=\"color: #888888;\"># to get same result<\/span>numbers <span style=\"color: #333333;\">=<\/span> []<span style=\"color: #008800; font-weight: bold;\">for<\/span> i <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style =\"color: #007020;\">range<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>):numbers<span style=\"color: #333333;\">.<\/span>append(random<span style=\"color: #333333;\">.<\/span>randint(<span style=\"color: #333333; \">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">9999999<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">9999999<\/span>))<span style=\"color: #008800; font-weight: bold;\">return<\/span> {<span style=\"background-color: #fff0f0;\">\"number\"<\/span> : numbers }<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>():<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"TensorFlow Positive-Negative-Zero numbers classifier test by demensdeum 2017 (demensdeum@gmail. com)\"<\/span>)maximalClassesCount <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">len<\/span>(<span style=\"color: #007020;\">set< \/span>(trainDatasetFunction()[<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>])) <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>numberFeature <span style=\"color: #333333;\">=<\/span> tensorflow<span style=\"color: #333333;\">.<\/span>feature_column<span style=\"color: #333333;\">. <\/span>numeric_column(<span style=\"background-color: #fff0f0;\">\"number\"<\/span>)classifier <span style=\"color: #333333;\">=<\/span> tensorflow<span style=\"color: #333333;\">.<\/span>estimator<span style=\"color: #333333;\">. <\/span>DNNClassifier(feature_columns <span style=\"color: #333333;\">=<\/span> [numberFeature], hidden_units <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: # 0000dd; font-weight: bold;\">20<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>], n_classes <span style=\"color: #333333;\">=<\/span> maximalClassesCount)generator <span style=\"color: #333333;\">=<\/span> classifier<span style=\"color: #333333;\">.<\/span>train(input_fn <span style=\"color: #333333;\" >=<\/span> trainDatasetFunction, steps <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1000<\/span>)<span style=\"color: #333333;\">.<\/span>predict(input_fn <span style=\"color: #333333;\">= <\/span> inputDatasetFunction)inputDataset <span style=\"color: #333333;\">=<\/span> inputDatasetFunction()results <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">list<\/span>(itertools<span style=\"color: #333333;\">. <\/span>islice(generator, <span style=\"color: #007020;\">len<\/span>(inputDatasetFunction()[<span style=\"background-color: #fff0f0;\">\"number\"<\/span>])))i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #008800; font-weight: bold;\">for<\/span> result <span style=\"color: #000000; font-weight: bold;\">in<\/span> results:<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"number: %d classified as %s\"<\/span> <span style= \"color: #333333;\">%<\/span> (inputDataset[<span style=\"background-color: #fff0f0;\">\"number\"<\/span>][i], classifiedAsString(result[<span style=\"background-color: #fff0f0;\">\"class_ids\"<\/span>][<span style=\"color: #0000dd; font-weight: bold;\">0<\/span> ])))i <span style=\"color: #333333;\">+=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>randomSeed <span style=\"color: #333333;\">=<\/span> time()main()<\/pre>\n<\/div>\n<p>It all starts in the main() method, we set the numeric column that the classifier will work with &#8211; <strong>tensorflow.feature_column.numeric_column(&#8220;number&#8221;)<\/strong> then the classifier parameters are set. It is useless to describe the current initialization arguments, since the API changes every day, and it is imperative to look at the documentation of the installed version of TensorFlow, not rely on outdated manuals.<\/p>\n<p>Next, training is started with an indication of the function that returns a dataset of numbers from -1000 to 1000 (<strong>trainDatasetFunction<\/strong>), with the correct classification of these numbers by the sign of positive, negative or zero. Next, we feed in numbers that were not in the training dataset &#8211; random numbers from -9999999 to 9999999 (<strong>inputDatasetFunction<\/strong>) for their classification.<\/p>\n<p>In the end, we run iterations on the number of input data (<strong>itertools.islice<\/strong>), print the result, run it and be surprised:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">number: 4063470 classified as Positivenumber: 6006715 classified as Positivenumber: -5367127 classified as Negativenumber: -7834276 classified as Negative<\/pre>\n<\/div>\n<blockquote class=\"imgur-embed-pub\" lang=\"en\" data-id=\"mTS5bXR\">\n<p><a href=\"\/\/imgur.com\/mTS5bXR\">iT&#8217;S ALIVE<\/a <\/p>\n<\/blockquote>\n<p><script async src=\"\/\/s.imgur.com\/min\/embed.js\" charset=\"utf-8\"><\/script><\/p>\n<p>To be honest, I&#8217;m still a little surprised that the classifier *understands* even those numbers that I didn&#8217;t teach it. I hope that in the future I&#8217;ll understand the topic of machine learning in more detail and there will be more tutorials.<\/p>\n<p>GitLab:<br \/><a href=\"https:\/\/gitlab.com\/demensdeum\/MachineLearning\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum\/MachineLearning<\/a><\/p>\n<p>Links:<br \/><a href=\"https:\/\/developers.googleblog.com\/2017\/09\/introducing-tensorflow-datasets.html\" target=\"_blank\" rel=\"noopener\">https:\/\/developers.googleblog.com\/2017\/09\/introducing-tensorflow-datasets.html<\/a><br \/>\n<a href=\"https:\/\/www.tensorflow.org\/versions\/master\/api_docs\/python\/tf\/estimator\/DNNClassifier\" target=\"_blank\" rel=\"noopener\">https:\/\/www.tensorflow.org\/versions\/master\/api_docs\/python\/tf\/estimator\/DNNClassifier<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I present to your attention the simplest example of working with the framework for working with Deep Learning &#8211; TensorFlow. In this example, we will teach the neural network to determine positive, negative numbers and zero. I entrust the installation of TensorFlow and CUDA to you, this task is really not easy) To solve classification<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/hi\/2017\/12\/06\/simple-example-tensorflow\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Simple TensorFlow Example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[87,86],"class_list":["post-1258","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-machine-learning","tag-tensorflow","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"hi","enabled_languages":["en","ru","zh","de","fr","ja","pt","hi"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false},"hi":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/1258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/comments?post=1258"}],"version-history":[{"count":26,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/1258\/revisions"}],"predecessor-version":[{"id":3988,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/1258\/revisions\/3988"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/media?parent=1258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/categories?post=1258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/tags?post=1258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}