We've summarized the relationships between Python packages, modules, classes, methods, functions, and instances. To summarize, I've introduced a TensorFlow 2.0 tutorial as an example.
The target audience for this article is: -I want to check the relationships between Python packages, modules, classes, methods, functions, and instances. ・ I'm new to Python and don't understand the structure of libraries such as TensorFlow.
The Tens of Flow tutorial used in this article
First, import the library
Structure of TensorFlow (general library)
Examples of packages, modules and functions
What are classes, methods and instances?
Examples of classes, methods and instances
In this article, we will use the following TensorFlow 2.0 tutorial as an example. TensorFlow Tutorial The full code is below.
TensorFlow tutorial code
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
To use a library (such as TensorFlow), you need to import the library. Also, before importing the library, it is necessary to install it in advance (pip install etc.), but since there are already many articles on this area, I will omit it. Below is the import part of TensorFlow excerpted from the tutorial.
import part
import tensorflow as tf
What this is doing is that I want to use the program code to import (tensorflow in this case) in the program I execute (the tutorial code shown in Chapter 1), so prepare (execute in advance). It is an image that tells you to take it. Also, although I write as tf here, it is troublesome to type tensorflow every time I use the TensorFlow function, so I just declare that I will abbreviate it as tf from the next time.
Not limited to TensorFlow, software libraries are composed of the following elements. -Package: A code group in which multiple different "modules" are collected is called a "package". -Module: A .py file (python program code) that contains "classes" and "functions". -Class: A group of "methods" is called a "class". -Method: A function defined in a "class". The difference from a normal function will be explained at the bottom of the article. -Function: A summary of some processing. For example, a function that adds 100 times or a function that calculates the volume from the length of three sides of a box. -Instance: The object to be actually processed is given to the class. It is explained in detail below.
If you read only the above, you will not understand it, so please take a look with the figure below. You can see the following two points.
As an example, let's show a function called load_data of TensorFlow in the following figure in a hierarchical structure. The figures on the left and right show the same thing.
As you can see from the (example) in the above figure, load_data is a function in mnist (module) in datasets (module) in keras (module) in tensorflow (package). And in the Python program code, a. (Dot) indicates that you are going down the hierarchy.
So, the code for loading the machine learning dataset below, excerpted from the tutorial, First, assign the module mnist in datasets in keras in tensorflow to the name mnist. In other words, we are creating a new name, mnist, and assigning mnist (module) in tensorflow. In the second line, the mnist created in the first line is added with. (Dot), and a function called load_data () in the lower hierarchy is used. By the way, () means that nothing is given as an argument.
Data load part
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
So the above code has exactly the same meaning as the code below.
Data load part (when mnist declaration is omitted)
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
You can easily check the structure of modules and functions in tensorflow as shown in the above figure on the official website below. * HP is version 2.1. TensorFlow Core v2.1.0 Module: tf.keras.datasets
If you open the tf.keras.datasets page with the link above, you can get the information as shown below. The numbers correspond to the figures below. ➀ tensorflow Hierarchical information of all packages and modules is displayed in a list. ➁ The currently open tf.keras.datasets are shown to be modules. ➂ It shows what modules are in the hierarchy one level below the currently open tf.keras.datasets.
Classes, methods, and instances are inevitable items for learning about Python and for understanding TensorFlow. I feel that packages, modules, and functions are sufficient for the purpose of managing the processing of a program in an easy-to-read manner, but classes exist to realize the idea of object-oriented programming in Python. There are many articles about object-oriented programming, so please have a look there. Here, I will explain only the difference between how to use classes, methods and instances, and functions.
First, the role of a method is almost the same as that of a function, and it does some processing together. The only major difference is that, as shown in the figure below, functions are used alone, while methods are used as a set with a class. In order to use the method, it is necessary to create something like a box from the class to the instance, which stores the target to be processed by the method and the data after processing, as shown in the above figure. Functions, on the other hand, can perform operations directly on ordinary variables.
It looks like this when written in code. It may be difficult to understand the class at first glance, so please check the explanation article of the class.
Create an instance from a class and execute a method
Instance = Class(Class arguments) # クラスからインスタンスを作成しています。Class argumentsが空()Often.
Instance.method(Method arguments) #The method is being processed for the created instance.
Therefore, in order to use the method, it is necessary to create an instance from the class in advance (unlike the function), so the following code will result in an error. This is a big difference from a function. * There are special methods, but I think it's okay if you don't know them at first.
Execute method without creating an instance
Class.method(Method arguments) #An error will occur.
As an example, the following figure shows the structure of the TensorFlow class Sequential and the methods compile, fit, and evaluate in the hierarchy below it. The figures on the left and right show the same thing.
And, in the code of the following data loading part excerpted from the tutorial, Sequential is a class, compile, fit, evaluate is a method, and model is Sequential is an instance. First, create an instance called model using a class called Sequential, and for this instance, use the methods compile (definition of learning method), fit (model learning), and evaluate (model evaluation) in the hierarchy below the class called Sequential. We are processing in order. * By the way, tf.keras.layers.Flatten etc. in the list of arguments of Sequential are classes and are used to define the structure of the machine learning model.
Examples of classes and instances in the TensorFlow tutorial
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
You can check the structure of tensorflow classes and methods and their arguments from the official website below as well as the functions. TensorFlow Core v2.1.0 Class Method: tf.keras.model.Sequential
Using TensorFlow as an example, I tried to organize the library very roughly. I hope this article will help you in studying Python. * If you make a mistake, please let us know in the comments.
TensorFlow TensorFlow Tutorial Python tutorial
Recommended Posts