當前位置: 首頁>>代碼示例>>Python>>正文


Python Dense.trainable方法代碼示例

本文整理匯總了Python中keras.layers.Dense.trainable方法的典型用法代碼示例。如果您正苦於以下問題:Python Dense.trainable方法的具體用法?Python Dense.trainable怎麽用?Python Dense.trainable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在keras.layers.Dense的用法示例。


在下文中一共展示了Dense.trainable方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_layer_trainability_switch

# 需要導入模塊: from keras.layers import Dense [as 別名]
# 或者: from keras.layers.Dense import trainable [as 別名]
def test_layer_trainability_switch():
    # with constructor argument, in Sequential
    model = Sequential()
    model.add(Dense(2, trainable=False, input_dim=1))
    assert model.trainable_weights == []

    # by setting the `trainable` argument, in Sequential
    model = Sequential()
    layer = Dense(2, input_dim=1)
    model.add(layer)
    assert model.trainable_weights == layer.trainable_weights
    layer.trainable = False
    assert model.trainable_weights == []

    # with constructor argument, in Model
    x = Input(shape=(1,))
    y = Dense(2, trainable=False)(x)
    model = Model(x, y)
    assert model.trainable_weights == []

    # by setting the `trainable` argument, in Model
    x = Input(shape=(1,))
    layer = Dense(2)
    y = layer(x)
    model = Model(x, y)
    assert model.trainable_weights == layer.trainable_weights
    layer.trainable = False
    assert model.trainable_weights == []
開發者ID:BlakePrice,項目名稱:keras,代碼行數:30,代碼來源:test_dynamic_trainability.py

示例2: Input

# 需要導入模塊: from keras.layers import Dense [as 別名]
# 或者: from keras.layers.Dense import trainable [as 別名]
recog_right=recog
recog_right.add(Dense(64,input_shape=(64,),activation='relu'))
recog_right.add(Lambda(lambda x: x + K.exp(x / 2) * K.random_normal(shape=(1, 64), mean=0.,
                              std=epsilon_std), output_shape=(64,)))
recog_right.add(Highway())
recog_right.add(Activation('sigmoid'))

recog1=Sequential()
recog1.add(Merge([recog_left,recog_right],mode = 'ave'))
recog1.add(Dense(784))

#### HERE***
recog11=Sequential()
layer=Dense(64,init='glorot_uniform',input_shape=(784,))
layer.trainable=False
recog11.add(layer)
layer2=Dense(784, activation='sigmoid',init='glorot_uniform')
layer2.trainable=False
recog11.add(layer2)
recog11.layers[0].W.set_value(np.ones((784,64)).astype(np.float32))

recog11.compile(loss='mean_squared_error', optimizer=sgd,metrics = ['mae'])

recog11.get_weights()[0].shape

gan_input = Input(batch_shape=(1,784))

gan_level2 = recog11(recog1(gan_input))

GAN = Model(gan_input, gan_level2)
開發者ID:kcavagnolo,項目名稱:ml_fun,代碼行數:32,代碼來源:keras_freeze_layer_weights.py

示例3: pop_layer

# 需要導入模塊: from keras.layers import Dense [as 別名]
# 或者: from keras.layers.Dense import trainable [as 別名]
model.add(Dense(10,trainable='False'))
model.add(Activation('softmax',trainable='False'))

# LOADING WEIGHTS TO FINE-TUNNE THEM
model.load_weights(weights_path)

pop_layer(model)
pop_layer(model)

# for layer in model.layers:
#   layer.trainable= False

nb_classes=13

layer_last=Dense(nb_classes)
layer_last.trainable=True

layer_last2=Activation('softmax')
layer_last2.trainable=True

model.add(layer_last)
model.add(layer_last2)

print(model.summary())


# let's train the model using SGD + momentum (how original).
#sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer="sgd",
              metrics=['accuracy'])
開發者ID:telecombcn-dl,項目名稱:dlcv04,代碼行數:33,代碼來源:cifar10_cnn_finetunning.py

示例4: Input

# 需要導入模塊: from keras.layers import Dense [as 別名]
# 或者: from keras.layers.Dense import trainable [as 別名]
recog_right=recog
recog_right.add(Dense(64,input_shape=(64,),activation='relu'))
recog_right.add(Lambda(lambda x: x + K.exp(x / 2) * K.random_normal(shape=(1, 64), mean=0.,
                              std=epsilon_std), output_shape=(64,)))
recog_right.add(Highway())
recog_right.add(Activation('sigmoid'))

recog1=Sequential()
recog1.add(Merge([recog_left,recog_right],mode = 'ave'))
recog1.add(Dense(784))
recog1.add(Activation('relu'))

#### GATE***
recog11=Sequential()
layer=Dense(2,init='glorot_uniform',input_shape=(784,))
layer.trainable=False
recog11.add(layer)
layer2=Dense(784, activation='sigmoid',init='glorot_uniform')
layer2.trainable=True
recog11.add(layer2)
recog11.layers[0].W.set_value(np.ones((784,2)).astype(np.float32))

recog11.compile(loss='mean_squared_error', optimizer=sgd,metrics = ['mae'])

recog11.get_weights()[0].shape

gan_input = Input(batch_shape=(1,784))

gan_level2 = recog11(recog1(gan_input))

GAN = Model(gan_input, gan_level2)
開發者ID:kcavagnolo,項目名稱:ml_fun,代碼行數:33,代碼來源:autoencoder_dimensionality.py


注:本文中的keras.layers.Dense.trainable方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。