unsupervised_graph, supervised_graph, features = build_computation_graph(input_var, run_parameters)
# Train graph has dropout
reconstruction, prediction = layers.get_output([unsupervised_graph, supervised_graph])
# Test graph has no dropout so deterministic = True
test_reconstruction, test_prediction = layers.get_output([unsupervised_graph, supervised_graph], deterministic=True)
if run_parameters.clip_unsupervised_output is not None:
    reconstruction = T.clip(reconstruction, run_parameters.clip_unsupervised_output[0],
                            run_parameters.clip_unsupervised_output[1])
    test_reconstruction = T.clip(test_reconstruction, run_parameters.clip_unsupervised_output[0],
                                 run_parameters.clip_unsupervised_output[1])

# Get all trainable params
params = layers.get_all_params(unsupervised_graph, trainable=True) + \
         layers.get_all_params(supervised_graph, trainable=True)
# params = layers.get_all_params(supervised_graph)[-2:]
params = utils.unique(params)

# Get regularizable params
regularization_params = layers.get_all_params(unsupervised_graph, regularizable=True) + \
                        layers.get_all_params(supervised_graph, regularizable=True)
regularization_params = utils.unique(regularization_params)

# Creating loss functions
# Train loss has to take into account of labeled image or not
if run_parameters.unsupervised_cost_fun == 'squared_error':
    loss1 = objectives.squared_error(reconstruction, input_var)
elif run_parameters.unsupervised_cost_fun == 'categorical_crossentropy':
    loss1 = objectives.categorical_crossentropy(reconstruction, input_var)
if supervised_cost_fun == 'squared_error':
    loss2 = objectives.squared_error(prediction, target_var) * repeat_col(labeled_var, 10)
elif supervised_cost_fun == 'categorical_crossentropy':
Exemple #2
0
 def get_all_params(self, **kwargs):
     layers = self.get_all_layers()
     params = sum([l.get_params(**kwargs) for l in layers], [])
     return unique(params)
def get_all_params(layer, unwrap_shared=True, **tags):
    """
    Returns a list of Theano shared variables or expressions that
    parameterize the layer.

    This function gathers all parameters of all layers below one or more given
    :class:`Layer` instances, including the layer(s) itself. Its main use is to
    collect all parameters of a network just given the output layer(s).

    By default, all shared variables that participate in the forward pass will
    be returned. The list can optionally be filtered by specifying tags as
    keyword arguments. For example, ``trainable=True`` will only return
    trainable parameters, and ``regularizable=True`` will only return
    parameters that can be regularized (e.g., by L2 decay).

    Parameters
    ----------
    layer : Layer or list
        The :class:`Layer` instance for which to gather all parameters, or a
        list of :class:`Layer` instances.

    unwrap_shared : bool (default: True)
        Affects only parameters that were set to a Theano expression. If
        ``True`` the function returns the shared variables contained in
        the expression, otherwise the Theano expression itself.

    **tags (optional)
        tags can be specified to filter the list. Specifying ``tag1=True``
        will limit the list to parameters that are tagged with ``tag1``.
        Specifying ``tag1=False`` will limit the list to parameters that
        are not tagged with ``tag1``. Commonly used tags are
        ``regularizable`` and ``trainable``.

    Returns
    -------
    params : list
        A list of Theano shared variables or expressions representing
        the parameters.

    Notes
    -----
    If any of the layers' parameters was set to a Theano expression instead
    of a shared variable, `unwrap_shared` controls whether to return the
    shared variables involved in that expression (``unwrap_shared=True``,
    the default), or the expression itself (``unwrap_shared=False``). In
    either case, tag filtering applies to the expressions, considering all
    variables within an expression to be tagged the same.

    Examples
    --------
    Collecting all parameters from a two-layer network:

    >>> from lasagne.layers import InputLayer, DenseLayer
    >>> l_in = InputLayer((100, 20))
    >>> l1 = DenseLayer(l_in, num_units=50)
    >>> l2 = DenseLayer(l1, num_units=30)
    >>> all_params = get_all_params(l2)
    >>> all_params == [l1.W, l1.b, l2.W, l2.b]
    True

    Parameters can be filtered by tags, and parameter expressions are
    unwrapped to return involved shared variables by default:

    >>> from lasagne.utils import floatX
    >>> w1 = theano.shared(floatX(.01 * np.random.randn(50, 30)))
    >>> w2 = theano.shared(floatX(1))
    >>> l2 = DenseLayer(l1, num_units=30, W=theano.tensor.exp(w1) - w2, b=None)
    >>> all_params = get_all_params(l2, regularizable=True)
    >>> all_params == [l1.W, w1, w2]
    True

    When disabling unwrapping, the expression for ``l2.W`` is returned instead:

    >>> all_params = get_all_params(l2, regularizable=True,
    ...                             unwrap_shared=False)
    >>> all_params == [l1.W, l2.W]
    True
    """
    layers = get_all_layers(layer)
    params = chain.from_iterable(
        l.get_params(unwrap_shared=unwrap_shared, **tags) for l in layers)
    return utils.unique(params)
Exemple #4
0
 def get_all_params(self, **kwargs):
     layers = self.get_all_layers()
     params = sum([l.get_params(**kwargs) for l in layers], [])
     return unique(params)
def build_model1(input_var,target_var,regularW=0,params_load=None):
    def inception(network,no_1x1=64, no_3x3r=96, no_3x3=128, no_5x5r=16, no_5x5=32, no_pool=32):
        out1=layers.Conv2DLayer(network,num_filters=no_1x1,filter_size=(1,1),
                               nonlinearity=nonLinear.leaky_rectify,
                               W=init.GlorotUniform(gain='relu'),pad='same'
                               )
        out3=layers.Conv2DLayer(network,num_filters=no_3x3r,filter_size=(1,1),
                               nonlinearity=nonLinear.leaky_rectify,
                               W=init.GlorotUniform(gain='relu'),pad='same'
                               )
        out3=layers.Conv2DLayer(out3,num_filters=no_3x3,filter_size=(3,3),
                               nonlinearity=nonLinear.leaky_rectify,
                               W=init.GlorotUniform(gain='relu'),pad='same'
                               )
        out5=layers.Conv2DLayer(network,num_filters=no_5x5r,filter_size=(1,1),
                               nonlinearity=nonLinear.leaky_rectify,
                               W=init.GlorotUniform(gain='relu'),pad='same'
                               )
        out5=layers.Conv2DLayer(out5,num_filters=no_5x5,filter_size=(5,5),
                               nonlinearity=nonLinear.leaky_rectify,
                               W=init.GlorotUniform(gain='relu'),pad='same'
                               )
        outpool=layers.MaxPool2DLayer(network,3,stride=1,pad=1)
        outpool=layers.Conv2DLayer(outpool,num_filters=no_pool,filter_size=(1,1),
                               nonlinearity=nonLinear.leaky_rectify,
                               W=init.GlorotUniform(gain='relu'),pad='same'
                               )
        return layers.concat([out1,out3,out5,outpool])
    
    network=layers.InputLayer(shape=(None,3,256,256),input_var=input_var)
    # size 256*256
    network=layers.Pool2DLayer(network,pool_size=(2,2),stride=2,pad=0,mode='average_inc_pad')
    #size 128*128
    network=layers.Pool2DLayer(network,pool_size=(2,2),stride=2,pad=0,mode='average_inc_pad')
    #size 64*64
    
    network=inception(network,no_1x1=64,no_3x3r=64,no_3x3=128,
                      no_5x5r=16,no_5x5=32,no_pool=32)
    
    network=layers.MaxPool2DLayer(network,pool_size=(3,3),stride=2)
    network=layers.DropoutLayer(network,p=0.1)
    #size 32*32
    network=inception(network,no_1x1=128,no_3x3r=128,no_3x3=256,
                      no_5x5r=32,no_5x5=64,no_pool=64)
    
    network=layers.MaxPool2DLayer(network,(3,3),stride=(2,2))
    network=layers.DropoutLayer(network,p=0.2)
    #size 16*16
    network1=inception(network,no_1x1=256,no_3x3r=256,no_3x3=512,
                      no_5x5r=64,no_5x5=128,no_pool=128)
    
    network1=layers.MaxPool2DLayer(network1,pool_size=(3,3),stride=2)
    network1=layers.DropoutLayer(network1,p=0.3)
    #size 8*8  
    network1=layers.GlobalPoolLayer(network1)    
    network1=layers.DenseLayer(network1,num_units=1000,
                              nonlinearity=nonLinear.leaky_rectify,
                              W=init.GlorotUniform(gain='relu'))
    network1=layers.DenseLayer(network1,num_units=2,
                              nonlinearity=nonLinear.softmax)
    
    
    #fast ouput
    network2 = layers.GlobalPoolLayer(network)    
    network2=layers.DenseLayer(network2,num_units=1000,
                              nonlinearity=nonLinear.leaky_rectify,
                              W=init.GlorotUniform(gain='relu'))
    network2=layers.DenseLayer(network2,num_units=2,
                              nonlinearity=nonLinear.softmax)
    
    prediction1=layers.get_output(network1)
    prediction2=layers.get_output(network2)
    loss = objectives.categorical_crossentropy(prediction1, target_var)+objectives.categorical_crossentropy(prediction2, target_var)*0.3
    
    loss=loss.mean()
    
    layers1 = layers.get_all_layers(network1)    
    layers2 = layers.get_all_layers(network2)
    
    params = chain.from_iterable(l.get_params(trainable=True) for l in (layers1+layers2))
    
    params = utils.unique(params)
    
    if params_load != None:
        [p.set_value(pval) for (p, pval) in zip(params, params_load)]
    
    return network1,loss,params