<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Frankkk</title>
    <description>The latest articles on Forem by Frankkk (@frankkk).</description>
    <link>https://forem.com/frankkk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1187718%2Fb8def80c-956c-4a99-8364-01dec23219c4.jpeg</url>
      <title>Forem: Frankkk</title>
      <link>https://forem.com/frankkk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/frankkk"/>
    <language>en</language>
    <item>
      <title>Python Tips- Maximum Value float('inf')</title>
      <dc:creator>Frankkk</dc:creator>
      <pubDate>Fri, 02 Feb 2024 04:39:49 +0000</pubDate>
      <link>https://forem.com/frankkk/python-tips-floatinf-269h</link>
      <guid>https://forem.com/frankkk/python-tips-floatinf-269h</guid>
      <description>&lt;p&gt;In Python, there are two types: 'int' and 'float'. It's important to note that in Python 3, integers (int) have no maximum limit.&lt;br&gt;
Here is a question: &lt;code&gt;what is the maximum value in Python?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For the float, we can use &lt;code&gt;sys.float_info&lt;/code&gt; to check the details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import sys
&amp;gt;&amp;gt;&amp;gt; sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

&amp;gt;&amp;gt;&amp;gt; print(sys.float_info.max)
1.7976931348623157e+308
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;float value range&lt;/code&gt; is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-1.7976931348623157e+308 &amp;lt;= f &amp;lt;= 1.7976931348623157e+308
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, we can declare the max value in float&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = float('inf')
&amp;gt;&amp;gt;&amp;gt; a
inf
&amp;gt;&amp;gt;&amp;gt; import math
&amp;gt;&amp;gt;&amp;gt; math.isinf(a)
True
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; b = math.inf
&amp;gt;&amp;gt;&amp;gt; math.isinf(b)
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can compare 'inf' and float_info.max&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a &amp;gt; sys.float_info.max
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How about the max value of 'int'? It is a 64 bit value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; sys.maxsize
9223372036854775807
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; print(sys.maxsize == 2**63 - 1)
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But &lt;code&gt;sys.maxsize&lt;/code&gt; is not the max int value, you can declare a value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; b = 10**309
&amp;gt;&amp;gt;&amp;gt; type(b)
&amp;lt;class 'int'&amp;gt;
&amp;gt;&amp;gt;&amp;gt; b&amp;gt;sys.float_info.max
True
&amp;gt;&amp;gt;&amp;gt; b&amp;gt;sys.maxsize 
True
&amp;gt;&amp;gt;&amp;gt; b &amp;gt; float('inf')
False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, if you want to know &lt;code&gt;why the Integer (int) has no max limit in Python&lt;/code&gt;, you need read the source code, the int object is with variable size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typedef struct _longobject PyLongObject;
struct _longobject {
  PyObject_VAR_HEAD
  digit ob_digit[1];
};
#define PyObject_VAR_HEAD      PyVarObject ob_base;
typedef struct {
    PyObject ob_base;
    Py_ssize_t ob_size; /* Number of items in variable part */
} PyVarObject;
typedef struct _object {
    _PyObject_HEAD_EXTRA
    Py_ssize_t ob_refcnt;
    struct _typeobject *ob_type;
} PyObject;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1cyyps4kr9caulcg9d0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1cyyps4kr9caulcg9d0o.png" alt="The integer object" width="800" height="371"&gt;&lt;/a&gt; Image is from [5].&lt;/p&gt;

&lt;p&gt;Please refer to [6] to read the source code in C~&lt;/p&gt;

&lt;p&gt;In summary, we can use &lt;code&gt;float('inf')&lt;/code&gt; as the max value in Python. For the float, we max value is &lt;code&gt;sys.float_info.max&lt;/code&gt;.  for the integer, we can use the value &lt;code&gt;sys.maxsize&lt;/code&gt;. But Integer (int) has no max limit in Python 3, from the source code in C language, the &lt;code&gt;int object is defined variable size&lt;/code&gt;, that is why it has no max limit. Actually, the maximum integer value depends on the memory size, you can try~&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;[1.Maximum and minimum float values in Python]&lt;br&gt;
&lt;a href="https://note.nkmk.me/en/python-sys-float-info-max-min/" rel="noopener noreferrer"&gt;https://note.nkmk.me/en/python-sys-float-info-max-min/&lt;/a&gt;&lt;br&gt;
[2.Integer (int) has no max limit in Python 3]&lt;br&gt;
&lt;a href="https://note.nkmk.me/en/python-int-max-value/" rel="noopener noreferrer"&gt;https://note.nkmk.me/en/python-int-max-value/&lt;/a&gt;&lt;br&gt;
[3.numeric-types-int-float-complex]&lt;br&gt;
&lt;a href="https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex" rel="noopener noreferrer"&gt;https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex&lt;/a&gt;&lt;br&gt;
[4.Int object source code ]&lt;br&gt;
&lt;a href="https://fasionchan.com/python-source/builting-object/int/" rel="noopener noreferrer"&gt;https://fasionchan.com/python-source/builting-object/int/&lt;/a&gt;&lt;br&gt;
[5.Int object]&lt;br&gt;
&lt;a href="https://juejin.cn/post/7209612932367597623" rel="noopener noreferrer"&gt;https://juejin.cn/post/7209612932367597623&lt;/a&gt;&lt;br&gt;
[6.Python source code]&lt;br&gt;
&lt;a href="https://github.com/python/cpython/blob/v3.7.0/Objects/longobject.c#L3" rel="noopener noreferrer"&gt;https://github.com/python/cpython/blob/v3.7.0/Objects/longobject.c#L3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>Python Tips: range (Function or Class) in Details</title>
      <dc:creator>Frankkk</dc:creator>
      <pubDate>Sun, 17 Dec 2023 23:50:17 +0000</pubDate>
      <link>https://forem.com/frankkk/python-range-function-3ipc</link>
      <guid>https://forem.com/frankkk/python-range-function-3ipc</guid>
      <description>&lt;p&gt;In Python, when we want to generate sequences of numbers and write a for loop to check the numbers, we may use range(), for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; for i in range(5):
...     print(i)
... 
Output:
0
1
2
3
4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, do you really know the function range()? What does range() function return? Do you know the class range?&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;The range function is within Python Built-in Types.&lt;br&gt;
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops[1]. It includes three arguments:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;range(start, stop[, step])&lt;br&gt;
&lt;strong&gt;start&lt;/strong&gt;&lt;br&gt;
The first value you want to generate (or 0 if the parameter was not supplied)&lt;br&gt;
&lt;strong&gt;stop&lt;/strong&gt;&lt;br&gt;
The last value value you want to generate, it not within the result&lt;br&gt;
&lt;strong&gt;step&lt;/strong&gt;&lt;br&gt;
The value of the step parameter (or 1 if the parameter was not supplied), can be positive(Increase) or negative(Decrease)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s show some examples below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; for i in range(5):
...     print(i)
... 
0
1
2
3
4
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; for i in range(0,5):
...     print(i)
... 
0
1
2
3
4
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; for i in range(0, 5, 2):
...     print(i)
... 
0
2
4
&amp;gt;&amp;gt;&amp;gt; for i in range(5, 0, -1):
...     print(i)
... 
5
4
3
2
1
&amp;gt;&amp;gt;&amp;gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's get into more details about range.&lt;/p&gt;

&lt;h1&gt;
  
  
  What type of return value from range()?
&lt;/h1&gt;

