Knowledge notes needed to understand the Python framework

I will write down what I learned by reading a Python book

I'm studying Python. For the books you read, please refer to the blog post below.

10 recommended books for learning 2020 Python and a memo to Qiita about what you learned

In terms of content, this article focuses on the knowledge needed to understand frameworks written in Python (specifically, deep learning frameworks such as TensorFlow and PyTorch).

The following is a prerequisite.

――We are an eternal beginner. Please be kind --The basics in Python are intended for readers who understand --The # line following the print means the output result. --Comments and editing requests are welcome

For the time being, we plan to add and modify it as needed.

Overall story

Python is all about objects

Everything in Python seems to be an object. I'm object-oriented in an atmosphere, so I don't really understand the meaning.

Even the class seems to be an object, but I'm not sure what that means. I'm not sure even if I read the following.

Visual Guide to Objects and Classes in Python-Everything is an Object

For the time being, I can read and write programs even if I don't understand them, but if I keep this in mind, I feel like I'll be enlightened someday, so I'll write it first.

It's important to read the official documentation

It seems obvious, but I think many people haven't read the official documentation (sorry, I didn't read too much).

I've read some Python books and noticed, but the books are also quite wrong, and some parts are omitted to make it easier for beginners to understand, so it's important to read the documentation. .. There is also Japanese.

Python 3.8.1 documentation

I think it's tough to read everything from corner to corner, but it seems good to take a quick look at PEP8, which describes the coding rules.

PEP 8 -- Style Guide for Python Code

Class relationship

Regarding the special method __hoge__

What is the expression __hoge__ that you often see? Those who say, it seems to be happy if you read the following article.

Even Python beginners want to use __hoge__! ~ How to use special attributes and special methods ~

Use an instance like a function in the __call__ method

You can write as follows.

class Function:
    def __call__(self, x):
        return 2*x

f = Function()
print(f(2))
# 4

that? Is this a function? In the method? If you think, you may want to check __call__.

This is an image that is used quite a bit in deep learning frameworks.

data structure

Lists, tuples, dictionaries, sets

The following four are used when dealing with multiple data in Python.

--List: [] --Tuple: () --Set: {} or set () --Dictionary: {}

When writing a simple program by yourself, it is usually enough to use lists, but in deep learning frameworks, things other than lists are often used. Specifically, dictionaries are used to correspond the labels of teacher data with files.

When you read the code or use it yourself, you need to understand how these are different. Also, as I will explain later, please note that the tuple is actually a comma, not a parenthesis.

Reference: [Python] Differences between lists, tuples, dictionaries, and sets

Commas are used to make tuples (parentheses can be omitted, but commas cannot be omitted)

A tuple with one element should be written as follows:

tuple = (1, )
print(tuple)
# (1,)

On the contrary, tuples are OK with just commas as shown below.

tuple = 1,
print(tuple)
# (1,)

In the introductory book, the list is often just [] and the tuple is (), so in fact it was a shock to make a tuple with a comma (,). ..

Also, Python has a method called unpack assignment, which expands and assigns elements to multiple variables (in the code below, ʻa, b = b, a`), but this is actually inside Python. It is treated as a tuple.

a = 1
b = 2
a, b = b, a
print(a,b)
# 2 1

I was surprised because I used this without being aware of it at all.

Reference: Tuples with one element in Python require a comma at the end

Comprehension:

For lists, you can write the following, which is called comprehension.

test = [i for i in range(5)]
print(test)
# [0, 1, 2, 3, 4]

Comprehensions can also be used for sets.

test = {i for i in range(5)}
print(test)
# {0, 1, 2, 3, 4}

You can use ʻif` to take out only the ones that meet the conditions, so it is convenient in various ways.

Iterator generator

Looking only at the specifications, it seems like "what is it used for?", But with a deep learning framework, it is very useful as a loader for datasets (collections of teacher data). ImageDataGenerator for TensorFlow (Keras) and DataLoader for PyTorch.

In deep learning, learning is carried out in batches (small chunks of data), so this iterator mechanism is very useful. Therefore, if you use the data loader without understanding the mechanism of the iterator, you will not understand it well, so it is good to understand the basics.

I thought that the following etc. are easy to understand in the Qiita article.

Python Iterators and Generators

Regarding testing

If you have any problems, you may be able to isolate the problems efficiently if you can use the test program, so it is good to know.

unittest --- Unittest Framework Official Document Note on how to use Python standard unittest What I was addicted to in Python unittest and what I wanted to know earlier

Other

It has nothing to do with the framework, but it's a useful tip when reading and writing Python.

Index loops using enumerate

If you need an index on the number of loops, you can use enumerate to concisely write:

test = ['a', 'b', 'c', 'd', 'e']

for i, x in enumerate(test):
    print(i, x)

# 0 a
# 1 b
# 2 c
# 3 d
# 4 e

It's not a big deal, but it's so popular that it's useful to know when reading or writing code.

Get the integer and decimal parts of a number at the same time with modf

It's useful to know that trying to get an accurate value is a bit annoying.

import math

print(math.modf(1.5))
# (0.5, 1.0)
print(type(math.modf(1.5)))
# <class 'tuple'>

Reference: Get integer and decimal parts of numbers at the same time with Python, math.modf

The meaning of the asterisk * in Python

In C and C ++, the asterisk is a pointer, so I have only unpleasant memories, but Python is completely different, so I'm relieved (?). It seems good to take a look at the following summary (though I think it's okay to look it up in case of trouble).

