コード例 #1
0
  def test_summary(self):

    class ToString:

      def __init__(self):
        self.contents = ''

      def __call__(self, msg):
        self.contents += msg + '\n'

    # Single-io
    model = test_utils.SmallSubclassMLP(
        num_hidden=32, num_classes=4, use_bn=True, use_dp=True)
    model(np.ones((3, 4)))  # need to build model first
    print_fn = ToString()
    model.summary(print_fn=print_fn)
    self.assertIn('Trainable params: 356', print_fn.contents)

    # Multi-io
    model = model_util.get_multi_io_subclass_model(
        num_classes=(5, 6), use_bn=True, use_dp=True)
    model([np.ones((3, 4)), np.ones((3, 4))])  # need to build model first
    print_fn = ToString()
    model.summary(print_fn=print_fn)
    self.assertIn('Trainable params: 587', print_fn.contents)

    # Single-io with unused layer
    model = test_utils.SmallSubclassMLP(
        num_hidden=32, num_classes=4, use_bn=True, use_dp=True)
    model.unused_layer = keras.layers.Dense(10)
    model(np.ones((3, 4)))  # need to build model first
    print_fn = ToString()
    model.summary(print_fn=print_fn)
    self.assertIn('Trainable params: 356', print_fn.contents)
    self.assertIn('0 (unused)', print_fn.contents)
コード例 #2
0
    def test_single_io_dimension_subclass_build(self):
        num_classes = 2
        input_dim = tf.compat.v1.Dimension(50)
        batch_size = tf.compat.v1.Dimension(None)

        model = test_utils.SmallSubclassMLP(
            num_hidden=32, num_classes=num_classes, use_dp=True, use_bn=True
        )

        self.assertFalse(model.built, "Model should not have been built")
        self.assertFalse(
            model.weights,
            ("Model should have no weights since it " "has not been built."),
        )
        model.build(input_shape=(batch_size, input_dim))
        self.assertTrue(
            model.weights,
            (
                "Model should have weights now that it "
                "has been properly built."
            ),
        )
        self.assertTrue(
            model.built, "Model should be built after calling `build`."
        )
        model(tf.ones((32, input_dim)))
コード例 #3
0
 def __init__(self, num_classes=2):
     super().__init__(name="nested_model_1")
     self.num_classes = num_classes
     self.dense1 = keras.layers.Dense(32, activation="relu")
     self.dense2 = keras.layers.Dense(num_classes, activation="relu")
     self.bn = keras.layers.BatchNormalization()
     self.test_net = test_utils.SmallSubclassMLP(
         num_hidden=32, num_classes=4, use_bn=True, use_dp=True
     )
コード例 #4
0
  def test_invalid_input_shape_build(self):
    num_classes = 2
    input_dim = 50

    model = test_utils.SmallSubclassMLP(
        num_hidden=32, num_classes=num_classes, use_dp=True, use_bn=True)

    self.assertFalse(model.built, 'Model should not have been built')
    self.assertFalse(model.weights, ('Model should have no weights since it '
                                     'has not been built.'))
    with self.assertRaisesRegex(ValueError,
                                'input shape is not one of the valid types'):
      model.build(input_shape=tf.compat.v1.Dimension(input_dim))
コード例 #5
0
  def test_single_io_workflow_with_tensors(self):
    num_classes = 2
    num_samples = 10
    input_dim = 50

    with tf.Graph().as_default(), self.cached_session():
      model = test_utils.SmallSubclassMLP(
          num_hidden=32, num_classes=num_classes, use_dp=True, use_bn=True)
      model.compile(loss='mse', optimizer='rmsprop')

      x = tf.ones((num_samples, input_dim))
      y = tf.zeros((num_samples, num_classes))

      model.fit(x, y, epochs=2, steps_per_epoch=10, verbose=0)
      _ = model.evaluate(steps=10, verbose=0)
コード例 #6
0
  def test_single_io_subclass_build(self):
    num_classes = 2
    input_dim = 50
    batch_size = None

    model = test_utils.SmallSubclassMLP(
        num_hidden=32, num_classes=num_classes, use_dp=True, use_bn=True)

    self.assertFalse(model.built, 'Model should not have been built')
    self.assertFalse(model.weights, ('Model should have no weights since it '
                                     'has not been built.'))
    model.build(input_shape=(batch_size, input_dim))
    self.assertTrue(model.weights, ('Model should have weights now that it '
                                    'has been properly built.'))
    self.assertTrue(model.built, 'Model should be built after calling `build`.')
    model(tf.ones((32, input_dim)))
コード例 #7
0
    def test_single_io_workflow_with_np_arrays(self):
        num_classes = 2
        num_samples = 100
        input_dim = 50

        model = test_utils.SmallSubclassMLP(num_hidden=32,
                                            num_classes=num_classes,
                                            use_dp=True,
                                            use_bn=True)
        model.compile(loss='mse',
                      optimizer='rmsprop',
                      metrics=['acc',
                               keras.metrics.CategoricalAccuracy()],
                      run_eagerly=test_utils.should_run_eagerly())

        x = np.ones((num_samples, input_dim))
        y = np.zeros((num_samples, num_classes))

        model.fit(x, y, epochs=2, batch_size=32, verbose=0)
        _ = model.evaluate(x, y, verbose=0)
コード例 #8
0
    def test_single_io_workflow_with_datasets(self):
        num_classes = 2
        num_samples = 10
        input_dim = 50

        with self.cached_session():
            model = test_utils.SmallSubclassMLP(num_hidden=32,
                                                num_classes=num_classes,
                                                use_dp=True,
                                                use_bn=True)
            model.compile(loss='mse',
                          optimizer='rmsprop',
                          run_eagerly=test_utils.should_run_eagerly())

            x = np.ones((num_samples, input_dim), dtype=np.float32)
            y = np.zeros((num_samples, num_classes), dtype=np.float32)
            dataset = tf.data.Dataset.from_tensor_slices((x, y))
            dataset = dataset.repeat(100)
            dataset = dataset.batch(10)

            model.fit(dataset, epochs=2, steps_per_epoch=10, verbose=0)
            _ = model.evaluate(dataset, steps=10, verbose=0)