&lt;p&gt;We can use &lt;code&gt;type&lt;/code&gt; to check the return value from range,  it shows &lt;code&gt;&amp;lt;class ‘range’&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; x = range(5)
&amp;gt;&amp;gt;&amp;gt; type(x)
&amp;lt;class 'range'&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In fact, class range in python is a class where derived from class  xrange(), function  range is more efficient than class  range()because it is a built-in function and does not require creating a new object instance[6]. &lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;help range&lt;/code&gt; We can see the source code of range class
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; help(range)
class range(object)
 |  range(stop) -&amp;gt; range object
 |  range(start, stop[, step]) -&amp;gt; range object
 |  
 |  Return an object that produces a sequence of integers from start (inclusive)
 |  to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
 |  start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
 |  These are exactly the valid indices for a list of 4 elements.
 |  When step is given, it specifies the increment (or decrement).
 |  
 |  Methods defined here:
 |  
 |  __bool__(self, /)
 |      self != 0
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self&amp;gt;=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(self, key, /)
 |      Return self[key].
 |  
 |  __gt__(self, value, /)
 |      Return self&amp;gt;value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self&amp;lt;=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self&amp;lt;value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __reduce__(...)
 |      Helper for pickle.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __reversed__(...)
 |      Return a reverse iterator.
 |  
 |  count(...)
 |      rangeobject.count(value) -&amp;gt; integer -- return number of occurrences of value
 |  
 |  index(...)
 |      rangeobject.index(value) -&amp;gt; integer -- return index of value.
 |      Raise ValueError if the value is not present.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  start
 |  
 |  step
 |  
 |  stop
(END)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  We can use &lt;code&gt;dir()&lt;/code&gt; to check the attributes:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; dir(range)
['__bool__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index', 'start', 'step', 'stop']
&amp;gt;&amp;gt;&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we can see it includes &lt;code&gt;__iter__&lt;/code&gt;, which means is an instance of iterable[2].&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Iterable is an object which can be looped over or iterated over with the help of a for loop. Objects like lists, tuples, sets, dictionaries, strings, etc. are called iterables[3].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here, range is iterable, but it is not an Iterator, we can check it below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# code
from collections.abc import Iterable, Iterator
x = range(5)

if isinstance(x, Iterable):
  print(f"{x} is iterable")
else:
  print(f"{x} is not iterable")

if isinstance(x, Iterator):
  print(f"{x} is Iterator")
else:
  print(f"{x} is not Iterator")

... 
... 
range(0, 5) is iterable
range(0, 5) is not Iterator
&amp;gt;&amp;gt;&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The definition of range in Python doc
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Sequence Types
&lt;/h2&gt;

&lt;p&gt;Rather than being a function, range is actually an immutable sequence type, as documented in Ranges and Sequence Types — list, tuple, range [4].&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;class range(stop)&lt;br&gt;
class range(start, stop, step=1)&lt;br&gt;
Rather than being a function, range is actually an immutable sequence type, as documented in Ranges and Sequence Types — list, tuple, range.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Arguments
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;range(start, stop[, step])&lt;br&gt;
The arguments to the range constructor must be integers (either built-in int or any object that implements the &lt;strong&gt;index&lt;/strong&gt;() special method) [5].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Operations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Common sequence operations(totally 12 items)
&lt;/h3&gt;

&lt;p&gt;Common sequence operations can be found here:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://docs.python.org/3/library/stdtypes.html&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Range objects implement the collections.abc.Sequence ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices (see Sequence Types — list, tuple, range) &lt;/p&gt;

&lt;p&gt;The collections.abc.Sequence ABC is provided to make it easier to correctly implement these operations on custom sequence types.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshybclz57xvc333x4c34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshybclz57xvc333x4c34.png" alt=" " width="800" height="477"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; r = range(0, 20, 2)
&amp;gt;&amp;gt;&amp;gt; r
range(0, 20, 2)
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; 11 in r
False
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; 10 in r
True
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; r.index(10)
5
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; r[5]
10
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; r[:5]
range(0, 10, 2)
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; r[-1]
18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Source code of range in C
&lt;/h1&gt;

&lt;p&gt;We can check the range code in C from c code below:&lt;br&gt;
&lt;code&gt;https://github.com/python/cpython/blob/main/Objects/rangeobject.c&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;First, we can know the &lt;strong&gt;rangeobject&lt;/strong&gt;.&lt;br&gt;
We have the start, stop, step pointers, also we have length, which can be used to store the length of the range elements. PyObject_HEAD defines the initial segment of every PyObject.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typedef struct {
    PyObject_HEAD
    PyObject *start;
    PyObject *stop;
    PyObject *step;
    PyObject *length;
} rangeobject;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Workflow when create a range object
&lt;/h2&gt;

&lt;p&gt;1) When reading the code, we can follow the 3 main functions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;range_new-&amp;gt;range_from_array-&amp;gt;make_range_object&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Range_new
&lt;/h3&gt;

&lt;p&gt;It mainly call the &lt;code&gt;range_from_array&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static PyObject *
range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
    if (!_PyArg_NoKeywords("range", kw))
        return NULL;

    return range_from_array(type, _PyTuple_ITEMS(args), PyTuple_GET_SIZE(args));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  range_from_array
&lt;/h3&gt;

&lt;p&gt;Check the arguments(&lt;code&gt;Py_ssize_t num_args&lt;/code&gt;), including start, stop, step,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If invalid arguments, return null, error&lt;/li&gt;
&lt;li&gt;If start is none, use 0,&lt;/li&gt;
&lt;li&gt;If step is none, use default value 1,
&amp;gt;Here, we can see it will check step argument by step = validate_step(step); While, in validate_step, it will return 1 if step is null.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    /* No step specified, use a step of 1. */
    if (!step)
        return PyLong_FromLong(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finally make a object &lt;code&gt;make_range_object&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static PyObject *
range_from_array(PyTypeObject *type, PyObject *const *args, Py_ssize_t num_args)
{
    rangeobject *obj;
    PyObject *start = NULL, *stop = NULL, *step = NULL;

    switch (num_args) {
        case 3:
            step = args[2];
            /* fallthrough */
        case 2:
            /* Convert borrowed refs to owned refs */
            start = PyNumber_Index(args[0]);
            if (!start) {
                return NULL;
            }
            stop = PyNumber_Index(args[1]);
            if (!stop) {
                Py_DECREF(start);
                return NULL;
            }
            step = validate_step(step);  /* Caution, this can clear exceptions */
            if (!step) {
                Py_DECREF(start);
                Py_DECREF(stop);
                return NULL;
            }
            break;
        case 1:
            stop = PyNumber_Index(args[0]);
            if (!stop) {
                return NULL;
            }
            start = _PyLong_GetZero();
            step = _PyLong_GetOne();
            break;
        case 0:
            PyErr_SetString(PyExc_TypeError,
                            "range expected at least 1 argument, got 0");
            return NULL;
        default:
            PyErr_Format(PyExc_TypeError,
                         "range expected at most 3 arguments, got %zd",
                         num_args);
            return NULL;
    }
    obj = make_range_object(type, start, stop, step);
    if (obj != NULL) {
        return (PyObject *) obj;
    }

    /* Failed to create object, release attributes */
    Py_DECREF(start);
    Py_DECREF(stop);
    Py_DECREF(step);
    return NULL;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  make_range_object
&lt;/h3&gt;

&lt;p&gt;Return the rangeobject by PyObject_New()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static rangeobject *
make_range_object(PyTypeObject *type, PyObject *start,
                  PyObject *stop, PyObject *step)
{
    rangeobject *obj = NULL;
    PyObject *length;
    length = compute_range_length(start, stop, step);
    if (length == NULL) {
        return NULL;
    }
    obj = PyObject_New(rangeobject, type);
    if (obj == NULL) {
        Py_DECREF(length);
        return NULL;
    }
    obj-&amp;gt;start = start;
    obj-&amp;gt;stop = stop;
    obj-&amp;gt;step = step;
    obj-&amp;gt;length = length;
    return obj;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Other operation functions in C
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Index
&lt;/h3&gt;

&lt;p&gt;We can check the index from a range:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; x = range(5)
&amp;gt;&amp;gt;&amp;gt; list(x)
[0, 1, 2, 3, 4]
&amp;gt;&amp;gt;&amp;gt; x.index(3)
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the C code, it first checks whether the target value in the range or not. If it contains the target value, it will call the &lt;code&gt;PyNumber_Subtract()&lt;/code&gt; to get the distance from start to calculate the index. &lt;br&gt;
 &lt;code&gt;PyObject *idx = PyNumber_Subtract(ob, r-&amp;gt;start);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The whole function code is here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static PyObject *
range_index(rangeobject *r, PyObject *ob)
{
    int contains;

    if (!PyLong_CheckExact(ob) &amp;amp;&amp;amp; !PyBool_Check(ob)) {
        Py_ssize_t index;
        index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX);
        if (index == -1)
            return NULL;
        return PyLong_FromSsize_t(index);
    }

    contains = range_contains_long(r, ob);
    if (contains == -1)
        return NULL;

    if (contains) {
        PyObject *idx = PyNumber_Subtract(ob, r-&amp;gt;start);
        if (idx == NULL) {
            return NULL;
        }

        if (r-&amp;gt;step == _PyLong_GetOne()) {
            return idx;
        }

        /* idx = (ob - r.start) // r.step */
        PyObject *sidx = PyNumber_FloorDivide(idx, r-&amp;gt;step);
        Py_DECREF(idx);
        return sidx;
    }

    /* object is not in the range */
    PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
    return NULL;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Compare two range objects
&lt;/h3&gt;

&lt;p&gt;We can also compare two range objects, actually, it compare the length of range objects, &lt;code&gt;cmp_result = PyObject_RichCompareBool(r0-&amp;gt;length, r1-&amp;gt;length, Py_EQ);&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; x = range(5)
&amp;gt;&amp;gt;&amp;gt; y = range(5)
&amp;gt;&amp;gt;&amp;gt; x==y
True
&amp;gt;&amp;gt;&amp;gt; id(x)
4551323104
&amp;gt;&amp;gt;&amp;gt; id(y)
4551266592
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion:
&lt;/h1&gt;

&lt;p&gt;We talked about how to use range and the source code behind it in Python and C!  In summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;range() is a built-in function, support 3 arguments: range(start, stop[, step])&lt;/li&gt;
&lt;li&gt;In Python 3, range is a class, which means range() is the constructor function&lt;/li&gt;
&lt;li&gt;It belongs to Sequence Types, it is iterable, not a iterator&lt;/li&gt;
&lt;li&gt;It supports common operations including containment tests, element index lookup, slicing and support for negative indices, just as the operations in &lt;code&gt;list&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Range does not allow any of its parameters to be a float, should be integers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are interested, please check the Python doc and C code in Github! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Life is short, I use Python~&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference:
&lt;/h2&gt;

&lt;p&gt;[1] &lt;a href="https://docs.python.org/3/library/stdtypes.html#range" rel="noopener noreferrer"&gt;https://docs.python.org/3/library/stdtypes.html#range&lt;/a&gt; &lt;br&gt;
[2] &lt;a href="https://docs.python.org/3/glossary.html#term-iterable" rel="noopener noreferrer"&gt;https://docs.python.org/3/glossary.html#term-iterable&lt;/a&gt; &lt;br&gt;
[3] &lt;a href="https://www.analyticsvidhya.com/blog/2021/07/everything-you-should-know-about-iterables-and-iterators-in-python-as-a-data-scientist/" rel="noopener noreferrer"&gt;https://www.analyticsvidhya.com/blog/2021/07/everything-you-should-know-about-iterables-and-iterators-in-python-as-a-data-scientist/&lt;/a&gt; &lt;br&gt;
[4] )&lt;a href="https://docs.python.org/3/library/functions.html#func-range" rel="noopener noreferrer"&gt;https://docs.python.org/3/library/functions.html#func-range&lt;/a&gt; &lt;br&gt;
[5] &lt;a href="https://docs.python.org/3/library/stdtypes.html#typesseq-range" rel="noopener noreferrer"&gt;https://docs.python.org/3/library/stdtypes.html#typesseq-range&lt;/a&gt; &lt;br&gt;
[6] &lt;a href="https://www.copahost.com/blog/python-range/" rel="noopener noreferrer"&gt;https://www.copahost.com/blog/python-range/&lt;/a&gt; &lt;br&gt;
[7] &lt;a href="https://github.com/python/cpython/blob/main/Objects/rangeobject.c" rel="noopener noreferrer"&gt;https://github.com/python/cpython/blob/main/Objects/rangeobject.c&lt;/a&gt; &lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>MLflow Tutorial with Image Recognition Example (Mnist)</title>
      <dc:creator>Frankkk</dc:creator>
      <pubDate>Tue, 31 Oct 2023 03:25:49 +0000</pubDate>
      <link>https://forem.com/frankkk/mlflow-tutorial-with-image-recognition-examplemnist-4jo5</link>
      <guid>https://forem.com/frankkk/mlflow-tutorial-with-image-recognition-examplemnist-4jo5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is MLOps?
&lt;/h3&gt;

&lt;p&gt;MLOps(Machine Learning Operations) is a core function of Machine Learning engineering,  aimed to simplify the deployment, maintenance, monitoring  of machine learning models in production with reliability and efficiency. MLOps needs the collaboration from data scientists, devops engineers, and operation engineers[1].&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fffw7zxvgl8p0jgusfwck.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fffw7zxvgl8p0jgusfwck.jpeg" alt=" " width="800" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fig 1. Process of MLOps [2]&lt;/p&gt;

&lt;h3&gt;
  
  
  What is MLflow?
&lt;/h3&gt;

&lt;p&gt;MLflow is an open source MLOps platform developed by Databricks. It helps machine learning engineers track and manage the models, code, dependencies, as well as deploy the models to the production environment. In other words, MLflow can  simplify the process of building, training, deploying, and monitoring machine-learning models. For more details, please check &lt;a href="https://mlflow.org/docs/latest/what-is-mlflow.html" rel="noopener noreferrer"&gt;https://mlflow.org/docs/latest/what-is-mlflow.html&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd2tqv6c1o0o9q23vcgk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd2tqv6c1o0o9q23vcgk.png" alt=" " width="800" height="321"&gt;&lt;/a&gt;&lt;br&gt;
Fig 2. MLflow Component[3]&lt;/p&gt;
&lt;h3&gt;
  
  
  Why are MLOps and MLflow?
&lt;/h3&gt;

&lt;p&gt;When training neural networks or machine learning models, we often face challenges listed as below, we can manually document model parameters in a text file or back up models for each experiment. This can be cumbersome and hinder collaboration with others. However, with the assistance of MLOps and MLflow, we can effortlessly train, deploy, and track models.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Challenge 1: How to track your model's version when you need to retrain your models?&lt;br&gt;
We need to train our models, and adjust the parameters, environments, dependencies, datasets. It is hard to maintain the models when we need to collaborate with others.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Challenge 2: How to deploy a model and provide services to the public?&lt;br&gt;
After we train a model, we need to publish the models and provide services to others. We need to provide APIs, and maintain the service reliable 7x24.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Challenge 3: How to monitor the models?&lt;br&gt;
Again, we need to track the service, maintain the service.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  MLflow Demo:
&lt;/h2&gt;

&lt;p&gt;In this example, we use an image recognition model based on Keras/TensorFlow, and MNIST dataset. If you are interested, you can follow[4] for the example of sklearn_logistic_regression.&lt;/p&gt;
&lt;h3&gt;
  
  
  Installation &amp;amp; Setup
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Step 1. Install conda&lt;br&gt;
&lt;code&gt;https://docs.conda.io/projects/conda/en/latest/user-guide/install/linux.html&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 2. Create a virtual environment for mflow&lt;br&gt;
&lt;code&gt;cd $your_folder &lt;br&gt;
conda create -n mlflow  (you can custom your environment name)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 3: Activate the conda environment&lt;br&gt;
&lt;code&gt;conda activate mlflow&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 4: Install mlfow&lt;br&gt;
&lt;code&gt;conda install mlflow (or pip install mlflow)&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 5: Run an MLFlow server with a filestore backend, the default port is 5000, you can change it by ‘-p’ option (i.e., mlflow server -p 5111)&lt;br&gt;
&lt;code&gt;mlflow server -h 0.0.0.0 --backend-store-uri /home/xxx/mlruns ( you can custom your backend uri path)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 6: Test the web &lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmm3bnr67kukevjjoj7ya.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmm3bnr67kukevjjoj7ya.jpg" alt=" " width="800" height="463"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Prepare and Run the MLflow Project
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Step1: Clone the MLflow project[5][6]: &lt;br&gt;
&lt;code&gt;git clone https://github.com/RoboticsAndCloud/mlflow-examples.git&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 2: Go to the mlflow-examples - Keras/TensorFlow - MNIST  example:&lt;br&gt;
&lt;code&gt;cd mlflow-examples/python/keras_tf_mnist&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, notice three files: &lt;strong&gt;&lt;em&gt;MLproject&lt;/em&gt;&lt;/strong&gt;,  &lt;strong&gt;&lt;em&gt;conda.yaml&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;train.py&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MLproject: a YAML defines the MLflow project structure, including project name, dependencies(in conda.yaml), parameters and entry point. Check https://mlflow.org/docs/latest/projects.html for more details.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5qb2nx6nykdijvyk2ug.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5qb2nx6nykdijvyk2ug.jpg" alt=" " width="427" height="336"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conda.yaml: a YAML file defines your project’s dependencies

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mxsz2dlfepgr6wszkex.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mxsz2dlfepgr6wszkex.jpg" alt=" " width="476" height="205"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;train.py: A python script which shows how to train your models and log the parameters into MLflow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsw8ojauxebclva0kurdb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsw8ojauxebclva0kurdb.jpg" alt=" " width="625" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Step 3: Set the tracking URI&lt;br&gt;
Here, we can export the environment variable to ensure our results are logged to our MLflow server&lt;br&gt;
&lt;code&gt;export MLfLOW_TRACKING_URI=http://localhost:5000&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 4: Running the MLflow Experiment&lt;br&gt;
&lt;code&gt;mlflow run . --experiment-name=keras_mnist --run-name runname_first_keras_keras_mnist&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 5: Check the MLflow Dashboard &lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotukwlvx8yxx5l28zoz5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotukwlvx8yxx5l28zoz5.jpg" alt=" " width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Model Serving
&lt;/h3&gt;

&lt;p&gt;Model Serving exposes the models, allowing us to access the service through REST API endpoints.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: Get the run ID&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fumxnerq955k2al282mpm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fumxnerq955k2al282mpm.png" alt=" " width="775" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Step 2: Server the model&lt;br&gt;
&lt;code&gt;mlflow models serve -m runs:/5c6a476d67d84239b01f874241f4009f/keras-model --port 5001 &lt;br&gt;
&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 3: Send recognition request and test the service&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Target File Image '0':(You can find some from Mnist dataset)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgdf0rp1ywmawjsu49ei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgdf0rp1ywmawjsu49ei.png" alt=" " width="28" height="28"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Set the tracking URI&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;export MLfLOW_TRACKING_URI=http://localhost:5000 &lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Send request&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 keras_predict.py --model-uri runs:/5c6a476d67d84239b01f874241f4009f/keras-model  --data-path /home/ascc/LF_Workspace/mnist_png/testing/0/9993.png

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Results&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can see ‘0’ is the highest probability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff86rp6mwh019ms6ap1yk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff86rp6mwh019ms6ap1yk.jpg" alt=" " width="800" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Send HTTP request and Results&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Remember, the port should be the same with the model server port, here is 5001&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python convert_png_to_mlflow_json.py /home/ascc/LF_Workspace/mnist_png/testing/0/9993.png | curl -X POST -H "Content-Type:application/json"   -d @-   http://localhost:5001/invocations
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgwmg31dfx8jqg144ztss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgwmg31dfx8jqg144ztss.png" alt=" " width="800" height="67"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Model version control:
&lt;/h3&gt;

&lt;p&gt;Once the model has been verified, you can proceed to register it, verify its version, and deploy it in another production environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Version control&lt;/em&gt;&lt;/strong&gt; is crucial when deploying models in a production environment. It enables you to monitor changes and revert to a specific version if needed. &lt;/p&gt;

&lt;p&gt;The development process can be segmented into multiple stages for model verification. MLflow defines three key stages: &lt;strong&gt;&lt;em&gt;staging, production, and archived&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1niox0hxqkqvqes0agr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1niox0hxqkqvqes0agr.jpg" alt=" " width="800" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors and solution you may meet:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Error 1
&lt;code&gt;ModuleNotFoundError: No module named 'pip._vendor.six'
&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Solution :You need update your pipenv
pip install pip -U
pip install pipenv -U
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Error 2
&lt;code&gt;AttributeError: module 'virtualenv.create.via_global_ref.builtin.cpython.mac_os' has no attribute 'CPython2macOsFramework'&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Solution: Virtualenv installed twice ( apt, pip, delete the python3-virtualenv by apt, sudo apt purge python3-virtualenv)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Error 3
&lt;code&gt;mlflow.exceptions.MlflowException: Run '37011fce0ac847dbaa31efb5fabf842d' not found&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Solution: You miss your tracking URI, 
export MLfLOW_TRACKING_URI=http://localhost:5000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary:
&lt;/h2&gt;

&lt;p&gt;This article demonstrates how to train, track, and manage a model using the MLflow platform. MLflow enables seamless design, deployment, and monitoring of machine learning models. Furthermore, we posit that MLOps can significantly enhance contributions to the field of AI.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference:
&lt;/h2&gt;

&lt;p&gt;[1]MLOps &lt;a href="https://www.databricks.com/glossary/mlops" rel="noopener noreferrer"&gt;https://www.databricks.com/glossary/mlops&lt;/a&gt;&lt;br&gt;
[2]What Is MLOps &lt;a href="https://ml-ops.org/content/mlops-principles" rel="noopener noreferrer"&gt;https://ml-ops.org/content/mlops-principles&lt;/a&gt; &lt;br&gt;
[3]MLflow component &lt;a href="https://www.datacamp.com/tutorial/mlflow-streamline-machine-learning-workflow" rel="noopener noreferrer"&gt;https://www.datacamp.com/tutorial/mlflow-streamline-machine-learning-workflow&lt;/a&gt;&lt;br&gt;
[4]Getting Started With MLflow &lt;a href="https://saturncloud.io/blog/getting-started-with-mlflow/" rel="noopener noreferrer"&gt;https://saturncloud.io/blog/getting-started-with-mlflow/&lt;/a&gt;&lt;br&gt;
[5]MLflow examples &lt;a href="https://github.com/amesar/mlflow-examples" rel="noopener noreferrer"&gt;https://github.com/amesar/mlflow-examples&lt;/a&gt; &lt;br&gt;
[6]MLflow examples &lt;a href="https://github.com/RoboticsAndCloud/mlflow-examples" rel="noopener noreferrer"&gt;https://github.com/RoboticsAndCloud/mlflow-examples&lt;/a&gt; &lt;/p&gt;

</description>
      <category>mlflow</category>
      <category>devops</category>
      <category>deeplearning</category>
      <category>mlops</category>
    </item>
  </channel>
</rss>
