def test_handles_multiple_masks(self): # We'll use the SlotSimilarityTupleMatcher to test this, because it takes two masked # inputs. Here we're using an input of shape (batch_size, num_options, num_tuples, # num_slots, num_words). tuple_input = Input(shape=(2, 3, 4, 5), dtype='int32') tuple_input_2 = Input(shape=(2, 3, 4, 5), dtype='int32') embedding = TimeDistributedEmbedding(input_dim=3, output_dim=6, mask_zero=True) # shape is now (batch_size, num_options, num_tuples, num_slots, num_words, embedding_dim) embedded_tuple = embedding(tuple_input) embedded_tuple_2 = embedding(tuple_input_2) encoder = EncoderWrapper(EncoderWrapper(EncoderWrapper(BOWEncoder()))) # shape is now (batch_size, num_options, num_tuples, num_slots, embedding_dim) encoded_tuple = encoder(embedded_tuple) encoded_tuple_2 = encoder(embedded_tuple_2) # Shape of input to the tuple matcher is [(batch size, 2, 3, 4, 6), (batch size, 2, 3, 4, 6)] # Shape of input_mask to the tuple matcher is [(batch size, 2, 3, 4), (batch size, 2, 3, 4)] # Expected output mask shape (batch_size, 2, 3) time_distributed = TimeDistributedWithMask( TimeDistributedWithMask( SlotSimilarityTupleMatcher({"type": "cosine_similarity"}))) time_distributed_output = time_distributed( [encoded_tuple, encoded_tuple_2]) mask_output = OutputMask()(time_distributed_output) model = DeepQaModel(input=[tuple_input, tuple_input_2], output=mask_output) zeros = [0, 0, 0, 0, 0] non_zeros = [1, 1, 1, 1, 1] # shape: (batch size, num_options, num_tuples, num_slots, num_words), or (1, 2, 3, 4, 5) tuples1 = numpy.asarray([[[[zeros, zeros, zeros, zeros], [non_zeros, zeros, zeros, zeros], [non_zeros, non_zeros, zeros, zeros]], [[non_zeros, non_zeros, zeros, zeros], [non_zeros, zeros, zeros, zeros], [zeros, zeros, zeros, zeros]]]]) tuples2 = numpy.asarray([[[[non_zeros, zeros, zeros, zeros], [non_zeros, zeros, zeros, zeros], [zeros, zeros, zeros, zeros]], [[non_zeros, non_zeros, zeros, zeros], [non_zeros, zeros, zeros, zeros], [non_zeros, zeros, zeros, zeros]]]]) actual_mask = model.predict([tuples1, tuples2]) expected_mask = numpy.asarray( [[[0, 1, 0], [1, 1, 0]]]) # shape: (batch size, num_options, num_tuples) assert actual_mask.shape == (1, 2, 3) numpy.testing.assert_array_almost_equal(expected_mask, actual_mask)
def test_on_masked_input(self): # TODO(matt): I don't really like having to build the whole model up to the attention # component here, but I'm not sure how to just test the selector with the right mask # without going through this. sentence_input = Input(shape=(3, ), dtype='int32') background_input = Input(shape=(3, 3), dtype='int32') embedding = TimeDistributedEmbedding(input_dim=3, output_dim=2, mask_zero=True) embedded_sentence = embedding(sentence_input) embedded_background = embedding(background_input) encoder = BOWEncoder(output_dim=2) encoded_sentence = encoder(embedded_sentence) encoded_background = EncoderWrapper(encoder)(embedded_background) merge_mode = lambda layer_outs: K.concatenate([ K.expand_dims(layer_outs[0], dim=1), K.expand_dims(layer_outs[0], dim=1), layer_outs[1] ], axis=1) merge_masks = lambda mask_outs: K.concatenate([ K.expand_dims(K.zeros_like(mask_outs[1][:, 0]), dim=1), K.expand_dims(K.zeros_like(mask_outs[1][:, 0]), dim=1), mask_outs[1 ] ], axis=1) merged = merge([encoded_sentence, encoded_background], mode=merge_mode, output_shape=(5, 2), output_mask=merge_masks) merged_mask = OutputMask()(merged) selector = DotProductKnowledgeSelector() attention_weights = selector(merged) model = DeepQaModel(input=[sentence_input, background_input], output=[merged_mask, attention_weights]) model.summary(show_masks=True) test_input = numpy.asarray([[2, 2, 2]]) test_background = numpy.asarray([[ [2, 2, 2], [2, 2, 2], [0, 0, 0], ]]) expected_mask = numpy.asarray([[0, 0, 1, 1, 0]]) expected_attention = numpy.asarray([[0.5, 0.5, 0.0]]) actual_mask, actual_attention = model.predict( [test_input, test_background]) numpy.testing.assert_array_almost_equal(expected_mask, actual_mask) numpy.testing.assert_array_almost_equal(expected_attention, actual_attention)
def test_mask_is_computed_correctly(self): # TODO(matt): I don't really like having to build a model to test this, but I'm not sure of # how else to do it. background_input = Input(shape=(3, 3), dtype='int32') embedding = TimeDistributedEmbedding(input_dim=3, output_dim=2, mask_zero=True) embedded_background = embedding(background_input) encoded_background = EncoderWrapper( BOWEncoder(output_dim=2))(embedded_background) mask_output = OutputMask()(encoded_background) model = DeepQaModel(input=[background_input], output=mask_output) test_background = numpy.asarray([[ [0, 0, 0], [2, 2, 2], [0, 0, 0], ]]) expected_mask = numpy.asarray([[0, 1, 0]]) actual_mask = model.predict([test_background]) numpy.testing.assert_array_almost_equal(expected_mask, actual_mask)