<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Nusret Ozates</title>
<link>https://nusretozates.github.io/myposts.html</link>
<atom:link href="https://nusretozates.github.io/myposts.xml" rel="self" type="application/rss+xml"/>
<description>Welcome to Nusret Ozates&#39;s personal website. Explore my projects, blogs, and more!</description>
<generator>quarto-1.8.27</generator>
<lastBuildDate>Fri, 19 Dec 2025 21:00:00 GMT</lastBuildDate>
<item>
  <title>My summary of the “Career Advice in AI” Lecture</title>
  <dc:creator>Nusret Ozates</dc:creator>
  <link>https://nusretozates.github.io/posts/career_advice_ai_summary/</link>
  <description><![CDATA[ 






<p>I finally had time to watch the AI Career Advice lesson by Andrew Ng and Laurence Moroney. It was a great lesson for me as I’m about to graduate from my MSc and am ready to join the industry again! I’ve created a summary and wanted to share it with everyone, but I strongly recommend watching it.</p>
<p>In Andrew Ng’s introduction, he mentions two important points. The first one is that, although saying this is (according to some people) politically incorrect, working hard is important for success. But there definitely are some exceptions, like when you have an injury, you just have a kid, and examples like that. The second one is about surrounding yourself with bright minds, high-quality people, both in your personal life and your work life, as you are the average of your surroundings. Choosing with whom you work is more important than where you work. Additionally, he mentioned that AI makes engineers faster, but the ones who also listen the user feedback, communicate with other people will be the fastest ones.</p>
<p>Laurence Moroney makes crucial and thoughtful additions to these. About the hard working part, he said hard work ≠ amount of time spent. It must be something measurable, like what is your output after those hours? X new products? Y papers read and understand the papers properly? And about your surroundings, he reminded you that those people also will choose if they want to see you around. Even if you are a 10x engineer, if you are a rude person, people won’t want to see you. After these additions, he talked about the 3 pillars of success that you need to show to the employers, not just tell.</p>
<section id="understanding-depth" class="level2">
<h2 class="anchored" data-anchor-id="understanding-depth">Understanding Depth</h2>
<p>Surface-level knowledge is not enough anymore. You need to have academic knowledge, diverse skills, and the ability to separate noise from the real trends because engagement is the currency of social media, not the accuracy, so there will be a lot of noise there. By diverse skills, he doesn’t mean knowing both about NLP and CV; he means knowing about training ML models while also knowing about how to deploy them, scale them, and build an application on them to be valuable even if the AI hype completely deflates tomorrow. He also gave a practical strategy to filtering noise: Develop trusted sources and filter them actively. Learn more about the fundamentals of the hyped tech (enters the academic knowledge) before judging its impact (e.g., Hollywood is over, SWE is over). And always aware of the trends and know why it is a trend right now. As an example, think about “AI Agents” before directly going into implementation, understand how they work, “when” and “why” it adds value, and when it won’t add any value. And how will it help?</p>
</section>
<section id="business-focus" class="level2">
<h2 class="anchored" data-anchor-id="business-focus">Business Focus</h2>
<p>You need to translate the capabilities of AI into a real business outcome. If you go directly with the hype, aka agents, these days, you will fail. First, you need to peel apart the business requirements, ask “why” and “what” a lot of times to understand the real bottleneck/problem. Additionally, risks of mispredictions, hallucinations, biases, and misuses (some edge cases you will never think of will be found by the users) are here and will stay here. Knowing how to manage those risks while making a process an AI-enabled process is critical.</p>
</section>
<section id="bias-towards-delivery" class="level2">
<h2 class="anchored" data-anchor-id="bias-towards-delivery">Bias Towards Delivery</h2>
<p>Building cool things that have no value is not that important anymore (as it was when hype started). You need to build useful things; if it is both useful and cool, it is definitely better. Show that you can ship working solutions more than demos. An example from him: Before applying to Google Cloud, while he was writing a Java book, he made a Java application that runs on the Google Cloud and showed it on the interview, which turned the interview process into questions about his app instead of weird questions like how many windows in New-York.</p>
<p>And some additional points:</p>
<ul>
<li>You will make mistakes, so learn from them and also be helpful when someone else makes a mistake</li>
<li>Vibe coding is good unless you mindlessly copy-paste the code. Every time you use AI to generate code, you are taking technical debt.</li>
<li>Learning how to fine-tune those small LLMs is currently one of the most important things, due to privacy reasons in a lot of industries</li>
</ul>
<p>If you want to watch:</p>
<div class="quarto-video ratio ratio-16x9"><iframe data-external="1" src="https://www.youtube.com/embed/AuZoDsNmG_s" title="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>


</section>

 ]]></description>
  <category>ml</category>
  <guid>https://nusretozates.github.io/posts/career_advice_ai_summary/</guid>
  <pubDate>Fri, 19 Dec 2025 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Pytorch Geometric Basics: How Message Passing Works</title>
  <dc:creator>Nusret Ozates</dc:creator>
  <link>https://nusretozates.github.io/posts/gnn_basics/</link>
  <description><![CDATA[ 






<p>I’m working with GNNs for my MSc thesis and naturally chose PyTorch Geometric (PyG), one of the most popular libraries in the field. While PyG is incredibly easy to use, I realized I needed to understand how message passing works under the hood to effectively customize it for my specific experiments. Now that I’ve gained this understanding, I’ve written this post to share the inner workings of PyG with anyone else looking to build their own custom layers.</p>
<section id="introduction-to-message-passing-in-gnns" class="level3">
<h3 class="anchored" data-anchor-id="introduction-to-message-passing-in-gnns">Introduction to Message Passing in GNNs</h3>
<p>For those unfamiliar, Graph Neural Networks (GNNs) are a class of neural networks designed to operate on graph-structured data. They leverage the relationships between nodes (entities) and edges (connections) to learn representations that capture both local and global graph structures. Message passing is a fundamental operation in GNNs, where information is exchanged between nodes and their neighbors to update node representations.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>PyG uses “source_to_target” flow by default, meaning messages are sent from source nodes to target nodes. Source nodes are typically denoted with a subscript “_j” and target nodes with “_i”. You can remember it like: Source = neighbors, Target = self.</p>
</div>
</div>
<p>In PyG, message passing is typically implemented using the <code>MessagePassing</code> class, which provides a flexible framework for defining custom message-passing schemes. It has 4 important methods:</p>
</section>
<section id="propagate" class="level3">
<h3 class="anchored" data-anchor-id="propagate">1. Propagate</h3>
<p>This function is responsible for orchestrating the message-passing process. It takes an edge index, a.k.a adjacency matrix, as a <span style="color:cyan">mandatory parameter</span>. You can (and probably must) give feature matrix <span style="color:cyan">x</span>. In addition, you can pass any other necessary data for the later steps we will see. You don’t update/override this function; you pass the necessary data to it to be further used in the next steps.</p>
<p>Some example parameters:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">propagate(edge_index, x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>x)</span>
<span id="cb1-2">propagate(edge_index, x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>x, edge_attr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>edge_attr)</span>
<span id="cb1-3">propagate(edge_index, x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>x, norm<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>norm)</span></code></pre></div></div>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>Choosing the name for the feature vector <span style="color:cyan">x</span> is critical. If you use x=x like the example above, PyG will automatically split it into <span style="color:cyan">x_i</span> and <span style="color:cyan">x_j</span> for target and source nodes, respectively. If you use feature_vec=x, you should use <span style="color:cyan">feature_vec_i</span> and <span style="color:cyan">feature_vec_j</span> in the later steps’ parameter names.</p>
</div>
</div>
</section>
<section id="message" class="level3">
<h3 class="anchored" data-anchor-id="message">2. Message</h3>
<p>This is where you create a <span style="color:cyan">message</span> for the source node from neighboring nodes. This function takes <span style="color:cyan">x_j</span> as input by default, which is the feature vector of the source nodes. This means you have to give your feature matrix named as x=x in the propagate function, or override the parameter name in the message function.</p>
<p>You can also access any other data you passed in the propagate function, such as edge attributes or normalization factors. For example, in the second example of the propagate function, you give <span style="color:cyan">norm</span> parameter, you can access it in the message function as norm.</p>
<p>Some example parameters:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">message(x_j)</span>
<span id="cb2-2">message(x_j, norm)</span>
<span id="cb2-3">message(x_j, x_i, norm, edge_index, x)</span></code></pre></div></div>
<p>Also, an example implementation that normalizes the messages by their node’s degree:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> message(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, x_j, norm):</span>
<span id="cb3-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x_j has shape [E, out_channels]</span></span>
<span id="cb3-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Creating messages..."</span>)</span>
<span id="cb3-4">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"x_j shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>x_j<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-5">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"norm shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>norm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-6"></span>
<span id="cb3-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4: Normalize node features.</span></span>
<span id="cb3-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> norm.view(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x_j</span></code></pre></div></div>
</section>
<section id="aggregate" class="level3">
<h3 class="anchored" data-anchor-id="aggregate">3. Aggregate</h3>
<p>Now that you have messages from your neighboring nodes, this is where you aggregate those messages. This method calls the <span style="color:cyan">Aggregator</span> object of the class by default, which is set to “add” by default. You can change it to “mean” or “max” when you initialize your custom MessagePassing class or implement your own aggregate function.</p>
<p>You can also override this method to implement your own aggregation logic. By overriding this method, you can weight the messages using any data you want, before using the default sum aggregation as an example.</p>
<p>It takes the following parameters:</p>
<ul>
<li><span style="color:cyan">inputs</span> which is the messages created in the message function</li>
<li><span style="color:cyan">index</span> that says the target node each message belongs to</li>
</ul>
<p>And whatever you want from the propagate function.</p>
</section>
<section id="update" class="level3">
<h3 class="anchored" data-anchor-id="update">4. Update</h3>
<p>This is the final step where you update the target node features using the aggregated messages. Depending on your architecture, you might do nothing here and return the aggregated messages, such as when you add self-loops. Alternatively, add the source node features to the aggregated messages or pass them through a neural network layer.</p>
<p>It takes <span style="color:cyan">inputs</span>, which is the aggregated messages from the aggregate function and whatever you want from the propagate function.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>In this blog post, we explored how message passing works in PyTorch Geometric by breaking down the key methods of the <code>MessagePassing</code> class: <code>propagate</code>, <code>message</code>, <code>aggregate</code>, and <code>update</code>. If you want to customize your GNN architecture and experiment with different message-passing schemes, understanding these methods is critical. With this knowledge, you can implement your own GNN layers and tailor them to your specific needs. I will drop a simple working code that I’ve borrowed from the PyG documentation below for reference.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Optional</span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch</span>
<span id="cb4-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> torch <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Tensor</span>
<span id="cb4-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> torch.nn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Linear, Parameter</span>
<span id="cb4-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> torch_geometric.nn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> MessagePassing</span>
<span id="cb4-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> torch_geometric.utils <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> add_self_loops, degree</span>
<span id="cb4-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> torch_geometric.data <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Data</span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> GCNConv(MessagePassing):</span>
<span id="cb4-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, in_channels, out_channels):</span>
<span id="cb4-12">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(aggr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'add'</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Add" aggregation (Step 5).</span></span>
<span id="cb4-13">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.lin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Linear(in_channels, out_channels, bias<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb4-14">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bias <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Parameter(torch.empty(out_channels))</span>
<span id="cb4-15"></span>
<span id="cb4-16">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.reset_parameters()</span>
<span id="cb4-17"></span>
<span id="cb4-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> reset_parameters(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb4-19">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.lin.reset_parameters()</span>
<span id="cb4-20">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bias.data.zero_()</span>
<span id="cb4-21"></span>
<span id="cb4-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> forward(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, x, edge_index):</span>
<span id="cb4-23">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x has shape [N, in_channels]</span></span>
<span id="cb4-24">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edge_index has shape [2, E]</span></span>
<span id="cb4-25">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forward pass..."</span>)</span>
<span id="cb4-26">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"x shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-27">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"edge_index shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>edge_index<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-28"></span>
<span id="cb4-29">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Add self-loops to the adjacency matrix.</span></span>
<span id="cb4-30">        edge_index, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> add_self_loops(edge_index, num_nodes<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>x.size(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb4-31"></span>
<span id="cb4-32">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Linearly transform node feature matrix.</span></span>
<span id="cb4-33">        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.lin(x)</span>
<span id="cb4-34"></span>
<span id="cb4-35">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: Compute normalization.</span></span>
<span id="cb4-36">        source, target <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> edge_index</span>
<span id="cb4-37">        deg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> degree(target, x.size(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>x.dtype)</span>
<span id="cb4-38">        deg_inv_sqrt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deg.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">pow</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb4-39">        deg_inv_sqrt[deg_inv_sqrt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'inf'</span>)] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb4-40">        norm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deg_inv_sqrt[source] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> deg_inv_sqrt[target]</span>
<span id="cb4-41"></span>
<span id="cb4-42">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4-5: Start propagating messages.</span></span>
<span id="cb4-43">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.propagate(edge_index, x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>x, norm<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>norm)</span>
<span id="cb4-44"></span>
<span id="cb4-45">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 6: Apply a final bias vector.</span></span>
<span id="cb4-46">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bias</span>
<span id="cb4-47"></span>
<span id="cb4-48">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb4-49"></span>
<span id="cb4-50">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> message(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, x_i, x_j, norm, edge_index):</span>
<span id="cb4-51">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x_j has shape [E, out_channels]</span></span>
<span id="cb4-52">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Creating messages..."</span>)</span>
<span id="cb4-53">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"x_i shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>x_i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-54">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"x_j shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>x_j<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-55">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"norm shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>norm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-56"></span>
<span id="cb4-57">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4: Normalize node features.</span></span>
<span id="cb4-58">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> norm.view(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x_j</span>
<span id="cb4-59"></span>
<span id="cb4-60">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> aggregate(</span>
<span id="cb4-61">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>,</span>
<span id="cb4-62">        inputs: Tensor,</span>
<span id="cb4-63">        index: Tensor,</span>
<span id="cb4-64">        ptr: Optional[Tensor] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>,</span>
<span id="cb4-65">        dim_size: Optional[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>,</span>
<span id="cb4-66">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Tensor:</span>
<span id="cb4-67">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Aggregating messages..."</span>)</span>
<span id="cb4-68">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Inputs shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>inputs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-69">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Index shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>index<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-70">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(index)</span>
<span id="cb4-71">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().aggregate(inputs, index, ptr, dim_size)</span>
<span id="cb4-72"></span>
<span id="cb4-73">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> update(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, inputs: Tensor) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Tensor:</span>
<span id="cb4-74">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Updating node embeddings..."</span>)</span>
<span id="cb4-75">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Inputs shape: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>inputs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-76">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(inputs)</span>
<span id="cb4-77">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().update(inputs)</span>
<span id="cb4-78"></span>
<span id="cb4-79"></span>
<span id="cb4-80">edge_index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.tensor([[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb4-81">                           [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb4-82">                           [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb4-83">                           [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>torch.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">long</span>)</span>
<span id="cb4-84">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.tensor([[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>torch.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb4-85"></span>
<span id="cb4-86">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Data(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>x, edge_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>edge_index.t().contiguous())</span>
<span id="cb4-87"></span>
<span id="cb4-88">conv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GCNConv(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-89">out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> conv(data.x, data.edge_index)</span>
<span id="cb4-90"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(out)</span></code></pre></div></div>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><a href="https://pytorch-geometric.readthedocs.io/en/latest/tutorial/create_gnn.html">PyTorch Geometric Documentation</a></li>
<li><a href="https://distill.pub/2021/gnn-intro/">For GNN basics, and also for the headline image reference</a></li>
<li><a href="https://www.youtube.com/playlist?list=PLoROMvodv4rOP-ImU-O1rYRg2RFxomvFp">Also, a great YouTube video for GNN basics from Stanford</a></li>
</ul>
<p>Thanks for coming so far, have fun!</p>


</section>

 ]]></description>
  <category>ml</category>
  <guid>https://nusretozates.github.io/posts/gnn_basics/</guid>
  <pubDate>Fri, 17 Oct 2025 21:00:00 GMT</pubDate>
  <media:content url="https://nusretozates.github.io/posts/gnn_basics/gnn.png" medium="image" type="image/png" height="67" width="144"/>
</item>
<item>
  <title>Academic Writing Notes: Paragraphs Development and Sentence Skills</title>
  <dc:creator>Nusret Ozates</dc:creator>
  <link>https://nusretozates.github.io/posts/paragraph_dev_sentece_skill/</link>
  <description><![CDATA[ 






<p>Do you remember the times you read an article/paper and you couldn’t understand what the author wanted to say even though you know the underlying concepts? Even the most brilliant ideas can be lost in poor writing or simplest ideas can be hard to understand. Choppy paragraphs, misplaced phrases, and grammatical run-ons can obscure your argument and frustrate your reader.</p>
<p>In this post, I will share what I learned from Koc University Academic Writing class videos and materials.</p>
<section id="topic-and-stress" class="level2">
<h2 class="anchored" data-anchor-id="topic-and-stress">Topic and Stress</h2>
<p>You can divide a sentence into two parts: the topic and the stress. The topic is what the sentence is about, and the stress is what you want to say about the topic or what is new information.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 47%">
<col style="width: 52%">
</colgroup>
<thead>
<tr class="header">
<th><span style="color: cyan">Topic Part</span>, Readers:</th>
<th style="text-align: left;"><span style="color: red">Stress Part</span>, Readers</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>- Expect to understand what the sentence is about.</td>
<td style="text-align: left;">- Expect to see new and imporant ideas.</td>
</tr>
<tr class="even">
<td>- Try to connect the sentence to what they have already read.</td>
<td style="text-align: left;">- Focus most of their interpretative effort.</td>
</tr>
</tbody>
</table>
<p>Example:</p>
<p><span style="color: cyan">Accounts of depression</span> evolved after psychologists introduced the <span style="color: red">concepts of defeat and entrapment</span>.</p>
<section id="known-new-contract" class="level3">
<h3 class="anchored" data-anchor-id="known-new-contract">Known-New Contract</h3>
<ul>
<li>Introduce your readers to the “big picture” first by giving them information they <strong>already know</strong> (the topic part).</li>
<li>Then they can <strong>link what’s familiar</strong> to the <strong>new information</strong> you give them (the stress part).</li>
</ul>
<p>As that <strong>new information</strong> becomes familiar, it too becomes old information that can link to newer information.</p>
<p>Example:</p>
<p><span style="color: cyan">Accounts of depression</span> evolved after psychologists introduced the <span style="color: red">concepts of defeat and entrapment</span>. <span style="color: cyan">These concepts</span> have been implicated in <span style="color: red">theoretical accounts of anxiety and suicide</span>. <span style="color: cyan">Such theories</span>..</p>
<p>Example 2:</p>
<p>However, <span style="color: red">managed WebRTC services using SFU architecture and SDN-assisted IP multicasting of scalable video within WebRTC system</span> <span style="color: cyan">are discussed for the first time in this paper</span></p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>The sentence above starts with a lot of complex terms and we don’t know their importance until the end of sentence, which is bad. The simple fix is just reversing the order!</p>
</div>
</div>
<p>However, <span style="color: cyan">this paper is the first</span> <span style="color: red">to analyze managed WebRTC services using SFU architecture and SDN-assisted IP multicasting of scalable video within WebRTC system</span></p>
</section>
<section id="what-if-i-want-to-stress-multiple-ideas-in-a-sentence" class="level3">
<h3 class="anchored" data-anchor-id="what-if-i-want-to-stress-multiple-ideas-in-a-sentence">What If I Want to Stress Multiple Ideas in a Sentence?</h3>
<ul>
<li>Try to introduce just one major idea per sentence, especially if the idea is complex.</li>
<li>If your text is complex and you have two ideas worth emphasizing, create two sentences.</li>
</ul>
<p>Example:</p>
<p>However, <span style="color: red">it uses the already limited upload bandwidth of clients inefficiently</span> and is not scalable with the number of clients, i.e., it becomes impractical as the number of endpoints grows bigger.</p>
<p>Instead of the sentence above, you can write:</p>
<p>However, <span style="color: red">mesh topology uses the already limited upload bandwidth of clients inefficiently</span>. It is not scalable with the number of clients, i.e., it becomes impractical as the number of endpoints grows bigger.</p>
<p>Sometimes two sentences should be one if they refer to the same idea.</p>
<p>Example:</p>
<p>Such leaders should make the work of their followers more pleasant. Moreover, they should treat the followers as equals, and respect them.</p>
<p>Instead of the sentence above, you can write:</p>
<p>Such leaders should make the work of their followers more pleasant by treating them equally and respectfully.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Additional Steps to Edit Complex Writing
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>Move the subject and the verb close together.</li>
<li>Break apart sentences that contain too much new information.</li>
<li>Use transitional phrases to indicate relationships: moreover, in addition, consequently, therefore…</li>
</ul>
</div>
</div>
</section>
</section>
<section id="run-ons" class="level2">
<h2 class="anchored" data-anchor-id="run-ons">Run-Ons</h2>
<p>A run-on is <strong>two complete thoughts</strong> run together with no sign to mark the break between them or with just a comma:</p>
<p>Then, in [2], they also presented a bisection algorithm to compute <img src="https://latex.codecogs.com/png.latex?%5Cepsilon">-pseudospectral abscissa of a fixed matrix, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Calpha_%7B%5Cepsilon%7D(A)">, and tried to compute minimum <img src="https://latex.codecogs.com/png.latex?%5Cepsilon">-pseudospectral abscissa over feasible matrices, <span style="color: red"><strong>however, an algorithm wasn’t presented yet.</strong></span></p>
<p>vs</p>
<p>Then, in [2], they also presented a bisection algorithm to compute <img src="https://latex.codecogs.com/png.latex?%5Cepsilon">-pseudospectral abscissa of a fixed matrix, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Calpha_%7B%5Cepsilon%7D(A)">. They also tried to compute minimum <img src="https://latex.codecogs.com/png.latex?%5Cepsilon">-pseudospectral abscissa over feasible matrices. <span style="color: red"><strong>However, an algorithm wasn’t presented yet.</strong></span></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>I personally didn’t like the second version too because it has too many “they also” parts.</p>
</div>
</div>
</section>
<section id="fragments" class="level2">
<h2 class="anchored" data-anchor-id="fragments">Fragments</h2>
<p>A sentence fragment is a group of words that lacks a subject or a verb and does not express a complete thought:</p>
<p><span style="color: red">Purdue offers many majors in engineering.</span> <span style="color: red"><strong>Such as electrical, chemical, and industrial engineering.</strong></span></p>
<p>Purdue offers many majors in engineering <strong>such as electrical, chemical, and industrial engineering</strong>.</p>
</section>
<section id="parallelism" class="level2">
<h2 class="anchored" data-anchor-id="parallelism">Parallelism</h2>
<p>Words in a pair or series should have a parallel structure.</p>
<p><span style="color: red"><strong>Not Parallel:</strong></span> The production manager was asked to write his report quickly, accurately, and <span style="color: red"><strong>in a detailed manner.</strong></span></p>
<p><span style="color: cyan"><strong>Parallel:</strong></span> The production manager was asked to write his report quickly, accurately, and <span style="color: cyan"><strong>thoroughly.</strong></span></p>
</section>
<section id="misplaced-modifiers" class="level2">
<h2 class="anchored" data-anchor-id="misplaced-modifiers">Misplaced Modifiers</h2>
<p>Misplaced modifiers do not describe the word in the way the writer intended because of their wrong place in a sentence.</p>
<p>George couldn’t drive to work in his small sports car <span style="color: red"><strong>with a broken leg.</strong></span></p>
<p><span style="color: cyan"><strong>With a broken leg,</strong></span> George couldn’t drive to work in his small sports car.</p>
<p>In this example, we and transformer models know that George has a broken leg, not the car. But grammatically, the modifier “with a broken leg” seems to describe the car. This is an easy example but in a academic text, it can be more complex and harder to spot.</p>
<p>In order to avoid misplaced modifiers, place the words as close as possible to what they describe.</p>
</section>
<section id="dangling-modifiers" class="level2">
<h2 class="anchored" data-anchor-id="dangling-modifiers">Dangling Modifiers</h2>
<p>A modifier that opens a sentence must be <strong>followed immediately</strong> by the <strong>word it is meant to describe</strong>. Otherwise the sentence takes on an unintended meaning.</p>
<p><span style="color:red">While smoking a pipe, <u>my dog</u> sat with me.</span></p>
<p>While smoking a pipe, <u>I sat with my dog</u>.</p>
<p>While <u>I was smoking a pipe</u>, my dog sat with me.</p>
<p>Again, this is also an easy example but in a academic text, it can be more complex and harder to spot.</p>
</section>
<section id="sentence-variety" class="level2">
<h2 class="anchored" data-anchor-id="sentence-variety">Sentence Variety</h2>
<ul>
<li><p>Too many sentences with the <strong>same structure and length</strong> can grow <strong>monotonous</strong> for readers.</p></li>
<li><p><strong>Varying</strong> sentence <strong>style and structure</strong> can also reduce repetition and add emphasis.</p></li>
<li><p><strong>Long sentences</strong> work well for incorporating <strong>a lot of information</strong>, and <strong>short sentences</strong> can often maximize crucial points.</p></li>
</ul>
<section id="overusing-long-sentences" class="level3">
<h3 class="anchored" data-anchor-id="overusing-long-sentences">Overusing Long Sentences</h3>
<p>Long sentences can be difficult to read and understand, especially if they contain multiple ideas or clauses. Breaking up long sentences into shorter ones can improve clarity and readability.</p>
<p>The company reported that yearly profit growth, which had steadily increased by more than 7% since 1989, had stabilized in 2009 with a 0% comp, and in 2010, the year they launched the OWN project, actually decreased from the previous year by 2%. This announcement stunned Wall Street analysts, but with the overall decrease in similar company profit growth worldwide, as reported by Author (Year) in his article detailing the company’s history, the company’s announcement aligns with industry trends and future industry predictions.</p>
<p>The company reported that profit growth stabilized in 2009, though it had steadily increased by more than 7% since 1989. In 2010, the year they launch the OWN project, company profit growth decreased from the previous year. This announcement stunned Wall Street analysts. According to Author (Year), however, this decrease is exemplar of a trend across similar company profit growth worldwide; it also supports future predictions for the industry.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Notice that the sentence count only increased by two, but thanks to the choice of where a sentence begin and end, the paragraph is easier to read. Moreover, the sentence variety is increased.</p>
</div>
</div>
</section>
<section id="short-sentences" class="level3">
<h3 class="anchored" data-anchor-id="short-sentences">Short Sentences</h3>
<p>Read the text below with your voice:</p>
<p>Too many short sentences can hurt an essay. They can make the writing seem choppy. The writing may seem like it is below a college level. Readers may lose interest. They may not want to continue reading.</p>
<p>See the effect? Let’s fix it:</p>
<p>Too many short sentences can hurt an essay, <strong>for</strong> it can make the writing seem choppy <strong>and</strong> below a college level. Because of this, readers may lose interest and not want to continue reading.</p>
</section>
</section>
<section id="change-the-rhythm" class="level2">
<h2 class="anchored" data-anchor-id="change-the-rhythm">Change the Rhythm!</h2>
<p>Change the rhythm of your writing by varying sentence length and structure. As you will see the example below, varying sentence length and structure can make your writing more interesting and engaging.</p>
<p>Vary the rhythm by alternating short and long sentences:</p>
<p>The Winslow family visited Canada and Alaska last summer to find some Native American art. In Anchorage stores they found some excellent examples of soapstone carvings. But they couldn’t find a dealer selling any of the woven wall hangings they wanted. They were very disappointed when they left Anchorage empty-handed.</p>
<p>The Winslow family visited Canada and Alaska last summer to find some native American art, such as soapstone carvings and wall hangings. Anchorage stores had many soapstone items available. Still, they were disappointed to learn that wall hangings, which they had especially wanted, were difficult to find. Sadly, they left empty-handed.</p>
<p>I think I see something similar to this in the novels I’ve read.</p>
</section>
<section id="repeated-subjects-or-topics" class="level2">
<h2 class="anchored" data-anchor-id="repeated-subjects-or-topics">Repeated Subjects or Topics</h2>
<ul>
<li>Handling the same topic for several sentences can lead to repetitive sentences. When that happens, consider using these parts of speech to fix the problem:</li>
</ul>
<p><strong>Relative pronouns</strong></p>
<p>Indiana used to be mainly an agricultural state. It has recently attracted more industry. Indiana, <span style="color: cyan"><strong>which</strong></span> used to be mainly an agricultural state, has recently attracted more industry.</p>
<p><strong>Participles</strong></p>
<p>Wei Xie was surprised to get a phone call from his sister. He was happy to hear her voice again. <span style="color: cyan"><strong>Surprised</strong></span> to get a phone call from his sister, Wei Xie was happy to hear her voice again.</p>
<p><strong>Prepositions</strong></p>
<p>The university has been facing pressure to cut its budget. It has eliminated funding for important programs. <span style="color: cyan"><strong>Under pressure</strong></span> to cut its budget, the university has eliminated funding for important programs.</p>
</section>
<section id="final-words" class="level2">
<h2 class="anchored" data-anchor-id="final-words">Final Words</h2>
<p>Finally, all these rules and tips are not strict rules, but guidelines to help you improve your writing. Also, it is very easy to forget them. The best thing you can do is keep writing constantly, while editing your own writing with these rules in mind. Over time, you will internalize these rules and your writing will improve. I also believe that reading a lot of well-written articles and books will help you improve your writing skills.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li>Koc University Writing Center</li>
<li>Sentence Variety. (2018). Retrieved from https://owl.purdue.edu/owl/general_writing/academic_writing/sentence_variety/index.html</li>
<li>Making Complex Writing Intelligible with Known-New Contract. (2018). Global Communication Center, Carnegie Mellon University. Retrieved from https://www.cmu.edu/gcc/handouts/old-new-handout-pdf</li>
</ul>


</section>

 ]]></description>
  <category>academy</category>
  <guid>https://nusretozates.github.io/posts/paragraph_dev_sentece_skill/</guid>
  <pubDate>Sun, 12 Oct 2025 21:00:00 GMT</pubDate>
  <media:content url="https://nusretozates.github.io/posts/paragraph_dev_sentece_skill/focus.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Test-Time Compute, Reasoning and Human Brain</title>
  <dc:creator>Nusret Ozates</dc:creator>
  <link>https://nusretozates.github.io/posts/test_time_compute/</link>
  <description><![CDATA[ 






<p>I have lots of things to do, but I’ve suddenly been struck by inspiration, and since the well-known work-avoidance mechanism has kicked in, I’m going to write down my thoughts on test-time compute, shared decoders, and reasoning. Here we go, a theoretical and lengthy piece is coming.</p>
<p>One of the things I love about LLMs is that they handle multiple tasks with a single loss and a single branch. That is, instead of a shared encoder + separate decoders for each task like in U-net models, there’s just a transformer decoder. As far as I know, this comes from T5 models where we model all NLP tasks as text-to-text. We’ll get back to LLMs in a bit, but let’s take a look at the vision side for now.</p>
<p>I don’t think there’s an equivalent of this in vision, for example, how would you combine segmentation and classification tasks? At the very least, the last layers would have to be different. However, there’s a model that comes very, very close to combining these, and the model’s developers are inspired by the human brain. The model is called BU-TD. Let’s start with the inspiration part. These folks are saying, ‘segmenting anything and everything at once’ is not the right approach; the human brain doesn’t work that way.</p>
<p><img src="https://nusretozates.github.io/posts/test_time_compute/img.png" class="img-fluid"></p>
<p>For example, the longer you look at the image above, the more details emerge; the brain doesn’t grasp the entire image with all its details at once. So why are we trying to make models do this? This is where BU-TD comes into play.</p>
<p><img src="https://nusretozates.github.io/posts/test_time_compute/img_1.png" class="img-fluid"></p>
<p>Again, in this model inspired by the human brain, first an encoder processes the image, and the decoder takes the vector coming from the encoder and additionally receives a task and argument vector. For example,</p>
<p><img src="https://nusretozates.github.io/posts/test_time_compute/img_2.png" class="img-fluid"></p>
<p>We tell the model to look at the image, task: find hairstyle, argument: bob. Afterwards, the decoder can optionally output the segmentation of Bob’s hair, but it may not; that part is a bit vague. By feeding the decoder’s result back into the encoder, we get the result corresponding to that style, and so on.</p>
<p>The nice thing about this system is that it first uses the weights in the decoder to the fullest, making it a parameter-efficient model. Secondly, since each task, argument, etc., is decoupled, the system learns concepts better. For example, during training, brother Bob is always bald, so Bob’s hairstyle value is always bald during training. However, since the model learns what short hair and long hair are from other examples independently of the person, it can detect this during testing if Bob comes to Turkey and gets hair implants. Thanks to this, the model learns much better with much less data.</p>
<p><img src="https://nusretozates.github.io/posts/test_time_compute/img_3.png" class="img-fluid"></p>
<p>Now we come back to LLMs. We are actually applying the TD part of this model, that is, using a single decoder for each task, in LLMs since the T5 models, especially in PrefixLM models, and when image tokens and (I think) the question are processed with self-attention, the TD logic is formed.</p>
<p>The second part is that when people look at a picture, we extract the details, relationships, etc. over time; all the details don’t come at a single glance, right? Well, this part actually corresponds to the concept we call test-time compute. For example, as we look at the image on the left below, the details on the right emerge.</p>
<p><img src="https://nusretozates.github.io/posts/test_time_compute/img_4.png" class="img-fluid"></p>
<p>Based on what we’ve learned up to this point, trying to process everything at once is not logical. Learning the relationships one by one during training and processing the image over time using more compute/time during testing is an effective solution in terms of both the number of parameters and learning more with less data. Our next problem is this: We don’t want to process the image all at once, okay, but we also don’t want to process the entire image; we want to process as much as necessary for the information we are interested in to save time and money. This is where reasoning comes into play. What does a good LLM model do in terms of reasoning? It divides the question we ask into the necessary parts and solves the parts step by step, and stops when it reaches the result. By doing this, we have the following system:</p>
<p><img src="https://nusretozates.github.io/posts/test_time_compute/img_5.png" class="img-fluid"></p>
<p>I’m saying that if I were shown a picture and asked, ‘What is the size of the bag of the woman holding the bag that the girl is looking at?’, my brain would process the image in a similar way and wouldn’t look for more details.</p>
<p>I think the BU-TD model itself is very limited in terms of input and output ranges but the main idea is still strong and I believe VLMs are very close to implementing this idea into the AI models.</p>
<p>That’s it, I couldn’t come to a conclusion with the text. These things suddenly came to my mind while reading a very unrelated article, I thought I’d write them down. Good luck and thanks for reading up to this point!</p>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><a href="https://arxiv.org/abs/2105.05592">BU-TD Model: “Image interpretation by iterative bottom-up top-down processing”</a></li>
<li><a href="https://arxiv.org/abs/1910.10683">T5 Model: “Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer”</a></li>
</ul>


</section>

 ]]></description>
  <category>ml</category>
  <category>llm</category>
  <guid>https://nusretozates.github.io/posts/test_time_compute/</guid>
  <pubDate>Thu, 06 Feb 2025 21:00:00 GMT</pubDate>
  <media:content url="https://nusretozates.github.io/posts/test_time_compute/img.png" medium="image" type="image/png" height="69" width="144"/>
</item>
<item>
  <title>BERT: Encoder Stack Is All You Need</title>
  <dc:creator>Nusret Ozates</dc:creator>
  <link>https://nusretozates.github.io/posts/bert/</link>
  <description><![CDATA[ 






<p>In this article, I will talk about the BERT model and I will add sources to learn more about the BERT. To understand how BERT works, you need to understand how transformer models work. You can read my article about <a href="../../posts/transformers/index.html">Transformers</a> or you can learn it from here.</p>
<p>BERT [1] (Bidirectional Encoder Representations from Transformers) is a new language representation model. It is one of the ways of creating word embeddings and sentence representation vectors. BERT uses the encoder stack of the transformer model to output the representation of each token in the given input. Additionally, it has a special token “[CLS]” at the beginning of the inputs to use in classification tasks. BERT has two steps:</p>
<ol type="1">
<li>Pre-training</li>
<li>Fine-tuning</li>
</ol>
<p>The pre-training step is where the model is trained to learn the given language(s) and output meaningful representation of the given input sequence (“sequence” term especially used here to emphasize given input can be more than one sentence.). To do this step, the model is trained using unlabeled text data using two different tasks. For fine-tuning, labeled data is needed and the model is fine-tuned to do given downstream tasks such as classification or question answering.</p>
<section id="pre-training" class="level2">
<h2 class="anchored" data-anchor-id="pre-training">Pre-training</h2>
<p>Pre-training is done with two different tasks:</p>
<ol type="1">
<li>Masked Language Model (MLM)</li>
<li>Next Sentence Prediction (NSP)</li>
</ol>
<section id="masked-language-model" class="level3">
<h3 class="anchored" data-anchor-id="masked-language-model">Masked Language Model</h3>
<p>According to the authors of the BERT model, deep bidirectional models are powerful than unidirectional models. The problem with bidirectional models is, it allows to each word indirectly “see itself” and the model can easily predict the target word. To solve this problem, the authors mask some percentage(%15 in the article) of the input tokens randomly by changing the word with the special token “[MASK]” and the model is tasked with the prediction of the masked tokens. This is called the “Masked Language Model” but it is called a “cloze test” (fill in the blanks questions) in literature. The output of the hidden vectors (only at the masked tokens’ positions) are given into a softmax layer with the neuron size is equal to vocabulary size to predict real words.</p>
<p>The “[MASK]” token is used for pre-training but not in fine-tuning section. This causes a mismatch and to solve this problem, the authors choose to not using the “[MASK]” token every time. The selected token i with the %80 probability, will be replaced with the “[MASK]” token, with %10 probability it will be replaced by a random token, and with %10 probability, it will remain unchanged. Then the token in the i’th position will be used to predict the original token.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nusretozates.github.io/posts/bert/masked.png" class="img-fluid figure-img"></p>
<figcaption>An example of the masked language model from Jay Alammar [2]</figcaption>
</figure>
</div>
</section>
<section id="next-sentence-prediction" class="level3">
<h3 class="anchored" data-anchor-id="next-sentence-prediction">Next Sentence Prediction</h3>
<p>Some NLP tasks such as Question Answering and Natural Language Inference(Determining whether a “hypothesis” is true ( entailment), false (contradiction), or undetermined (neutral) given a “premise”.) need to understand the relation between sentences. This relation cannot be captured with the masked language model task. To train a model that can capture the relationship between two sentences, the authors give two sentences “Sentence A” and “Sentence B” as input. With the %50 probability, “Sentence B” is the actual sentence that comes after “Sentence A” and with the %50 probability it is a random sentence. As in the example above, the special “[CLS]” token is used to predict if the second sentence comes after the first sentence.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nusretozates.github.io/posts/bert/nsp.png" class="img-fluid figure-img"></p>
<figcaption>An example of the next sentence prediction model from Jay Alammar [2]</figcaption>
</figure>
</div>
</section>
<section id="fine-tuning" class="level3">
<h3 class="anchored" data-anchor-id="fine-tuning">Fine-tuning</h3>
<p>It is a very simple step, by swapping the input depending on the task (single sentence or two sentences) and connecting the output to an appropriate classification layer. For a sentence classification task, the output vector of the model’s “[CLS]” token could be connected to a softmax layer and</p>
<ol type="1">
<li>Freeze the BERT weights and train the latest classification weights</li>
<li>Fine-tune all weights</li>
</ol>
</section>
<section id="code-example" class="level3">
<h3 class="anchored" data-anchor-id="code-example">Code Example</h3>
<p>Using Tensorflow Hub, training or fine-tuning BERT models is very easy. In the following steps, I will show you how you can use a BERT model to detect toxicity in texts for the Toxic Comment Classification Challenge. Download the train data first and create a Tensorflow dataset, separate it as train and validation set using this code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tensorflow <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> tf</span>
<span id="cb1-2"></span>
<span id="cb1-3">dataset <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.data.experimental.make_csv_dataset(</span>
<span id="cb1-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data/kaggle_toxic_comments/train.csv'</span>, batch_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>batch_size, num_epochs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-5">    , select_columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'comment_text'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'toxic'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'severe_toxic'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'obscene'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'threat'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'insult'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'identity_hate'</span>])</span>
<span id="cb1-6"></span>
<span id="cb1-7">dataset <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dataset.unbatch()</span>
<span id="cb1-8">validation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dataset.take(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span>
<span id="cb1-9">train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dataset.skip(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span>
<span id="cb1-10"></span>
<span id="cb1-11">train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> data: (</span>
<span id="cb1-12">    dataset_preprocessing(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'comment_text'</span>]),</span>
<span id="cb1-13">    [data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'toxic'</span>],</span>
<span id="cb1-14">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'severe_toxic'</span>],</span>
<span id="cb1-15">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'obscene'</span>],</span>
<span id="cb1-16">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'threat'</span>],</span>
<span id="cb1-17">     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data['sexual_explicit'],</span></span>
<span id="cb1-18">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'insult'</span>],</span>
<span id="cb1-19">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'identity_hate'</span>],</span>
<span id="cb1-20">     ])).batch(batch_size).cache().prefetch(tf.data.AUTOTUNE)</span>
<span id="cb1-21"></span>
<span id="cb1-22">validation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> validation.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> data: (</span>
<span id="cb1-23">    dataset_preprocessing(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'comment_text'</span>]),</span>
<span id="cb1-24">    [data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'toxic'</span>],</span>
<span id="cb1-25">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'severe_toxic'</span>],</span>
<span id="cb1-26">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'obscene'</span>],</span>
<span id="cb1-27">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'threat'</span>],</span>
<span id="cb1-28">     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data['sexual_explicit'],</span></span>
<span id="cb1-29">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'insult'</span>],</span>
<span id="cb1-30">     data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'identity_hate'</span>],</span>
<span id="cb1-31">     ])).batch(batch_size).cache().prefetch(tf.data.AUTOTUNE)</span>
<span id="cb1-32"></span>
<span id="cb1-33"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> train, test</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Note from 2026
</div>
</div>
<div class="callout-body-container callout-body">
<p>The code above is too old and you would likely use huggingface transformers library to do the same task today.</p>
</div>
</div>
<p>In the code above, I did some preprocessing but you can just use “data[“comment_text”]”.</p>
<p>Let’s create the BERT model using the Tensorflow Hub:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tensorflow.keras.layers <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tensorflow_hub <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> hub</span>
<span id="cb2-3"></span>
<span id="cb2-4">text_input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Input(shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.string)</span>
<span id="cb2-5">preprocessor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hub.KerasLayer(</span>
<span id="cb2-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3"</span>)</span>
<span id="cb2-7">encoder_inputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> preprocessor(text_input)</span>
<span id="cb2-8">encoder <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hub.KerasLayer(</span>
<span id="cb2-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/4"</span>,</span>
<span id="cb2-10">    trainable<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb2-11">outputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> encoder(encoder_inputs)</span>
<span id="cb2-12">pooled_output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> outputs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pooled_output"</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># [batch_size, 512].</span></span>
<span id="cb2-13">sequence_output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> outputs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sequence_output"</span>][:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, :]</span></code></pre></div></div>
<p>Now we have two different outputs, pooled output, and sequence output. The pooled output represents each input sequence as a whole, and the sequence output represents each input token in context. Either of those can be used as input to further model building. For this task I want to use the output of the [CLS] token so I connect the sequence_output to a sigmoid layer and create the model like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">classification_output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Dense(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, activation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sigmoid'</span>)(sequence_output)</span>
<span id="cb3-2"></span>
<span id="cb3-3">embedding_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.Model(text_input, classification_output)</span></code></pre></div></div>
<p>Now compile the model and train it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">embedding_model.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">compile</span>(</span>
<span id="cb4-2">    optimizer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.keras.optimizers.Nadam(learning_rate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.025</span>),</span>
<span id="cb4-3">    loss<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.keras.losses.BinaryCrossentropy(),</span>
<span id="cb4-4">    metrics<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[tf.keras.metrics.AUC()],</span>
<span id="cb4-5">    run_eagerly<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb4-6"></span>
<span id="cb4-7">)</span>
<span id="cb4-8"></span>
<span id="cb4-9">embedding_model.summary()</span>
<span id="cb4-10"></span>
<span id="cb4-11">embedding_model.fit(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>train_data, validation_data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>test_data, epochs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<p>Thanks for reading!</p>
</section>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ol type="1">
<li><a href="https://arxiv.org/pdf/1810.04805.pdf" class="uri">https://arxiv.org/pdf/1810.04805.pdf</a> - The BERT paper.</li>
<li><a href="http://jalammar.github.io/illustrated-bert/" class="uri">http://jalammar.github.io/illustrated-bert/</a> - Awesome and more detailed explanation of BERT model</li>
<li><a href="https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/4" class="uri">https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/4</a> - An example BERT model from Tensorflow Hub</li>
</ol>


</section>

 ]]></description>
  <category>ml</category>
  <guid>https://nusretozates.github.io/posts/bert/</guid>
  <pubDate>Wed, 23 Jun 2021 21:00:00 GMT</pubDate>
  <media:content url="https://nusretozates.github.io/posts/bert/masked.png" medium="image" type="image/png" height="95" width="144"/>
