def _testEnsembleLatticesLayerProjection(self, interpolation_type, lattice_sizes, structure, output_dim, is_monotone, parameters, expected_projected_parameters): """Test monotonicity projection of lattice layers.""" with ops.Graph().as_default(): input_tensor = array_ops.zeros([1, len(lattice_sizes)], dtype=dtypes.float32) (_, param_tensors, proj, _) = lattice_layers.ensemble_lattices_layer( input_tensor, structure_indices=structure, lattice_sizes=lattice_sizes, is_monotone=is_monotone, output_dim=output_dim, lattice_initializers=parameters, interpolation_type=interpolation_type) with self.test_session() as sess: sess.run(variables.global_variables_initializer()) # Check initialization. param_tensor_values = sess.run(param_tensors) self.assertEqual(len(param_tensor_values), len(parameters)) for (param_value, expected_value) in zip(param_tensor_values, parameters): self.assertAllClose(param_value, expected_value, atol=1e-4) # Check projection. sess.run(proj) param_tensor_values = sess.run(param_tensors) self.assertEqual(len(param_tensor_values), len(expected_projected_parameters)) for (param_value, expected_value) in zip(param_tensor_values, expected_projected_parameters): self.assertAllClose(param_value, expected_value, atol=1e-4)
def _ensemble_lattices_layer(input_tensor, input_dim, output_dim, interpolation_type, calibration_min, calibration_max, calibration_num_keypoints, num_lattices, lattice_rank, lattice_size, regularizer_amounts, is_monotone): """Creates an ensemble of lattices layer.""" projections = [] structures = [ range(lattice_cnt * lattice_rank, (lattice_cnt + 1) * lattice_rank) for lattice_cnt in range(num_lattices) ] calibrated_input, proj = _calibration_layer(input_tensor, input_dim, calibration_min, calibration_max, calibration_num_keypoints, output_min=0, output_max=lattice_size - 1) if proj: projections += proj lattice_outputs, _, proj, reg = lattice_layers.ensemble_lattices_layer( calibrated_input, [lattice_size] * input_dim, structures, is_monotone=is_monotone, output_dim=output_dim, interpolation_type=interpolation_type, **regularizer_amounts) if proj: projections += proj return lattice_outputs, projections, reg
def testSimplexRegularization(self): lattice_sizes = [2, 3] structure = [[0], [1], [0, 1]] # Construct params. parameters = [] parameters.append([[0.0, 1.0], [1.0, 0.0]]) parameters.append([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0]]) parameters.append([[0.0, 0.1, 1.1, 2.3, 3.1, 4.2], [5.1, 2.11, 1.11, 3.21, -1.02, -2.2]]) output_dim = 2 with tf.Graph().as_default(): input_tensor = tf.compat.v1.placeholder(shape=[None, 2], dtype=tf.float32) init_params = [ tf.constant(param, dtype=tf.float32) for param in parameters ] (_, _, _, regularization) = lattice_layers.ensemble_lattices_layer( input_tensor, lattice_sizes=lattice_sizes, structure_indices=structure, output_dim=output_dim, interpolation_type='simplex', l1_reg=0.1, l2_reg=0.1, l1_torsion_reg=0.1, l2_torsion_reg=0.1, l1_laplacian_reg=[0.1, 0.1], l2_laplacian_reg=[0.1, 0.1], lattice_initializers=init_params) with self.session() as sess: sess.run(tf.compat.v1.global_variables_initializer()) self.assertAlmostEqual(28.114279, sess.run(regularization), delta=1e-5)
def _testEnsembleLatticesLayerEvaluation( self, interpolation_type, lattice_sizes, structure, output_dim, inputs, parameters, expected_outputs_list): """Test evaluation of ensemble lattices layers.""" with ops.Graph().as_default(): input_tensor = array_ops.constant(inputs, dtype=dtypes.float32) init_params = [ array_ops.constant(param, dtype=dtypes.float32) for param in parameters ] (output_tensor_lists, _, _, _) = lattice_layers.ensemble_lattices_layer( input_tensor, lattice_sizes=lattice_sizes, structure_indices=structure, output_dim=output_dim, interpolation_type=interpolation_type, lattice_initializers=init_params) self.assertEqual(len(output_tensor_lists), len(structure)) with self.test_session() as sess: sess.run(variables.global_variables_initializer()) output_values_list = sess.run(output_tensor_lists) self.assertAllClose(output_values_list, expected_outputs_list)
def prediction_builder(self, mode, per_dimension_feature_names, hparams, calibrated): """Construct the prediciton.""" self.check_hparams(hparams, adjusted=True) lattice_sizes = [ hparams.get_feature_param(f, 'final_lattice_size') for f in per_dimension_feature_names ] lattice_monotonic = [(hparams.get_feature_param(f, 'monotonicity') != 0) for f in per_dimension_feature_names] num_lattices = hparams.get_param('num_lattices') lattice_rank = hparams.get_param('lattice_rank') rtl_seed = hparams.get_param('rtl_seed') # Create and save structure if it does not exists. if not file_io.file_exists(self._structure_file): structure = self._create_structure( len(lattice_sizes), num_lattices, lattice_rank, rtl_seed) self._save_structure(structure) structure = self._load_structure() # Check structure is what we expect. if len(structure) != num_lattices: raise ValueError( 'Expect %d number of lattices, but found %d number of lattices in ' 'structure: %s' % (num_lattices, len(structure), str(structure))) for each_lattice in structure: if len(each_lattice) != lattice_rank: raise ValueError( 'Expect %d lattice rank, but found %d in structure: %s' % (lattice_rank, len(each_lattice), str(structure))) lattice_l1_reg = hparams.get_param('lattice_l1_reg') lattice_l2_reg = hparams.get_param('lattice_l2_reg') lattice_l1_torsion_reg = hparams.get_param('lattice_l1_torsion_reg') lattice_l2_torsion_reg = hparams.get_param('lattice_l2_torsion_reg') lattice_l1_laplacian_reg = [ hparams.get_feature_param(f, 'lattice_l1_laplacian_reg') for f in per_dimension_feature_names ] lattice_l2_laplacian_reg = [ hparams.get_feature_param(f, 'lattice_l2_laplacian_reg') for f in per_dimension_feature_names ] interpolation_type = hparams.get_param('interpolation_type') packed_results = lattice_layers.ensemble_lattices_layer( calibrated, lattice_sizes, structure, is_monotone=lattice_monotonic, interpolation_type=interpolation_type, lattice_initializers=self.lattice_initializers_fn_, l1_reg=lattice_l1_reg, l2_reg=lattice_l2_reg, l1_torsion_reg=lattice_l1_torsion_reg, l2_torsion_reg=lattice_l2_torsion_reg, l1_laplacian_reg=lattice_l1_laplacian_reg, l2_laplacian_reg=lattice_l2_laplacian_reg) (output_tensors, _, projection_ops, regularization) = packed_results # Take an average of output_tensors and add bias. output_tensor = array_ops.stack( output_tensors, axis=0, name='stacked_output') ensemble_output = math_ops.reduce_mean(output_tensor, axis=0) ensemble_bias_init = hparams.get_param('ensemble_bias') b = variables.Variable([ensemble_bias_init], name='ensemble_bias') prediction = ensemble_output + b # Returns prediction Tensor, projection ops, and regularization. return prediction, projection_ops, regularization
def prediction_builder_from_calibrated( self, mode, per_dimension_feature_names, hparams, calibrated): """Construct the prediciton.""" self.check_hparams(hparams, adjusted=True) lattice_sizes = [ hparams.get_feature_param(f, 'final_lattice_size') for f in per_dimension_feature_names ] lattice_monotonic = [(hparams.get_feature_param(f, 'monotonicity') != 0) for f in per_dimension_feature_names] num_lattices = hparams.get_param('num_lattices') lattice_rank = hparams.get_param('lattice_rank') rtl_seed = hparams.get_param('rtl_seed') interpolation_type = hparams.get_param('interpolation_type') # Create and save structure if it does not exists. if not file_io.file_exists(self._structure_file): structure = self._create_structure( len(lattice_sizes), num_lattices, lattice_rank, rtl_seed) self._save_structure(structure) structure = self._load_structure() # Check structure is what we expect. if len(structure) != num_lattices: raise ValueError( 'Expect %d number of lattices, but found %d number of lattices in ' 'structure: %s' % (num_lattices, len(structure), str(structure))) for each_lattice in structure: if len(each_lattice) != lattice_rank: raise ValueError('Expect %d lattice rank, but found %d in structure: %s' % (lattice_rank, len(each_lattice), str(structure))) # Setup the regularization. regularizer_amounts = {} for reg_name in regularizers.LATTICE_MULTI_DIMENSIONAL_REGULARIZERS: regularizer_amounts[reg_name] = hparams.get_param( 'lattice_{}'.format(reg_name)) for reg_name in regularizers.LATTICE_ONE_DIMENSIONAL_REGULARIZERS: regularizer_amounts[reg_name] = [ hparams.get_feature_param(feature_name, 'lattice_{}'.format(reg_name)) for feature_name in per_dimension_feature_names ] packed_results = lattice_layers.ensemble_lattices_layer( calibrated, lattice_sizes, structure, is_monotone=lattice_monotonic, interpolation_type=interpolation_type, lattice_initializers=self.lattice_initializers_fn_, **regularizer_amounts) (output_tensors, _, projection_ops, regularization) = packed_results # Take an average of output_tensors and add bias. output_tensor = tf.stack( output_tensors, axis=0, name='stacked_output') ensemble_output = tf.reduce_mean(output_tensor, axis=0) ensemble_bias_init = hparams.get_param('ensemble_bias') b = tf.Variable([ensemble_bias_init], name='ensemble_bias') prediction = ensemble_output + b # Returns prediction Tensor, projection ops, and regularization. return prediction, projection_ops, regularization