<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: karthik-ke</title>
    <description>The latest articles on DEV Community by karthik-ke (@karthikke).</description>
    <link>https://dev.to/karthikke</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F542021%2F7bfbd506-ce6d-4872-b206-aeb16f02befe.JPG</url>
      <title>DEV Community: karthik-ke</title>
      <link>https://dev.to/karthikke</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karthikke"/>
    <language>en</language>
    <item>
      <title>Django + Celery + CPU/GPU + Tensorflow, Keras + Docker</title>
      <dc:creator>karthik-ke</dc:creator>
      <pubDate>Thu, 05 Aug 2021 07:33:38 +0000</pubDate>
      <link>https://dev.to/karthikke/django-celery-cpu-gpu-tensorflow-keras-docker-4mk6</link>
      <guid>https://dev.to/karthikke/django-celery-cpu-gpu-tensorflow-keras-docker-4mk6</guid>
      <description>&lt;p&gt;We held up a situation where we build tensorflow model using a GPU machine and calling the model in the same machine working fine as expected. &lt;/p&gt;

&lt;p&gt;We wanted to consume this model in our application which runs under Django framework with the help of celery tasks, while calling the model in the application flow inside celery worker it didn't work or got hang up. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We tried following possible solution but nothing worked.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Increased the CPU machine number of cores and RAM size. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Created a AWS instance with GPU enabled and tried to consume the model. We haven't added configuration to use the GPU since tensorflow automatically detects GPU and use it for model prediction. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since the Django application and celery running in docker container, we thought docker couldn't able to use the GPU automatically, so we changed our base image to tensorflow CPU/GPU as base docker image and tried to consume the model but failed :(.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We created a new API to consume the model but failed to get the output again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We noticed from the beginning the model call is working fine without application flow but via application it's not working. so we tried to call the model through a python script by using multiprocessing and found out that with multiprocessing(since celery does that) it's not working. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After we found that with multiprocessing tensorflow model predict was not working, we came through the following post which given a clear picture what's happening inside. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/45459205/keras-predict-not-returning-inside-celery-task"&gt;why tensorflow model predict not running under multiprocessing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We added the flow in our requirements.txt &lt;/p&gt;

&lt;p&gt;&lt;code&gt;eventlet&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And in celery command while building the docker added --pool as below,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;celery -A Proj worker -l info -Q Q1,Q2 --pool=eventlet -c 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By this change we were able to call the model and get the prediction in the application flow &lt;em&gt;without GPU enabled machine!!!&lt;/em&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To get the prediction alone from a tensorflow model we don't need a GPU machine. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We spent a quite few days by doing many changes apart from what I mentioned above. Thought to share this so someone's day will be saved..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Happy Coding!!! 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>django</category>
      <category>python</category>
      <category>machinelearning</category>
      <category>analytics</category>
    </item>
    <item>
      <title>Go Language to go for beginners </title>
      <dc:creator>karthik-ke</dc:creator>
      <pubDate>Thu, 17 Dec 2020 10:02:07 +0000</pubDate>
      <link>https://dev.to/karthikke/go-language-to-go-for-beginners-17gd</link>
      <guid>https://dev.to/karthikke/go-language-to-go-for-beginners-17gd</guid>
      <description>&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: &lt;/p&gt;

&lt;p&gt;Go language is designed &amp;amp; developed at Google (Go). It is a compiled language that is like C and CPP but with memory safety, structural typing, and CSP (Communicating Sequential Processes). &lt;br&gt;
This documentation will not guide you to each element in Go but will give you a good idea to start with Go. Go’s key feature is concurrency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Some software developed using Go&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Docker&lt;/li&gt;
&lt;li&gt;Kubernetes&lt;/li&gt;
&lt;li&gt;Heroku &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Go - Hello world:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We had traditional to practice any code, we start by writing a hello world sample code. We go by the same, &lt;/p&gt;

&lt;p&gt;For installation and get started: &lt;a href="https://golang.org"&gt;Go&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Sample code:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ol7wb9K7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go%2520sample%2520code%2520from%2520go%2520site.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ol7wb9K7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go%2520sample%2520code%2520from%2520go%2520site.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As I referred earlier Go influenced by C, Go code start executed from the main function. &lt;/p&gt;

&lt;p&gt;Unlike C programming language if you want to use any in-built function you need to specify the package name before the function, here the Println function comes from &lt;code&gt;fmt&lt;/code&gt; (format) package. The function definition is identified in Go with the keyword func.&lt;/p&gt;

