Beispiel #1
0
    def test_trace_multi_io_model_outputs(self):
        input_dim = 5
        num_classes = 3
        num_classes_b = 4
        input_a = keras.layers.Input(shape=(input_dim, ), name='input_a')
        input_b = keras.layers.Input(shape=(input_dim, ), name='input_b')

        dense = keras.layers.Dense(num_classes, name='dense')
        dense2 = keras.layers.Dense(num_classes_b, name='dense2')
        dropout = keras.layers.Dropout(0.5, name='dropout')
        branch_a = [input_a, dense]
        branch_b = [input_b, dense, dense2, dropout]

        model = testing_utils.get_multi_io_model(branch_a, branch_b)

        input_a_ts = tf.constant(
            np.random.random((10, input_dim)).astype(np.float32))
        input_b_ts = tf.constant(
            np.random.random((10, input_dim)).astype(np.float32))

        if testing_utils.get_model_type() == 'subclass':
            with self.assertRaisesRegex(ValueError,
                                        'input shapes have not been set'):
                saving_utils.trace_model_call(model)

        model.compile(optimizer='sgd',
                      loss='mse',
                      run_eagerly=testing_utils.should_run_eagerly())
        model.fit(x=[
            np.random.random((8, input_dim)).astype(np.float32),
            np.random.random((8, input_dim)).astype(np.float32)
        ],
                  y=[
                      np.random.random((8, num_classes)).astype(np.float32),
                      np.random.random((8, num_classes_b)).astype(np.float32)
                  ],
                  epochs=2)

        fn = saving_utils.trace_model_call(model)
        # tf.function requires that the input structures match when calling a
        # ConcreteFunction. For some reason V1 models defines the inputs as a list,
        # while V2 models sets the inputs as a tuple.
        if (not tf.executing_eagerly()
                and testing_utils.get_model_type() != 'functional'):
            signature_outputs = fn([input_a_ts, input_b_ts])
        else:
            signature_outputs = fn((input_a_ts, input_b_ts))
        outputs = model([input_a_ts, input_b_ts])
        if model.output_names:
            expected_outputs = {
                model.output_names[0]: outputs[0],
                model.output_names[1]: outputs[1]
            }
        else:
            expected_outputs = {'output_1': outputs[0], 'output_2': outputs[1]}
        self._assert_all_close(expected_outputs, signature_outputs)
def get_multi_io_temporal_model():
    timesteps = 2
    inp_1 = layers.Input(shape=(1, ), name='input_1')
    inp_2 = layers.Input(shape=(1, ), name='input_2')
    x = layers.RepeatVector(timesteps)
    out_1 = layers.TimeDistributed(Bias(), name='output_1')
    out_2 = layers.TimeDistributed(Bias(), name='output_2')

    branch_a = [inp_1, x, out_1]
    branch_b = [inp_2, x, out_2]
    return testing_utils.get_multi_io_model(branch_a, branch_b)
def get_multi_io_model():
  inp_1 = layers.Input(shape=(1,), name='input_1')
  inp_2 = layers.Input(shape=(1,), name='input_2')
  x = layers.Dense(3, kernel_initializer='ones', trainable=False)
  out_1 = layers.Dense(
      1, kernel_initializer='ones', name='output_1', trainable=False)
  out_2 = layers.Dense(
      1, kernel_initializer='ones', name='output_2', trainable=False)

  branch_a = [inp_1, x, out_1]
  branch_b = [inp_2, x, out_2]
  return testing_utils.get_multi_io_model(branch_a, branch_b)
