我最近开始使用 Keras
,在他们的文档中,显示了几行代码
inp = Input(shape=(2,))
hl_x = Dense(4, activation='tanh', name= 'First_Hidden_Layer_Following_Input' )(inp)
哪里
type(Input)
>> function
type(inp)
>>>tensorflow.python.framework.ops.Tensor
Input
是一个函数,inp
是一个 tensor
类型的变量
这是什么意思以及它是如何工作的?
最佳答案
Dense(....)
返回一个对象,that can be __called__()
,类似于参数化函数:
def print_multiple(k):
"""Returns a function that prints 'k' times whatever you give it."""
return lambda x: print(*(x for _ in range(k)))
print_multiple(6)("Merry")
print_multiple(4)("Christmas")
打印
Merry Merry Merry Merry Merry Merry
Christmas Christmas Christmas Christmas
keras.layers.dense是一个可调用对象 - 所以沿着以下路线:
class PrintMult:
"""Object that prints 'how_often' times whatever you give it."""
def __init__(self, how_often):
self.how_often = how_often
def __call__(self, what_ever):
print(*(what_ever for _ in range(self.how_often)))
PrintMult(5)("Yeeha") # Yeeha Yeeha Yeeha Yeeha Yeeha
关于python - function_f()(variable_v) 的含义和工作方式( () 中后跟变量的函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59420425/