</item>
<item>
  <title>Transformer Architecture: How Transformer Models Work?</title>
  <dc:creator>Nusret Ozates</dc:creator>
  <link>https://nusretozates.github.io/posts/transformers/</link>
  <description><![CDATA[ 






<p>Before Transformers, RNNs with attention mechanisms were state-of-the-art approaches to language modeling and neural machine translation. But RNNs have a very critical problem. The structure of RNN does not allow to do parallel computing. There were some optimizations to make them faster but the main problem remains. The Transformer model completely removed RNNs and built all architecture based on attention mechanism.</p>
<p>Like most neural machine translation models, Transformers have an encoder-decoder structure. It uses stacked encoders that contain attention layers and Feed Forward Neural networks.</p>
<section id="encoder-and-decoder-stacks" class="level2">
<h2 class="anchored" data-anchor-id="encoder-and-decoder-stacks">Encoder and Decoder Stacks</h2>
<section id="encoder" class="level3">
<h3 class="anchored" data-anchor-id="encoder">Encoder</h3>
<p>Each encoder layer has two sub-layers, the “multi-head attention layer” (It will be explained in the next chapters) and a Feedforward Neural Network, both of them followed by a normalization layer and there is 2 residual connection around each sub-layer. Each encoder layer has the same structure but they do not have the same weights. To use residual connections, all sub-layers and embedding layers output the same dimension. We denote this dimension as parameter <img src="https://latex.codecogs.com/png.latex?d_%0A%7Bmodel%7D">. In the original article <img src="https://latex.codecogs.com/png.latex?d_%7Bmodel%7D%20=%20512">. The first encoder’s input is embedding vectors of the source sentence with position information injected (will be explained in the positional encoding subsection). Each of the other encoders’ inputs is the output of the encoder below.</p>
<p>In summary: An encoder receives input as a list of vectors. Then it processes these vectors by passing them to a self-attention layer, then sends them to a Feedforward Neural Network. Finally, the output goes to the next encoder. The last encoder sends its output to every decoder. The number of Encoders is a hyperparameter.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nusretozates.github.io/posts/transformers/encoder.png" class="img-fluid figure-img"></p>
<figcaption>The word at each position goes through a self-attention process. Then, each result pass through a Feedforward Neural Network (Same DNN but each vector pass through separately) In this example, there are two words but the maximum number of words (that can be given to the model) is a hyper-parameter.</figcaption>
</figure>
</div>
</section>
<section id="decoder" class="level3">
<h3 class="anchored" data-anchor-id="decoder">Decoder</h3>
<p>The structure of the decoder is nearly the same as the encoder. The difference is, decoder layer has one more sub-layer that contains a “masked” multi-head attention layer. When predicting the position “i” we need to be sure about we are attending the known outputs at position &lt; “i”. The Transformer model is auto-regressive, it makes predictions one part at a time, and uses its output to decide what to do next.</p>
<p>During training, we are using teacher-forcing. Teacher forcing is passing the true output to the next time step regardless of what the model predicts at the current time step. As the Transformer predicts each word, self-attention allows it to look at the previous words in the input sequence to better predict the next word. To prevent the model from peeking at the expected output the model uses a look-ahead mask. This is what we call a masked multi-head attention layer. For a target sentence with 4 words, a look-ahead mask look like this:</p>
<pre><code>[[0, 1, 1, 1],
 [0, 0, 1, 1],
 [0, 0, 0, 1],
 [0, 0, 0, 0]]</code></pre>
<p>We are using this matrix as below</p>
<pre><code>scaled_attention_logits -= mask * 1e9</code></pre>
<p>Here, scaled_attention_logits is the normalized result of matrix multiplication of query and key. We make the values from “future” near to zero by appending -1e9 to the values that need to be masked. With that, values from the future will have no impact when calculating attention value.</p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>Why we are using teacher forcing and give the true output to the model?</p>
</div>
</div>
<p>Short answer: Otherwise we would have to run the decoder a number of times equal to the number of words in the target sentence.</p>
<p>Longer answer: The secret lies in the implementation. Let’s say the true output is a three-word sentence and embedding vector dimension is five and our vocabulary size is twenty. Lastly, our batch size is 1 for ease of explanation. After passing these words from the embedding vector, we have a <a href="batch-size, number of words, the dimension of embedding">1,3,5</a> dimensional matrix. After all of the decoders’ calculations, we have a vector that has the same dimension. As we can see in the Transformer model architecture we will pass this vector to a final linear/dense layer. The number of neurons in this layer is twenty because it is our vocabulary size. That means we are multiplying [1,3,5] dimensional matrix with [5,20] dimensional matrix. The result is [1,3,20] dimensional matrix. That means, every word predicted the next word by running the decoder one time. How do we calculate loss? With Sparse Categorical Cross Entropy. This explanation below is the documentation of SparseCategoricalCrossEntropy class in Tensorflow:</p>
<pre><code>Use this crossentropy loss function when there are two or more label classes.
We expect labels to be provided as integers.There should be ‘# classes’ floating point values per feature for ‘y_pred’ and a single floating point value per feature for ‘y_true’.
Example:

y_true = [1, 2]
y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]</code></pre>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nusretozates.github.io/posts/transformers/transformer.png" class="img-fluid figure-img"></p>
<figcaption>The Transformer model architecture</figcaption>
</figure>
</div>
</section>
</section>
<section id="self-attention" class="level2">
<h2 class="anchored" data-anchor-id="self-attention">Self-Attention</h2>
<p>Let’s suppose we want to translate the following sentence:</p>
<p>“The books were on the shelves because they are old.”</p>
<p>What does “they” in this sentence refer to? Is it referring to the books or the shelves? It’s a simple question to a human, but not as simple as an algorithm. Self-attention allows the model to associate “they” with books.</p>
<p>An attention mechanism maps a query and a key-value pair to an output. All of them are vectors. The way to calculate output is the weighted sum of the values. Weights are computed by a function that uses query and key as input. These weights are not trainable, query and key values are calculated by trainable and learned weights.</p>
<p>Query, Key, and Value are created from the encoder’s input vectors(e.g.&nbsp;embedding of input words for the first layer) by multiplying these input vectors with 3 different weight matrices <img src="https://latex.codecogs.com/png.latex?W_Q">, <img src="https://latex.codecogs.com/png.latex?W_K">,<img src="https://latex.codecogs.com/png.latex?W_V"> that can be trained during the training process. Every layer (both encoder and decoder layers) in the Transformer model has its <img src="https://latex.codecogs.com/png.latex?W_%7BQ%7D">, <img src="https://latex.codecogs.com/png.latex?W_%7BK%7D">, <img src="https://latex.codecogs.com/png.latex?W_%7BV%7D"> matrices.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nusretozates.github.io/posts/transformers/qkv.png" class="img-fluid figure-img"></p>
<figcaption>Calculation of Query, Key and Value vectors</figcaption>
</figure>
</div>
<section id="how-do-we-use-query-key-and-value-to-calculate-attention" class="level3">
<h3 class="anchored" data-anchor-id="how-do-we-use-query-key-and-value-to-calculate-attention">How Do We Use Query, Key, and Value to Calculate Attention?</h3>
<p>Let’s suppose we are calculation the self-attention for the word “better”. We will use “Scaled Dot-Product”.</p>
<ol type="1">
<li>We compute dot products of the query with all keys</li>
<li>The results will be divided by <img src="https://latex.codecogs.com/png.latex?sqrt(d_k)"> where <img src="https://latex.codecogs.com/png.latex?d_k"> is the dimension of the key vectors. This is called scaling.</li>
<li>Lastly, we apply the softmax function to the result from step two.</li>
</ol>
<p>This will tell us, how much attention we need to give to each word to encode “better”. For faster processing, Queries are packed in the matrix Q, values are packed in the matrix V and keys are packed in the matrix K. The general scaled dot-product attention formula is:</p>
<p><img src="https://latex.codecogs.com/png.latex?Attention(Q,%20K,%20V)%20=%20softmax(%5C%5Cfrac%7BQK%5ET%7D%7B%5C%5Csqrt%7Bd_k%7D%7D)V"></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nusretozates.github.io/posts/transformers/attention.png" class="img-fluid figure-img"></p>
<figcaption>Scaled Dot-Product Attention</figcaption>
</figure>
</div>
</section>
</section>
<section id="multi-head-attention" class="level2">
<h2 class="anchored" data-anchor-id="multi-head-attention">Multi-Head Attention</h2>
<p>The idea/question behind multi-head self-attention is: <strong>“How do we improve the model’s ability to focus on different features of the input sentence?”.</strong></p>
<ol type="1">
<li>With 1 head self-attention, the encoding could be dominated by the actual word.</li>
<li>Multi-head attention allows the model to learn different semantic meanings of attention. Eg. One for grammar, one for vocabulary, etc.</li>
</ol>
<p>For example, if we have head=8 (referenced as h), there will be 8 different Q, K, and V matrices and 8 different outputs. <img src="https://latex.codecogs.com/png.latex?d_%7Bk%7D%20=%20d_%7Bv%7D%20=%20d_%7Bmodel%7D/h">, weights will have dimension:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?W_%7BQ%7D">: <img src="https://latex.codecogs.com/png.latex?d_%7Bmodel%7D%20x%20d_%7Bk%7D"></li>
<li><img src="https://latex.codecogs.com/png.latex?W_%7BK%7D">: <img src="https://latex.codecogs.com/png.latex?d_%7Bmodel%7D%20x%20d_%7Bk%7D"></li>
<li><img src="https://latex.codecogs.com/png.latex?W_%7BV%7D">: <img src="https://latex.codecogs.com/png.latex?d_%7Bmodel%7D%20x%20d_%7Bv%7D"></li>
</ul>
<p>and the outputs will be concatenated and multiplied by another weight matrix <img src="https://latex.codecogs.com/png.latex?W_%7BO%7D"> with dimension <img src="https://latex.codecogs.com/png.latex?(hd_%7Bv%7D%20x%20d_%7Bmodel%7D)"> to get the final</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nusretozates.github.io/posts/transformers/mha.png" class="img-fluid figure-img"></p>
<figcaption>Multi-Head Attention: We embed each word, create 8 “attention heads”, multiply X with weight matrices and find 8 different Key, Query, and Value matrices, calculate attention vector using these matrices, concatenate these matrices and multiply them with weight matrix <img src="https://latex.codecogs.com/png.latex?W%5EO"> to get the output. This output is the output of the (for example) Multi-Head Attention layer and we will pass this output to the “Add and Normalize” layer.</figcaption>
</figure>
</div>
</section>
<section id="positional-encoding" class="level2">
<h2 class="anchored" data-anchor-id="positional-encoding">Positional Encoding</h2>
<p>As the Transformer model doesn’t use recurrence, there isn’t any position information(time step t) in the model input. It is just a bag of words. To provide the model this important information, we inject(add) some information vector( positional encoding) to each embedding. These positional encoding vectors have the same dimension as embeddings.</p>
<p>One way to add this information is having a second embedding, as we have an embedding vector for “computer”, why shouldn’t we have an embedding for position 3? With this approach, the model can learn positional embeddings during training. The problem with this approach is, say we have 1000 training examples and the maximum input length is 50. If 800 of these training examples have the length of 30, the other 20 embedding vectors will be poorly trained and may not generalize well in practice.</p>
<p>As an alternative, we can use a static function that takes an integer input and give a vector in a way that captures the relationship between positions. This function should give that position 8 is more related to position 9 than position</p>
<ol start="30" type="1">
<li>In the Transformer model, authors decided to use sine and cosine functions of different frequencies:</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?PE_%7B(pos,%202i)%7D%20=%20sin(pos/10000%5E%7B2i/d_%7Bmodel%7D%7D)"> <img src="https://latex.codecogs.com/png.latex?PE_%7B(pos,%202i+1)%7D%20=%20cos(pos/10000%5E%7B2i/d_%7Bmodel%7D%7D)"></p>
<p>Here, <strong>pos</strong> is the position of the token, and <strong>i</strong> is the dimension. Each dimension of these positional encodings corresponds to a sinusoid. (A curve similar to the sine function but possibly shifted in phase, period, amplitude, or any combination thereof.) The wavelengths form a geometric progression from <img src="https://latex.codecogs.com/png.latex?2%5Cpi%20%5C,%20to%20%5C,%2010000%20%5Ccdot%202%5Cpi">. This function is chosen by authors because they believe(hypothesized) this would allow the model to easily learn to attend by relative positions since for any fixed offset k, <img src="https://latex.codecogs.com/png.latex?PE_%7Bpos+k%7D"> can be represented as a linear function of <img src="https://latex.codecogs.com/png.latex?PE_%7Bpos%7D">.</p>
<p>That’s all I know :) Thank you for reading!</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ol type="1">
<li><a href="http://jalammar.github.io/illustrated-transformer/" class="uri">http://jalammar.github.io/illustrated-transformer/</a> - An excellent overview of Transformer models.</li>
<li><a href="https://kazemnejad.com/blog/transformer_architecture_positional_encoding/" class="uri">https://kazemnejad.com/blog/transformer_architecture_positional_encoding/</a> - A detailed explanation of positional encodings.</li>
<li><a href="https://arxiv.org/pdf/1409.0473.pdf" class="uri">https://arxiv.org/pdf/1409.0473.pdf</a> - Neural Machine Translation by Jointly Learning to Align and Translate.</li>
<li><a href="https://arxiv.org/abs/1706.03762v5" class="uri">https://arxiv.org/abs/1706.03762v5</a> - Attention Is All You Need.</li>
</ol>


