Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

PREVIOUS NEXT
Code Example
Python :: arcpy select visible raster 
Python :: pss signatures python 
Python :: python gmail 
Python :: how to add twoo segmen time series in a single plot 
Python :: selenium text value is empty in flask returns 
Python :: sphinx, where to write the glossary of a sofware project 
Python :: RuntimeError: cannot open featureclass in python 
Python :: pip upgrade command 
Shell :: remove postgresql ubuntu 
Shell :: remove angular cli 
Shell :: kill all server 5000 mac 
Shell :: remove all docker iamges commandl 
Shell :: npm list global packages 
Shell :: git stash untracked files 
Shell :: check gnome version ubuntu terminal 
Shell :: choco list installed 
Shell :: docker rm all containers 
Shell :: conda install ipywidgets 
Shell :: search for port localhost mac 
Shell :: ubuntu play on linux install 
Shell :: nvm ubuntu 
Shell :: Ultimate Perfomance command 
Shell :: how to uninstall vscode in ubuntu using terminal 
Shell :: remove oh my zsh ubuntu 
Shell :: laravel 8 install composer 
Shell :: how to change java version in linux 
Shell :: check battery health windows 
Shell :: install telnet mac 
Shell :: gcloud get projects 
Shell :: install imagemagick mac 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =