Both python and keras are beginners. It is a memorandum for myself.
The Keras functional API allows you to use more complex neural networks than the keras Sequential API.
That's where the following writing style comes out.
001.py
import keras
from keras import layers
inputs = layers.Input(shape=(784,))
dense_hoge = layers.Dense(64, activation='relu')
x = dense_hoge(inputs)#this!
dense_hoge is an instance of the keras.layers.core.Dense class, but the instance name is followed by parentheses, and the variable name inputs is written inside.
Moreover, the last two lines are
002.py
x = layers.Dense(64, activation='relu')(inputs)
You can also write. I've never seen the way variable name 1 (variable name 2) is written.
What the hell is this doing and how?
It was said that the \ _ \ _ call \ _ \ _ function defined in the class is processing.
Let's write a simple class.
003.py
class A:
def __init__(self):
print("ClassA __init__")
def __call__(self):
print ("ClassA __call__")
Create a class A object and store it in a.
003.py
a = A()
#ClassA __init__
Try calling instance a with ().
003.py
a()
#ClassA __call__
So, I found that if you add () to the instance name, the process defined by \ _ \ _ call \ _ \ _ will be executed.
What about classes where \ _ \ _ call \ _ \ _ is not defined?
004.py
class B:
def __init__(self):
print("ClassB __init__")
b = B()
#ClassB __init__
b() #I get the following error
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: 'B' object is not callable
The'B'object was displayed as not callable. It is said that callable () can be used to check if it is callable, so I tried it.
004.py
callable(a)
#True
callable(b)
#False
Class B that does not define \ _ \ _ call \ _ \ _ is now False.
Specifying a class instead of an instance resulted in true for both Class A and Class B.
005.py
callable(A)
#True
callable(B)
#True
\ _ \ _ Call \ _ \ _ was that the object could be the return value. So, if \ _ \ _ call \ _ \ _ returns a callable object, can we call the next \ _ \ _ call \ _ \ _ in succession? The following Class C returns an instance of Class A. An instance of Class A is callable, and calling \ _ \ _ call \ _ \ _ should return Class A \ _ \ _ call \ _ \ _.
006.py
class C:
def __init__(self):
print("ClassC __init__")
def __call__(self):
print ("ClassC __call__")
return A()
c = C()
c()()
#ClassC __call__
#ClassA __init__ #of class C__call__Output when instantiating Class A with
#ClassA __call__
.... I was able to call in succession. I don't think you should use it too much as the code will be confusing, but I found it to work continuously.
I found that I could write the variable name (). In python, even a character string used as a function name could be used as a variable name. For example, the variable name is print. If print () is used, will it be called \ _ \ _ call \ _ \ _? Or is it a normal print function?
007.py
print = A()
print('123') #The following error display
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: __call__() takes 1 positional argument but 2 were given
\ _ \ _ Call \ _ \ _ was called.
I found that I could write the variable name ().
What is the difference between defining and using \ _ \ _ call \ _ \ _ in a class and defining and using the function hoge ()? What is the difference between a variable name (argument) and a variable name.hoge (argument)?
As a disadvantage of how to write a variable (),
There are two possible points. Is there any merit in using \ _ \ _ call \ _ \ _?
Methods like \ _ \ _ call \ _ \ _ seem to be called special methods. It seems that it can be used in various ways, but it seems that there are many explanation pages, so I would like to end here.
Recommended Posts