Beispiel #4
0
    def test_trace_multi_io_model_outputs(self):
        input_dim = 5
        num_classes = 3
        num_classes_b = 4
        input_a = keras.layers.Input(shape=(input_dim, ), name='input_a')
        input_b = keras.layers.Input(shape=(input_dim, ), name='input_b')

        dense = keras.layers.Dense(num_classes, name='dense')
        dense2 = keras.layers.Dense(num_classes_b, name='dense2')
        dropout = keras.layers.Dropout(0.5, name='dropout')
        branch_a = [input_a, dense]
        branch_b = [input_b, dense, dense2, dropout]

        model = testing_utils.get_multi_io_model(branch_a, branch_b)

        input_a_np = np.random.random((10, input_dim)).astype(np.float32)
        input_b_np = np.random.random((10, input_dim)).astype(np.float32)

        if testing_utils.get_model_type() == 'subclass':
            with self.assertRaisesRegex(ValueError,
                                        'input shapes have not been set'):
                saving_utils.trace_model_call(model)

        model.compile(optimizer='sgd',
                      loss='mse',
                      run_eagerly=testing_utils.should_run_eagerly())
        model.fit(x=[
            np.random.random((8, input_dim)).astype(np.float32),
            np.random.random((8, input_dim)).astype(np.float32)
        ],
                  y=[
                      np.random.random((8, num_classes)).astype(np.float32),
                      np.random.random((8, num_classes_b)).astype(np.float32)
                  ],
                  epochs=2)

        fn = saving_utils.trace_model_call(model)
        signature_outputs = fn([input_a_np, input_b_np])
        outputs = model([input_a_np, input_b_np])
        if model.output_names:
            expected_outputs = {
                model.output_names[0]: outputs[0],
                model.output_names[1]: outputs[1]
            }
        else:
            expected_outputs = {'output_1': outputs[0], 'output_2': outputs[1]}
        self._assert_all_close(expected_outputs, signature_outputs)
    def test_training_and_eval_methods_on_multi_input_output_dataset(self):
        input_a = keras.layers.Input(shape=(3, ), name='input_1')
        input_b = keras.layers.Input(shape=(3, ), name='input_2')
        dense = keras.layers.Dense(4, name='dense')
        dropout = keras.layers.Dropout(0.5, name='dropout')
        branch_a = [input_a, dense]
        branch_b = [input_b, dense, dropout]

        model = testing_utils.get_multi_io_model(branch_a, branch_b)
        model.compile(optimizer='rmsprop',
                      loss='mse',
                      run_eagerly=testing_utils.should_run_eagerly())

        input_a_np = np.random.random((10, 3)).astype(dtype=np.float32)
        input_b_np = np.random.random((10, 3)).astype(dtype=np.float32)
        output_d_np = np.random.random((10, 4)).astype(dtype=np.float32)
        output_e_np = np.random.random((10, 4)).astype(dtype=np.float32)

        # Test with tuples
        dataset_tuple = tf.data.Dataset.from_tensor_slices(
            ((input_a_np, input_b_np), (output_d_np, output_e_np)))
        dataset_tuple = dataset_tuple.repeat(100)
        dataset_tuple = dataset_tuple.batch(10)

        model.fit(dataset_tuple, epochs=1, steps_per_epoch=2, verbose=1)
        model.evaluate(dataset_tuple, steps=2, verbose=1)

        # Test with dict
        input_dict = {'input_1': input_a_np, 'input_2': input_b_np}
        if testing_utils.get_model_type() == 'subclass':
            output_dict = {'output_1': output_d_np, 'output_2': output_e_np}
        else:
            output_dict = {'dense': output_d_np, 'dropout': output_e_np}

        dataset_dict = tf.data.Dataset.from_tensor_slices(
            (input_dict, output_dict))
        dataset_dict = dataset_dict.repeat(100)
        dataset_dict = dataset_dict.batch(10)

        model.fit(dataset_dict, epochs=1, steps_per_epoch=2, verbose=1)
        model.evaluate(dataset_dict, steps=2, verbose=1)

        predict_dataset_dict = tf.data.Dataset.from_tensor_slices(input_dict)
        predict_dataset_dict = predict_dataset_dict.repeat(100)
        predict_dataset_dict = predict_dataset_dict.batch(10)
        model.predict(predict_dataset_dict, steps=1)
Beispiel #6
0
    def test_model_methods_with_eager_tensors_multi_io(self):
        if not tf.executing_eagerly():
            # Only test V2 Function and V2 Eager modes, as V1 Graph mode with
            # symbolic tensors has different requirements.
            return

        input_a = keras.layers.Input(shape=(3, ), name='input_a')
        input_b = keras.layers.Input(shape=(3, ), name='input_b')

        dense = keras.layers.Dense(4, name='dense')
        dropout = keras.layers.Dropout(0.5, name='dropout')

        model = testing_utils.get_multi_io_model([input_a, dense],
                                                 [input_b, dense, dropout])

        optimizer = rmsprop.RMSprop(learning_rate=0.001)
        loss = 'mse'
        loss_weights = [1., 0.5]
        metrics = ['mae', metrics_module.CategoricalAccuracy()]
        model.compile(optimizer,
                      loss,
                      metrics=metrics,
                      loss_weights=loss_weights,
                      run_eagerly=testing_utils.should_run_eagerly(),
                      sample_weight_mode=None)

        input_a = tf.zeros(shape=(10, 3))
        input_b = tf.zeros(shape=(10, 3))
        target_a = tf.zeros(shape=(10, 4))
        target_b = tf.zeros(shape=(10, 4))

        model.fit([input_a, input_b], [target_a, target_b],
                  epochs=1,
                  batch_size=5,
                  verbose=0)
        # Test: no shuffle.
        model.fit([input_a, input_b], [target_a, target_b],
                  epochs=1,
                  batch_size=5,
                  verbose=0,
                  shuffle=False)
        # Test: validation data.
        model.fit([input_a, input_b], [target_a, target_b],
                  epochs=1,
                  batch_size=2,
                  verbose=0,
                  validation_data=([input_a, input_b], [target_a, target_b]))
        model.train_on_batch([input_a, input_b], [target_a, target_b])
        model.predict([input_a, input_b], batch_size=5)
        model.evaluate([input_a, input_b], [target_a, target_b],
                       batch_size=2,
                       verbose=0)
        model.test_on_batch([input_a, input_b], [target_a, target_b])

        # Test: mix np and tensors.
        input_b = np.zeros(shape=(10, 3)).astype('float32')
        target_b = np.zeros(shape=(10, 4)).astype('float32')
        model.fit([input_a, input_b], [target_a, target_b],
                  epochs=1,
                  batch_size=5,
                  verbose=0)
        model.fit([input_a, input_b], [target_a, target_b],
                  epochs=1,
                  batch_size=2,
                  verbose=0,
                  validation_data=([input_a, input_b], [target_a, target_b]))
        model.fit([input_a, input_b], [target_a, target_b],
                  epochs=1,
                  batch_size=5,
                  verbose=0,
                  shuffle=False)
        model.train_on_batch([input_a, input_b], [target_a, target_b])
        model.predict([input_a, input_b], batch_size=5)
        model.evaluate([input_a, input_b], [target_a, target_b],
                       batch_size=2,
                       verbose=0)
        model.test_on_batch([input_a, input_b], [target_a, target_b])