</section>

 ]]></description>
  <category>ml</category>
  <guid>https://nusretozates.github.io/posts/transformers/</guid>
  <pubDate>Fri, 19 Feb 2021 21:00:00 GMT</pubDate>
  <media:content url="https://nusretozates.github.io/posts/transformers/transformer.png" medium="image" type="image/png" height="182" width="144"/>
</item>
<item>
  <title>Attention mechanism and how it works on neural machine translation</title>
  <dc:creator>Nusret Ozates and Hasan Kemik</dc:creator>
  <link>https://nusretozates.github.io/posts/attention_mechanism/</link>
  <description><![CDATA[ 






<p>In neural machine translation, we aim to find a sentence y that maximizes probability of y given source sentence x. We basically find <img src="https://latex.codecogs.com/png.latex?argmax_y%20%5C,%20p(y%7Cx)">. Before transformers, we were using RNNs with a encoder-decoder approach. An encoder read the inputs and return a fixed-length vector to decoder. The decoder uses this vector as a starting hidden state and outputs a translation from that encoded vector.</p>
<section id="rnn-encoder-decoder" class="level2">
<h2 class="anchored" data-anchor-id="rnn-encoder-decoder">RNN Encoder-Decoder</h2>
<p>Let’s look at the encoder-decoder architecture more formally.</p>
<section id="encoder" class="level3">
<h3 class="anchored" data-anchor-id="encoder">Encoder</h3>
<p>The encoder, reads an input sequence(vector sequence) and “encode” these sequence into a vector c.&nbsp;To calculate c, we calculate hidden states with:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20h_t%20=%20f(h_%7Bt-1%7D,%20x_t)%20"></p>
<p>and vector c is equal to:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20c%20=%20q%5Cleft(%5C%7Bh_1,%20h_2,%20%5Cldots,%20h_%7BT_x%7D%5C%7D%5Cright)%20"></p>
<p><img src="https://latex.codecogs.com/png.latex?h_t%20%5Cin%20R%5En"> is the hidden state at time t, c is a vector, generated from the sequence of hidden states, f and q are some non-linear functions. As an example, f can be an LSTM and <img src="https://latex.codecogs.com/png.latex?q%5Cleft(%5C%7Bh_1,%20h_2,%20%5Cldots,%20h_%7BT_x%7D%5C%7D%5Cright)"> can be equal to <img src="https://latex.codecogs.com/png.latex?h_%7BT_x%7D">, the last hidden state of the encoder.</p>
</section>
<section id="decoder" class="level3">
<h3 class="anchored" data-anchor-id="decoder">Decoder</h3>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>In the article, hidden states of encoder are interpreted as “h” and in the decoder, author choose to use “s” for decoder’s hidden state.</p>
</div>
</div>
<p>The decoder predicts the next word <img src="https://latex.codecogs.com/png.latex?y_t">, given context vector c and all other previously predicted words. Finally, the probability of the translated sentence y is calculated with:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20p(y)%20=%20%5Cprod_%7Bt=1%7D%5E%7BT_y%7D%20p(y_t%20%7C%20y_1,%20...,%20y_%7Bt-1%7D,%20c)%20"></p>
<p>How do we calculate <img src="https://latex.codecogs.com/png.latex?p(y_t%20%7C%20y_1,%20...,%20y_%7Bt-1%7D,%20c)">?</p>
<p><img src="https://latex.codecogs.com/png.latex?%20p(y_t%20%7C%20y_1,%20...,%20y_%7Bt-1%7D,%20c)%20=%20g(y_%7Bt-1%7D,%20s_t,%20c)%20"></p>
<p>where g is a non-linear function that outputs the probability of <img src="https://latex.codecogs.com/png.latex?y_t"> and <img src="https://latex.codecogs.com/png.latex?s_t"> is the hidden state of the RNN.</p>
</section>
</section>
<section id="attention-mechanism-a-new-approach-to-encoder-decoder-structure" class="level2">
<h2 class="anchored" data-anchor-id="attention-mechanism-a-new-approach-to-encoder-decoder-structure">Attention Mechanism: A new approach to Encoder-Decoder Structure</h2>
<section id="encoder-1" class="level3">
<h3 class="anchored" data-anchor-id="encoder-1">Encoder</h3>
<p>In the encoder structure, the hidden states are calculated as Eq.1. Hence, in order to understand the context not only according to previous words but also according to next words, the sentence is traversed 2 times. First one is from beginning to end, second one is from end to beginning. For this operation, Bidirectional RNNs(BiRNNs) are used. This operation creates two different vectors. By concatenating these two vectors, we have our new hidden state vector h, which h contains summarized knowledge of forward and backward words.</p>
<p><img src="https://latex.codecogs.com/png.latex?%20h_j%20=%20%5B%5Coverrightarrow%7Bh_j%5ET%7D;%20%5Coverleftarrow%7Bh_j%5ET%7D%5D%20"></p>
</section>
<section id="decoder-1" class="level3">
<h3 class="anchored" data-anchor-id="decoder-1">Decoder</h3>
<p>In the decoder structure, probability of each word is computed according to previous word’s vector <img src="https://latex.codecogs.com/png.latex?y_%7Bi-1%7D"> , hidden state <img src="https://latex.codecogs.com/png.latex?s_i"> and context vector <img src="https://latex.codecogs.com/png.latex?c_i">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%20p(y_i%20%7C%20y_1,%20...,%20y_%7Bi-1%7D,%20x)%20=%20g(y_%7Bi-1%7D,%20s_i,%20c_i)%20"></p>
<p>Hidden state is computed according to previous hidden state <img src="https://latex.codecogs.com/png.latex?s_%7Bi-1%7D">, previous word’s vector <img src="https://latex.codecogs.com/png.latex?y_%7Bi-1%7D"> and current context vector <img src="https://latex.codecogs.com/png.latex?c_i">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%20s_i%20=%20f(s_%7Bi-1%7D,%20y_%7Bi-1%7D,%20c_i)%20"></p>
<p>The context vector <img src="https://latex.codecogs.com/png.latex?c_i"> depends on all hidden states with their weights. These weights represent the amount of “attention” needed to be given to predict next word <img src="https://latex.codecogs.com/png.latex?y_i">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%20c_i%20=%20%5Csum_%7Bj=1%7D%5E%7BT_x%7D%20%5Calpha_%7Bij%7D%20h_j%20"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Calpha_%7Bij%7D"> is the weight for hidden state <img src="https://latex.codecogs.com/png.latex?h_j"> when predicting word <img src="https://latex.codecogs.com/png.latex?y_i">. The “attention amount” calculated by the formula:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20%5Calpha_%7Bij%7D%20=%20%5Cfrac%7Bexp(e_%7Bij%7D)%7D%7B%5Csum_%7Bk=1%7D%5E%7BT_x%7D%20exp(e_%7Bik%7D)%7D%20"></p>
<p><img src="https://latex.codecogs.com/png.latex?e(i,j)"> represents “energy”, the importance of hidden state <img src="https://latex.codecogs.com/png.latex?h_j">, respect to the previous hidden state <img src="https://latex.codecogs.com/png.latex?s_%7Bi-1%7D">. It is calculated by concatenation of <img src="https://latex.codecogs.com/png.latex?h_j"> and <img src="https://latex.codecogs.com/png.latex?s_%7Bi-1%7D"> is fed through a feedforward neural network a, which allows to train an alignment model through backward propagation.</p>
<p><img src="https://latex.codecogs.com/png.latex?%20e_%7Bij%7D%20=%20a(s_%7Bi-1%7D,%20h_j)%20"></p>
<p>And with this approach, a new era of NLP has started. This attention mechanism become a basic building block of the famous transformer models.</p>
</section>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><a href="https://arxiv.org/abs/1409.0473">Neural Machine Translation by Jointly Learning to Align and Translate</a></li>
</ul>


</section>

 ]]></description>
  <category>ml</category>
  <guid>https://nusretozates.github.io/posts/attention_mechanism/</guid>
  <pubDate>Sat, 12 Dec 2020 21:00:00 GMT</pubDate>
  <media:content url="https://nusretozates.github.io/posts/attention_mechanism/img.png" medium="image" type="image/png" height="56" width="144"/>