&lt;p&gt;All the functions and codes in Go should be under a package so that we can use the current package in another package (same as fmt in the above package-main). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Function open curly bracket should start with the same line of the function definition&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Variables:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Variable creation and assigning value in Go in different ways as follows,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;1. Declaring a variable:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
   &lt;code&gt;var a int&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variable &lt;code&gt;a&lt;/code&gt; created here without assigning value to it, the default value of a is 0 (int).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;2. Declaring a variable and assigning value:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
   &lt;code&gt;var a= 5&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; is the keyword which is declaring variable &lt;code&gt;a&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;5&lt;/code&gt; is the value for variable &lt;code&gt;a&lt;/code&gt;
&lt;code&gt;var a int = 5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In the above example same, we are assigning value 5 to the variable &lt;code&gt;a&lt;/code&gt; but mentioning its type &lt;code&gt;int&lt;/code&gt;. Here specifying type is optional, but when it comes to declaration alone it's mandatory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;3. Declaring a variable and assigning value:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
   &lt;code&gt;a := 5&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here creation and assigning a value for &lt;code&gt;a&lt;/code&gt; is happening by using the operator &lt;code&gt;:=&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---xII8U4J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go_variable_assign.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---xII8U4J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go_variable_assign.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Variables available in Go&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Bool &lt;/li&gt;
&lt;li&gt;  String &lt;/li&gt;
&lt;li&gt;  int, int8, int16, int32, int64 &lt;/li&gt;
&lt;li&gt;  uint, uint8, uint16, uint32, uint64&lt;/li&gt;
&lt;li&gt;  float32, float64 &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Creating a variable or import any package and not using anywhere in your Go throws error unlike other languages (Python, Java, C, CPP).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Loops:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go have only &lt;code&gt;for&lt;/code&gt; loop unlike other languages Go does not have &lt;code&gt;do while&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt; loops in it. When it comes to loops, we need to specify three things variable value assignment, condition, incrementation. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;1. Infinite loop:&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;In this type, we do not specify any of the three-parameter we mentioned above, so it becomes an infinite loop, useless though I want to mention this weird functionality availability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2reRaHRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/infinite_loop.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2reRaHRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/infinite_loop.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;2.Variable assignment, condition, increment/decrement in different areas:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go is not like C language always specify assignment, condition, increment in one place, Go allows these three in different places as well. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sJiFasL3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/for_1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sJiFasL3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/for_1.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;3.Traditional pattern:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Assignment, condition, and increment/ decrement at loop initialization.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--63sG0Cc_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/for_2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--63sG0Cc_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/for_2.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go language function declaration is done by using the key word &lt;code&gt;func&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;1.   Function with no return value:&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
We can able to create a function without any return. Just to mention function without return type is possible in Go.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R0eUofGZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go_func_no_return.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R0eUofGZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go_func_no_return.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;2.   Function with multiple parameters &amp;amp; one return:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the below snippet add function takes two-parameter and adding and returns a single value. Unlike other languages like python, we need to specify the type of return here in Go.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yEqunGGF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go_fun_1_return.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yEqunGGF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go_fun_1_return.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we discussed earlier variable declaration and assignment happening at &lt;code&gt;res: = add (2,3)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;3.   Function with multiple parameters &amp;amp; multiple return:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some languages do not support multiple return values but Go gives that feature. Refer to the below snippet. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RuT7VsSC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go_fun_2_return.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RuT7VsSC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/go_fun_2_return.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above add function has two input values a, b as integer and &lt;code&gt;sum_val&lt;/code&gt;, &lt;code&gt;sub_val&lt;/code&gt; as output, and their return also mentioned. In the function we can just give a return it will work in Go. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to use your own function in a package outside of the other package you need to create that function with a capital letter if you are creating a function with the small case you can’t use that outside of the package. &lt;br&gt;
func &lt;code&gt;inside_package ()&lt;/code&gt; --&amp;gt; only accessible inside a package &lt;br&gt;
func &lt;code&gt;Outside_package ()&lt;/code&gt; --&amp;gt; accessible by other packages as well. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Conditional statements in Go:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go has two conditional statements if-else and switch. It is typically the same as C will be seeing the example so that we can have a better understanding. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;if else:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One thing to be noted herein Go we start else if or else block and end of the if block itself. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VNdcslf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/if_else.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VNdcslf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/if_else.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Switch:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Same as C switch comes with default when condition not falling in any of the cases. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--krfCJESu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/switch.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--krfCJESu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/karthik-ke/common_images/main/switch.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;That all I feel basics to start with Go… Thanks for your time... Thoughts!? Please comment It&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;:) :) :)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>go</category>
    </item>
  </channel>
</rss>
