Пример #1
0
def make_high_dim_model(high_dim_data, request):
    # Disable the redefined-outer-name violation in this function
    # pylint: disable=redefined-outer-name
    data = high_dim_data

    if request.param == 'DenseMPO':
        model = Sequential()
        model.add(
            DenseMPO(data.shape[-1],
                     num_nodes=int(math.log(int(data.shape[-1]), 8)),
                     bond_dim=8,
                     use_bias=True,
                     activation='relu',
                     input_shape=(data.shape[-1], )))
    elif request.param == 'DenseDecomp':
        model = Sequential()
        model.add(
            DenseDecomp(512,
                        decomp_size=128,
                        use_bias=True,
                        activation='relu',
                        input_shape=(data.shape[-1], )))
    elif request.param == 'DenseCondenser':
        model = Sequential()
        model.add(
            DenseCondenser(exp_base=2,
                           num_nodes=3,
                           use_bias=True,
                           activation='relu',
                           input_shape=(data.shape[-1], )))
    elif request.param == 'DenseExpander':
        model = Sequential()
        model.add(
            DenseExpander(exp_base=2,
                          num_nodes=3,
                          use_bias=True,
                          activation='relu',
                          input_shape=(data.shape[-1], )))
    elif request.param == 'DenseEntangler':
        num_legs = 3
        leg_dim = round(data.shape[-1]**(1. / num_legs))
        assert leg_dim**num_legs == data.shape[-1]

        model = Sequential()
        model.add(
            DenseEntangler(leg_dim**num_legs,
                           num_legs=num_legs,
                           num_levels=3,
                           use_bias=True,
                           activation='relu',
                           input_shape=(data.shape[-1], )))

    return data, model
Пример #2
0
def test_decomp_higher_dim_input_output_shape(high_dim_data):
    # pylint: disable=redefined-outer-name
    data = high_dim_data
    output_dim = 256
    decomp_size = 128

    model = Sequential()
    model.add(
        DenseDecomp(output_dim,
                    decomp_size=decomp_size,
                    use_bias=True,
                    activation='relu',
                    input_shape=(data.shape[-1], )))

    actual_output_shape = model(data).shape
    expected_output_shape = model.compute_output_shape(data.shape)

    np.testing.assert_equal(expected_output_shape, actual_output_shape)
Пример #3
0
def test_decomp_num_parameters(dummy_data):
  # Disable the redefined-outer-name violation in this function
  # pylint: disable=redefined-outer-name
  data, _ = dummy_data
  output_dim = 256
  decomp_size = 128

  model = Sequential()
  model.add(
      DenseDecomp(output_dim,
                  decomp_size=decomp_size,
                  use_bias=True,
                  activation='relu',
                  input_shape=(data.shape[1],)))

  # num_params = a_params + b_params + bias_params
  expected_num_parameters = (data.shape[1] * decomp_size) + (
      decomp_size * output_dim) + output_dim

  np.testing.assert_equal(expected_num_parameters, model.count_params())
Пример #4
0
def test_config(make_model):
    # Disable the redefined-outer-name violation in this function
    # pylint: disable=redefined-outer-name
    model = make_model

    expected_num_parameters = model.layers[0].count_params()

    # Serialize model and use config to create new layer
    model_config = model.get_config()
    layer_config = model_config['layers'][0]['config']
    if 'mpo' in model.layers[0].name:
        new_model = DenseMPO.from_config(layer_config)
    elif 'decomp' in model.layers[0].name:
        new_model = DenseDecomp.from_config(layer_config)

    # Build the layer so we can count params below
    new_model.build(layer_config['batch_input_shape'])

    # Check that original layer had same num params as layer built from config
    np.testing.assert_equal(expected_num_parameters, new_model.count_params())