</item>
<item>
  <title>Things you need to know about Docker to get started</title>
  <dc:creator>Nusret Ozates</dc:creator>
  <link>https://nusretozates.github.io/posts/docker-get-started/</link>
  <description><![CDATA[ 






<p>Some useful commands and concepts to use Docker!</p>
<p>The original video that I take notes from :</p>
<div class="quarto-video ratio ratio-16x9"><iframe data-external="1" src="https://www.youtube.com/embed/fqMOX6JJhGo" title="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
<p>In this article, I‘ll talk about Docker. We will begin from why we need to use it, to how do we manage multiple Docker containers at the same time.</p>
<section id="why-do-we-need-docker" class="level2">
<h2 class="anchored" data-anchor-id="why-do-we-need-docker">Why do we need Docker?</h2>
<p>We have web servers, database services, messaging services, etc. and all of them have their dependencies(libraries, OS version, etc.) and there can be a conflict between them. We call it “The matrix from Hell”.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nusretozates.github.io/posts/docker-get-started/matrix_hell.webp" class="img-fluid figure-img"></p>
<figcaption>The matrix from Hell</figcaption>
</figure>
</div>
</section>
<section id="what-does-docker-do" class="level2">
<h2 class="anchored" data-anchor-id="what-does-docker-do">What does docker do?</h2>
<p>Run each component in a separate, isolated environment with its dependencies and its libraries. All within the same VM or host.</p>
</section>
<section id="what-are-the-differences-with-vm" class="level2">
<h2 class="anchored" data-anchor-id="what-are-the-differences-with-vm">What are the differences with VM?</h2>
<p>VMs are complete isolation! They have their hardware, kernel, and OS. But docker containers use the same hardware and same Linux kernel.</p>
<blockquote class="blockquote">
<p>That is the reason why you can’t have a Windows container. You can say: “Hey! I have a docker on windows!”. Then I say, look for WSL. 😄</p>
Containers meant to run a specific task or process, not meant to host an OS.
</blockquote>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://nusretozates.github.io/posts/docker-get-started/vm_vs_docker.webp" class="img-fluid figure-img"></p>
<figcaption>Virtual Machines vs Containers</figcaption>
</figure>
</div>
</section>
<section id="some-useful-docker-commands" class="level2">
<h2 class="anchored" data-anchor-id="some-useful-docker-commands">Some Useful Docker Commands</h2>
<ol type="1">
<li>docker version: It gives the docker version.</li>
<li>docker run: It is used to run a container from an image
<ul>
<li>docker run nginx ⇒ Runs instance of the Nginx application on the docker host</li>
<li>docker run -d nginx ⇒ Runs in the detached mode. That means the container will run in the background, and you can continue to use the terminal</li>
<li>docker run — name webapp nginx ⇒ Run a container with the given name</li>
<li>docker run -it nginx ⇒ “-i” gives stdin to docker, you can get input from the terminal. “-t” gives terminal so your dockerized app can print something</li>
<li>docker run -v /opt/datadir:/var/lib/mysql ….. ⇒ The container maps /var/lib/mysql(in docker) to /opt/datadir(in your pc). Your data will persist even when you delete the container.</li>
<li>docker run -p 80:5000 nginx ⇒ Forward your port 80 to container’s port 5000.</li>
</ul></li>
</ol>
<p>Note: You can’t bind the same host port to the multiple docker instances.</p>
<p><img src="https://nusretozates.github.io/posts/docker-get-started/port_bind.webp" class="img-fluid"></p>
<ol start="3" type="1">
<li><p>docker ps: List all running containers and several key information about them. If used with the “-a” parameter, you can see previously stopped or exited containers.</p></li>
<li><p>docker stop: It stops the running containers. Needs container ID or name.</p>
<ul>
<li>docker stop silly_sammet</li>
</ul></li>
<li><p>docker rm: Removes stopped or exited container permanently. If it prints the name back, we are good.</p>
<ul>
<li>docker rm silly_sammet</li>
</ul></li>
<li><p>docker images: Gives a list of downloaded images and their sizes.</p></li>
<li><p>docker rmi: Removes the given image. You need to remove all dependent containers before.</p>
<ul>
<li>docker rmi nginx</li>
</ul></li>
<li><p>docker pull: Just downloads the images so you won’t wait when you want to run the image.</p></li>
<li><p>docker exec: Execute a command in the container.</p>
<ul>
<li>docker exec distracted_meclintock(container name) cat /etc/host(command)</li>
</ul></li>
<li><p>docker inspect: It returns all details of the container in JSON format.</p>
<ul>
<li>docker inspect webapp</li>
</ul></li>
<li><p>docker logs: This shows the logs of a container. It is useful when your container runs in detached mode</p></li>
</ol>
</section>
<section id="tags" class="level2">
<h2 class="anchored" data-anchor-id="tags">Tags</h2>
<p>For example “docker redis” command will run the latest Redis version for you. What if you want to use an older version of Redis?</p>
<p>docker run redis:4.0 bold part is the Tag of a container.</p>
<p>Where can I find tags of the docker image?</p>
<p><a href="https://hub.docker.com" class="uri">https://hub.docker.com</a></p>
<p>Environment Variables</p>
<p>In python, we access an environment variable like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb1-1">os.environ.get(‘APP_COLOR’)</span></code></pre></div></div>
<p>How can you set it in docker?</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> APP_COLOR=pink web-app</span></code></pre></div></div>
<p>How to create my own image?</p>
<p>Let’s say we have a webserver to run on an Ubuntu OS, what would be our steps to run it?</p>
<ol type="1">
<li>OS — Ubuntu</li>
<li>Update apt repo</li>
<li>Install dependencies using apt</li>
<li>Install Python dependencies using pip</li>
<li>Copy source code to ex. /opt folder</li>
<li>Run web server using ex. “flask” command</li>
</ol>
<p>Then we need to do these steps in a file called Dockerfile.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> Ubuntu  </span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt-get</span> update </span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt-get</span> install python  </span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install flask </span>
<span id="cb3-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install flask-mysql  </span>
<span id="cb3-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COPY</span> . /opt/source-code  </span>
<span id="cb3-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENTRYPOINT</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FLASK_APP</span> = /opt/source-code/app.py flask run</span></code></pre></div></div>
<p>Let’s build our Dockerfile and have a docker image!</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> build <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-t</span> nusret/chatbot <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Address of the dockerfile without double quote"</span></span></code></pre></div></div>
<p>And push it to the DockerHub if you want</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> push nusret/chatbot</span></code></pre></div></div>
</section>
<section id="what-is-this-dockerfile" class="level2">
<h2 class="anchored" data-anchor-id="what-is-this-dockerfile">What is this Dockerfile?</h2>
<p><img src="https://nusretozates.github.io/posts/docker-get-started/dockerfile1.webp" class="img-fluid"></p>
<p>Dockerfile is a text file written in a specific format that docker can understand.</p>
<p><img src="https://nusretozates.github.io/posts/docker-get-started/dockerfile2.webp" class="img-fluid"></p>
</section>
<section id="how-can-i-exportimport-my-docker-image-as-a-tar-file" class="level2">
<h2 class="anchored" data-anchor-id="how-can-i-exportimport-my-docker-image-as-a-tar-file">How can I export/import my docker image as a tar file?</h2>
<p>You can export your Docker Image as a .tar file with this command:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> save —output chatbot.tar nusret/chatbot</span></code></pre></div></div>
<p>And you can easily import it with a very similar command.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> load —input chatbot.tar</span></code></pre></div></div>
</section>
<section id="entrypoint-vs-cmd" class="level2">
<h2 class="anchored" data-anchor-id="entrypoint-vs-cmd">ENTRYPOINT VS CMD</h2>
<p>Let’s say we have a docker container that just “sleeps” named “sleeper”. The docker file would be like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> Ubuntu  </span>
<span id="cb8-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CMD</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sleep"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</span>]</span></code></pre></div></div>
<p>When I run the command:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run sleeper sleep 10</span></code></pre></div></div>
<p>This CMD command will get replaced with sleep 10. But as this is a sleeper container, I could only say “10” and the container must sleep. To do this we change the dockerfile like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> Ubuntu  </span>
<span id="cb10-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENTRYPOINT</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sleep"</span>]</span></code></pre></div></div>
<p>This time when I run:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run sleeper 10</span></code></pre></div></div>
<p>The “10” will be appended to the “sleep” command and I can just set the sleep time. But what if I don’t write any number? How can I add a default sleep time?</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb12-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> Ubuntu  </span>
<span id="cb12-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENTRYPOINT</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sleep"</span>]</span>
<span id="cb12-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CMD</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</span>]</span></code></pre></div></div>
</section>
<section id="docker-networking" class="level2">
<h2 class="anchored" data-anchor-id="docker-networking">Docker Networking</h2>
<p><img src="https://nusretozates.github.io/posts/docker-get-started/docker_networking.png" class="img-fluid"></p>
<ol type="1">
<li>Default network a container gets attached to. A bridged network is a private internal network created by docker on the host. All containers are attached to this network and have their internal IP addresses. Containers can access each other by using these internal IPs. If you want to access any of these containers from the outside world, you need to bind/forward the host’s port to the ports on the Docker network</li>
<li>Another way to access these containers is removing the network isolation between the docker host and the docker container by associating the container with the host’s network.</li>
<li>In the “none” network, the container is not attached to any network, and it is not accessible from external networks or any other docker containers.</li>
</ol>
<p>Containers can access each other using their names. Docker creates a DNS server that helps containers using each other’s names.</p>
<p><img src="https://nusretozates.github.io/posts/docker-get-started/docker_networking_2.png" class="img-fluid"></p>
</section>
<section id="docker-compose" class="level2">
<h2 class="anchored" data-anchor-id="docker-compose">Docker Compose</h2>
<p><img src="https://nusretozates.github.io/posts/docker-get-started/docker_compose.png" class="img-fluid"></p>
<p>When we have a complex app that runs with multiple containers, we need to write lots of run commands! But we have docker-compose.</p>
<p>With the latest command at the bottom, you can run all of these images and more! We are using a .yaml file to configure docker-compose.</p>
<p>Let’s say we have a sample application like this:</p>
<p><img src="https://nusretozates.github.io/posts/docker-get-started/docker_compose_2.png" class="img-fluid"></p>
<p>What you would do without docker-compose:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>redis redis </span>
<span id="cb13-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>db postgres:9.4 </span>
<span id="cb13-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vote <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> 5000:80 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--link</span> redis:redis voting-app </span>
<span id="cb13-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>result <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> 5001:80 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--link</span> db:db result-app </span>
<span id="cb13-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>worker <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--link</span> db:db <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--link</span> redis:redis worker</span></code></pre></div></div>
<p>With docker-compose, you can write a docker-compose.yaml file like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">redis</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> redis</span></span>
<span id="cb14-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">db</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> postgres:9.4</span></span>
<span id="cb14-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vote</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> voting-app</span></span>
<span id="cb14-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 5000:80</span></span>
<span id="cb14-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">result</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> result-app</span></span>
<span id="cb14-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 5001:80</span></span>
<span id="cb14-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">worker</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> worker  </span></span></code></pre></div></div>
<p>And run them all with a single command:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb15-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker-compose</span> up</span></code></pre></div></div>
<p>What if some of the images are not already built or not in the DockerHub? Like the “voting-app” in our example, we can change the image key with a build key and specify a location of a directory that contains the application code and Dockerfile.</p>
<p>Change this code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vote</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb16-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> voting-app</span></span>
<span id="cb16-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb16-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 5000:80</span></span></code></pre></div></div>
<p>To this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vote</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb17-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">build</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> ./vote</span></span>
<span id="cb17-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb17-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 5000:80</span></span></code></pre></div></div>
</section>
<section id="docker-compose-versions" class="level2">
<h2 class="anchored" data-anchor-id="docker-compose-versions">Docker Compose Versions</h2>
<p>Docker-compose evolved over time and now supports a lot more options than it did in the beginning.</p>
<section id="version-1" class="level3">
<h3 class="anchored" data-anchor-id="version-1">Version 1</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">redis</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb18-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> redis</span></span>
<span id="cb18-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">db</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb18-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> postgres:9.4</span></span>
<span id="cb18-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vote</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb18-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> voting-app</span></span>
<span id="cb18-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb18-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 5000:80</span></span>
<span id="cb18-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">links</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb18-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> redis</span></span></code></pre></div></div>
<p>It has several limitations. For example, if you wanted to deploy containers on a different network other than the default bridge network, there was no way of specifying that in this version of the file. Also, say you have a startup dependency or startup order of some kind. For example, your database container must come up first, and only then should the voting app be started. There was no way you could specify that in this version.</p>
</section>
<section id="version-2" class="level3">
<h3 class="anchored" data-anchor-id="version-2">Version 2</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">version</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">services</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb19-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">redis</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb19-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> redis</span></span>
<span id="cb19-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">db</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb19-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> postgres:9.4</span></span>
<span id="cb19-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vote</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb19-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> voting-app</span></span>
<span id="cb19-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb19-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 5000:80</span></span>
<span id="cb19-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">depends_on</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb19-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> redis</span></span></code></pre></div></div>
<p>Support for these came in version 2. With version 2 and up, the format of the file also changed. You no longer specify your stack information directly as you did before. It is all encapsulated in the services section.</p>
<p>Another difference is with networking. With version 2, docker-compose automatically creates a dedicated bridged network for this application and then attaches all containers to that new network. All containers are then able to communicate with each other using each other’s service name. So you don’t need to use links.</p>
</section>
<section id="version-3" class="level3">
<h3 class="anchored" data-anchor-id="version-3">Version 3</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">version</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb20-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">services</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb20-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">redis</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb20-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> redis</span></span>
<span id="cb20-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">db</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb20-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> postgres:9.4</span></span>
<span id="cb20-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vote</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb20-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> voting-app</span></span>
<span id="cb20-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb20-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 5000:80</span></span></code></pre></div></div>
<p>This is the latest version of today. Version 3 comes with support for the docker swarm. There are some options removed and added. To see details: <a href="https://docs.docker.com/compose/" class="uri">https://docs.docker.com/compose/</a></p>
</section>
</section>
<section id="networking-in-docker-compose" class="level2">
<h2 class="anchored" data-anchor-id="networking-in-docker-compose">Networking in Docker Compose</h2>
<p>Let us say we modify the architecture a little bit to contain the traffic from the different sources in separate networks. For example, we would like to separate the user-generated traffic from the application’s internal traffic. So, we create a front-end network dedicated to the traffic from users and a backend network dedicated to the traffic within the application.</p>
<p><img src="https://nusretozates.github.io/posts/docker-get-started/docker_compose_3.png" class="img-fluid"></p>
<p>This is the .yaml file you need to write:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">version</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3"</span></span>
<span id="cb21-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">services</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">redis</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> redis</span></span>
<span id="cb21-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> back-end</span></span>
<span id="cb21-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">db</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> postgres:9.4</span></span>
<span id="cb21-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> back-end</span></span>
<span id="cb21-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vote</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> voting-app</span></span>
<span id="cb21-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5000:80"</span></span>
<span id="cb21-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> front-end</span></span>
<span id="cb21-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> back-end</span></span>
<span id="cb21-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">result</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> result-app</span></span>
<span id="cb21-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5001:80"</span></span>
<span id="cb21-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> front-end</span></span>
<span id="cb21-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> back-end</span></span>
<span id="cb21-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">worker</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> worker</span></span>
<span id="cb21-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> front-end</span></span>
<span id="cb21-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> back-end</span></span>
<span id="cb21-30"></span>
<span id="cb21-31"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">front-end</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">driver</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> bridge</span></span>
<span id="cb21-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">back-end</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb21-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">driver</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> bridge</span></span></code></pre></div></div>
<p>So, that’s it! Please watch the video from FreeCodeCamp’s channel to get more detailed information. Also, take a look at the <a href="https://www.youtube.com/user/mmumshad">KodeKlaud’s</a> channel!</p>


</section>

 ]]></description>
  <category>devops</category>
  <category>mlops</category>
  <category>software</category>
  <guid>https://nusretozates.github.io/posts/docker-get-started/</guid>
  <pubDate>Sat, 07 Nov 2020 21:00:00 GMT</pubDate>
  <media:content url="https://nusretozates.github.io/posts/docker-get-started/image.png" medium="image" type="image/png" height="96" width="144"/>
</item>
</channel>
</rss>
