Horje
when training= false still dropout Code Example
when training= false still dropout
from keras import layers
from keras import models
from keras import backend as K
import numpy as np

inp = layers.Input(shape=(10,))
out = layers.Dropout(0.5)(inp)

model = models.Model(inp, out)
model.layers[-1].trainable = False  # set dropout layer as non-trainable
model.compile(optimizer='adam', loss='mse') # IMPORTANT: we must always compile model after changing `trainable` attribute

# create a custom backend function so that we can control the learning phase
func = K.function(model.inputs + [K.learning_phase()], model.outputs)

x = np.ones((1,10))
# learning phase = 1, i.e. training mode
print(func([x, 1]))
# the output will be:
[array([[2., 2., 2., 0., 0., 2., 2., 2., 0., 0.]], dtype=float32)]
# as you can see some of the neurons have been dropped

# now set learning phase = 0, i.e test mode
print(func([x, 0]))
# the output will be:
[array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]], dtype=float32)]
# unsurprisingly, no neurons have been dropped in test phase




Python

Related
bash: line 1: templates/addtask.html: No such file or directory in flask app Code Example bash: line 1: templates/addtask.html: No such file or directory in flask app Code Example
python force realod Code Example python force realod Code Example
pandas replace inf with 0 Code Example pandas replace inf with 0 Code Example
how to convert array of arrays into single array with unique values in numpy Code Example how to convert array of arrays into single array with unique values in numpy Code Example
how to kill python process started by excel Code Example how to kill python process started by excel Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7