Reverse asterisk lookup for Python 3.x

You can calculate the matrix product with @

You can calculate the matrix product with @. Please note that it can be used only with Python3.5, NumPy 1.10.0 or later.

import numpy as np

x = np.array([1, 2])
y = np.array([1],[2])
x @ y
# array([5])

The same result can be obtained with np.matmul (x, y). There is also np.dot (x, y), but this seems to change the result when it is an array of 3 dimensions or more (in 2 dimensions, the same result as matmul, @) ..

In machine learning systems, matrix multiplication is often used, so you may see it.

Summary

I've briefly summarized what I learned in Python in terms of knowledge to deepen my understanding of the framework. I'm thinking of making a note so that I don't forget about other themes.

If you want to know more about Python, please refer to the following articles. Also, if you have any other better books, please let us know in the comments or on Twitter.

10 recommended books for learning 2020 Python and a memo to Qiita about what you learned

Recommended Posts

Knowledge notes needed to understand the Python framework
Use the Python framework "cocotb" to test Verilog.
Python framework bottle notes
14 quizzes to understand the surprisingly confusing scope of Python
Try to understand Python self
Python notes to forget soon
Make the display of Python module exceptions easier to understand
The first API to make with python Djnago REST framework
Minimum knowledge to get started with the Python logging module
[Hyperledger Iroha] Notes on how to use the Python SDK
Leave the troublesome processing to Python
In the python command python points to python3.8
How to get the Python version
[Python] How to import the library
[Python] Change the alphabet to numbers
Try to implement and understand the segment tree step by step (python)
[python] A note that started to understand the behavior of matplotlib.pyplot
Introducing the BOT framework Minette for Python
Understand the benefits of the Django Rest Framework
[Python3] Understand the basics of Beautiful Soup
python notes: Modularization: __name__ == How to use'__main__'
Learning notes from the beginning of Python 1
Miscellaneous notes about the Django REST framework
[Python] Understand the content of error messages
Try using the Python web framework Django (1)-From installation to server startup
[Python] Pandas to fully understand in 10 minutes
Python amateurs try to summarize the list ①
[Python] Understand how to use recursive functions
A way to understand Python duck typing
Learning notes from the beginning of Python 2
The road to compiling to Python 3 with Thrift
I tried the Python Tornado Testing Framework
[Python3] Understand the basics of file operations
I searched for the skills needed to become a web engineer in Python
The fastest way for beginners to master Python
Introduction to Python Let's prepare the development environment
Python constants like None (according to the reference)
Try to solve the Python class inheritance problem
[Python] How to change the date format (display format)
Just add the python array to the json data
Let's understand how to pass arguments (Python version)
Specify the Python executable to use with virtualenv
Personal notes to doc Python code in Sphinx
To dynamically replace the next method in python
Say hello to the world with Python with IntelliJ
Try using the Python web framework Tornado Part 1
Draw graphs in Julia ... Leave the graphs to Python
Excel X Python The fastest way to work
The easiest way to use OpenCV with python
The trick to write flatten concisely in python
[Algorithm x Python] How to use the list
How to erase the characters output by Python
Introduction to Python with Atom (on the way)
[Introduction to Algorithm] Find the shortest path [Python3]
A python amateur tries to summarize the list ②
Try using the Python web framework Tornado Part 2
[Python] I will upload the FTP to the FTP server.
Move your hand to understand the chi-square distribution
[Python] Throw a message to the slack channel
An article summarizing the pitfalls addicted to python
I want to display the progress in Python!