溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python中語法定義的示例分析

發(fā)布時間:2021-08-12 13:55:06 來源:億速云 閱讀:125 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)python中語法定義的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1. 括號與函數(shù)調(diào)用

def devided_3(x):
   return x/3.

print(a)    #不帶括號調(diào)用的結(jié)果:<function a at 0x139c756a8>
print(a(3)) #帶括號調(diào)用的結(jié)果:1

不帶括號時,調(diào)用的是函數(shù)在內(nèi)存在的首地址; 帶括號時,調(diào)用的是函數(shù)在內(nèi)存區(qū)的代碼塊,輸入?yún)?shù)后執(zhí)行函數(shù)體。

2. 括號與類調(diào)用

class test():
  y = 'this is out of __init__()'
  def __init__(self):
    self.y = 'this is in the __init__()'
 
x = test  # x是類位置的首地址
print(x.y) # 輸出類的內(nèi)容:this is out of __init__()
x = test() # 類的實例化
print(x.y) # 輸出類的屬性:this is in the __init__() ;

3. function(#) (input)

def With_func_rtn(a):
  print("this is func with another func as return")
  print(a)
  def func(b):
    print("this is another function")
    print(b)
  return func
func(2018)(11)
>>> this is func with another func as return
  2018
  this is another function
  11

其實,這種情況最常用在卷積神經(jīng)網(wǎng)絡(luò)中:

def model(input_shape):
  # Define the input placeholder as a tensor with shape input_shape.
  X_input = Input(input_shape)
  # Zero-Padding: pads the border of X_input with zeroes
  X = ZeroPadding2D((3, 3))(X_input)
  # CONV -> BN -> RELU Block applied to X
  X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
  X = BatchNormalization(axis = 3, name = 'bn0')(X)
  X = Activation('relu')(X)
  # MAXPOOL
  X = MaxPooling2D((2, 2), name='max_pool')(X)
  # FLATTEN X (means convert it to a vector) + FULLYCONNECTED
  X = Flatten()(X)
  X = Dense(1, activation='sigmoid', name='fc')(X)
  # Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
  model = Model(inputs = X_input, outputs = X, name='HappyModel')
  return model

感謝各位的閱讀!關(guān)于“python中語法定義的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI