def __init__(self,
                 name,
                 ndim_x,
                 ndim_y,
                 n_centers=10,
                 hidden_sizes=(16, 16),
                 hidden_nonlinearity=tf.nn.tanh,
                 n_training_epochs=1000,
                 x_noise_std=None,
                 y_noise_std=None,
                 adaptive_noise_fn=None,
                 entropy_reg_coef=0.0,
                 weight_decay=0.0,
                 weight_normalization=True,
                 data_normalization=True,
                 dropout=0.0,
                 l2_reg=0.0,
                 l1_reg=0.0,
                 random_seed=None):

        Serializable.quick_init(self, locals())
        self._check_uniqueness_of_scope(name)

        self.name = name
        self.ndim_x = ndim_x
        self.ndim_y = ndim_y

        self.random_seed = random_seed
        self.random_state = np.random.RandomState(seed=random_seed)
        tf.set_random_seed(random_seed)

        self.n_centers = n_centers

        self.hidden_sizes = hidden_sizes
        self.hidden_nonlinearity = hidden_nonlinearity

        self.n_training_epochs = n_training_epochs

        # regularization parameters
        self.x_noise_std = x_noise_std
        self.y_noise_std = y_noise_std
        self.entropy_reg_coef = entropy_reg_coef
        self.adaptive_noise_fn = adaptive_noise_fn
        self.weight_decay = weight_decay
        self.l2_reg = l2_reg
        self.l1_reg = l1_reg
        self.weight_normalization = weight_normalization
        self.data_normalization = data_normalization
        self.dropout = dropout

        self.can_sample = True
        self.has_pdf = True
        self.has_cdf = True

        self.fitted = False

        # build tensorflow model
        self._build_model()
예제 #2
0
    def __init__(self, name, ndim_x, ndim_y, flows_type=None, n_flows=10, hidden_sizes=(16, 16),
                 hidden_nonlinearity=tf.tanh, n_training_epochs=1000, x_noise_std=None, y_noise_std=None, adaptive_noise_fn=None,
                 weight_decay=0.0, weight_normalization=True, data_normalization=True, dropout=0.0, l2_reg=0.0, l1_reg=0.0,
                 random_seed=None):
        Serializable.quick_init(self, locals())
        self._check_uniqueness_of_scope(name)


        self.name = name
        self.ndim_x = ndim_x
        self.ndim_y = ndim_y

        self.random_seed = random_seed
        self.random_state = np.random.RandomState(seed=random_seed)
        tf.set_random_seed(random_seed)

        # charateristics of the flows to be used
        if flows_type is None:
            flows_type = ['affine'] + ['radial' for _ in range(n_flows)]
        assert all([f in FLOWS.keys() for f in flows_type])
        self.flows_type = flows_type

        # specification of the network
        self.hidden_sizes = hidden_sizes
        self.hidden_nonlinearity = hidden_nonlinearity

        self.n_training_epochs = n_training_epochs

        # regularization parameters
        self.x_noise_std = x_noise_std
        self.y_noise_std = y_noise_std
        self.adaptive_noise_fn = adaptive_noise_fn

        # decoupled weight decay
        self.weight_decay = weight_decay

        # l1 / l2 regularization
        self.l2_reg = l2_reg
        self.l1_reg = l1_reg


        # normalizing the network weights
        self.weight_normalization = weight_normalization

        # whether to normalize the data to zero mean, and uniform variance
        self.data_normalization = data_normalization

        # the prob of dropping a node
        self.dropout = dropout

        # gradients for planar flows tend to explode -> clip them by global norm
        self.gradient_clipping = True if 'planar' in flows_type else False

        # as we'll be using reversed flows, sampling is too slow to be useful
        self.can_sample = False
        self.has_pdf = True
        # tf has a cdf implementation only for 1-D Normal Distribution
        self.has_cdf = True if self.ndim_y == 1 else False

        self.fitted = False

        # build tensorflow model
        self._build_model()
    def __init__(self,
                 name,
                 output_dim,
                 hidden_sizes,
                 hidden_nonlinearity,
                 output_nonlinearity,
                 hidden_W_init=L.XavierUniformInitializer(),
                 hidden_b_init=tf.zeros_initializer(),
                 output_W_init=L.XavierUniformInitializer(),
                 output_b_init=tf.zeros_initializer(),
                 input_var=None,
                 input_layer=None,
                 input_shape=None,
                 batch_normalization=False,
                 weight_normalization=False,
                 dropout_ph=None):
        """
        :param dropout_ph: None if no dropout should be used. Else a scalar placeholder that determines the prob of dropping a node.
        Remember to set placeholder to Zero during test / eval
        """

        Serializable.quick_init(self, locals())

        with tf.variable_scope(name):
            if input_layer is None:
                l_in = L.InputLayer(shape=(None, ) + input_shape,
                                    input_var=input_var,
                                    name="input")
            else:
                l_in = input_layer
            self._layers = [l_in]
            l_hid = l_in
            if batch_normalization:
                l_hid = L.batch_norm(l_hid)
            for idx, hidden_size in enumerate(hidden_sizes):
                l_hid = L.DenseLayer(l_hid,
                                     num_units=hidden_size,
                                     nonlinearity=hidden_nonlinearity,
                                     name="hidden_%d" % idx,
                                     W=hidden_W_init,
                                     b=hidden_b_init,
                                     weight_normalization=weight_normalization)
                if dropout_ph is not None:
                    l_hid = L.DropoutLayer(l_hid, dropout_ph, rescale=False)
                if batch_normalization:
                    l_hid = L.batch_norm(l_hid)
                self._layers.append(l_hid)
            l_out = L.DenseLayer(l_hid,
                                 num_units=output_dim,
                                 nonlinearity=output_nonlinearity,
                                 name="output",
                                 W=output_W_init,
                                 b=output_b_init,
                                 weight_normalization=weight_normalization)
            if batch_normalization:
                l_out = L.batch_norm(l_out)
            self._layers.append(l_out)
            self._l_in = l_in
            self._l_out = l_out
            # self._input_var = l_in.input_var
            self._output = L.get_output(l_out)

            LayersPowered.__init__(self, l_out)
