Python keras.models.clone_model() Examples

The following are 7 code examples for showing how to use keras.models.clone_model(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module keras.models , or try the search function .

Example 1
Project: mpi_learn   Author: vlimant   File: utils.py    License: GNU General Public License v3.0 6 votes vote down vote up
def load_model(filename=None, model=None, weights_file=None, custom_objects={}):
    """Loads model architecture from JSON and instantiates the model.
        filename: path to JSON file specifying model architecture
        model:    (or) a Keras model to be cloned
        weights_file: path to HDF5 file containing model weights
	custom_objects: A Dictionary of custom classes used in the model keyed by name"""
    import_keras()
    from keras.models import model_from_json, clone_model
    if filename is not None:
        with open( filename ) as arch_f:
            json_str = arch_f.readline()
            new_model = model_from_json( json_str, custom_objects=custom_objects) 
    if model is not None:
        new_model = clone_model(model)
    if weights_file is not None:
        new_model.load_weights( weights_file )
    return new_model 
Example 2
Project: delft   Author: kermitt2   File: models.py    License: Apache License 2.0 5 votes vote down vote up
def clone_model(self):
        model_copy = clone_model(self.model)
        model_copy.set_weights(self.model.get_weights())
        return model_copy 
Example 3
def fit(self,
            latent_dim = 128,
            reg_latent = 0,
            layers = [512, 256, 128, 64],
            reg_layes = [0 ,0, 0, 0],
            learning_rate = 0.001,
            epochs = 30,
            batch_size = 256,
            num_negatives = 4,
            **earlystopping_kwargs):


        self.latent_dim = latent_dim
        self.reg_latent = reg_latent
        self.layers = layers
        self.reg_layes = reg_layes
        self.learning_rate = learning_rate
        self.epochs = epochs
        self.batch_size = batch_size
        self.num_negatives = num_negatives


        self._init_model()

        self._best_model = clone_model(self.model)
        self._best_model.set_weights(self.model.get_weights())

        self._train_with_early_stopping(epochs,
                                        algorithm_name = self.RECOMMENDER_NAME,
                                        **earlystopping_kwargs)

        print("MCRec_RecommenderWrapper: Tranining complete")

        self.model = clone_model(self._best_model)
        self.model.set_weights(self._best_model.get_weights()) 
Example 4
def _update_best_model(self):
        # Keras only clones the structure of the model, not the weights
        self._best_model = clone_model(self.model)
        self._best_model.set_weights(self.model.get_weights()) 
Example 5
def deep_clone_model(source_model):

    destination_model = clone_model(source_model)
    destination_model.set_weights(source_model.get_weights())

    return destination_model 
Example 6
Project: FC-AIDE-Keras   Author: csm9493   File: sigma_est.py    License: MIT License 5 votes vote down vote up
def get_model(self):
        
        model = clone_model(self.model_copy)
        model.load_weights('./weights/sigma_estimation_model.hdf5')
        adam=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0)
        model.compile(loss=fine_tuning_loss, optimizer=adam)
        
        return model 
Example 7
Project: typhon   Author: atmtools   File: qrnn.py    License: MIT License 4 votes vote down vote up
def __init__(self,
                 input_dim,
                 quantiles,
                 depth=3,
                 width=128,
                 activation="relu",
                 ensemble_size=1,
                 **kwargs):
        """
        Create a QRNN model.

        Arguments:

            input_dim(int): The dimension of the measurement space, i.e. the number
                            of elements in a single measurement vector y

            quantiles(np.array): 1D-array containing the quantiles  to estimate of
                                 the posterior distribution. Given as fractions
                                 within the range [0, 1].

            depth(int): The number of hidden layers  in the neural network to
                        use for the regression. Default is 3, i.e. three hidden
                        plus input and output layer.

            width(int): The number of neurons in each hidden layer.

            activation(str): The name of the activation functions to use. Default
                             is "relu", for rectified linear unit. See 
                             `this <https://keras.io/activations>`_ link for
                             available functions.

            **kwargs: Additional keyword arguments are passed to the constructor
                      call `keras.layers.Dense` of the hidden layers, which can
                      for example be used to add regularization. For more info consult
                      `Keras documentation. <https://keras.io/layers/core/#dense>`_
        """
        self.input_dim = input_dim
        self.quantiles = np.array(quantiles)
        self.depth = depth
        self.width = width
        self.activation = activation

        model = Sequential()
        if depth == 0:
            model.add(Dense(input_dim=input_dim,
                            units=len(quantiles),
                            activation=None))
        else:
            model.add(Dense(input_dim=input_dim,
                            units=width,
                            activation=activation))
            for i in range(depth - 2):
                model.add(Dense(units=width,
                                activation=activation,
                                **kwargs))
            model.add(Dense(units=len(quantiles), activation=None))
        self.models = [clone_model(model) for i in range(ensemble_size)]