예제 #4
0
    def __init__(self,
                 name,
                 ndim_x,
                 ndim_y,
                 center_sampling_method='k_means',
                 n_centers=50,
                 keep_edges=True,
                 init_scales='default',
                 hidden_sizes=(16, 16),
                 hidden_nonlinearity=tf.nn.tanh,
                 train_scales=True,
                 n_training_epochs=1000,
                 x_noise_std=None,
                 y_noise_std=None,
                 adaptive_noise_fn=None,
                 entropy_reg_coef=0.0,
                 weight_decay=0.0,
                 weight_normalization=True,
                 data_normalization=True,
                 dropout=0.0,
                 l2_reg=0.0,
                 l1_reg=0.0,
                 random_seed=None):

        Serializable.quick_init(self, locals())
        self._check_uniqueness_of_scope(name)

        self.name = name
        self.ndim_x = ndim_x
        self.ndim_y = ndim_y

        self.random_seed = random_seed
        self.random_state = np.random.RandomState(seed=random_seed)
        tf.set_random_seed(random_seed)

        self.n_centers = n_centers

        self.hidden_sizes = hidden_sizes
        self.hidden_nonlinearity = hidden_nonlinearity

        self.n_training_epochs = n_training_epochs

        # center sampling parameters
        self.center_sampling_method = center_sampling_method
        self.keep_edges = keep_edges

        # regularization parameters
        self.x_noise_std = x_noise_std
        self.y_noise_std = y_noise_std
        self.adaptive_noise_fn = adaptive_noise_fn
        self.entropy_reg_coef = entropy_reg_coef
        self.weight_decay = weight_decay
        self.l2_reg = l2_reg
        self.l1_reg = l1_reg
        self.weight_normalization = weight_normalization
        self.data_normalization = data_normalization
        self.dropout = dropout

        if init_scales == 'default':
            init_scales = np.array([0.7, 0.3])

        self.n_scales = len(init_scales)
        self.train_scales = train_scales
        self.init_scales = init_scales
        # Transform scales so that the softplus will result in passed init_scales
        self.init_scales_softplus = [
            np.log(np.exp(s) - 1) for s in init_scales
        ]

        self.can_sample = True
        self.has_pdf = True
        self.has_cdf = True

        self.fitted = False

        # build tensorflow model
        self._build_model()