def test_formulate_smt_constraints_convolution_layer_text(self): with self.test_session(): # Temporary graphs should be created inside a session. Notice multiple # graphs are being created in this particular code. So, if each graph # isn't created inside a separate session, the tensor names will have # unwanted integer suffices, which then would cause problems while # accessing tensors by name. _create_temporary_tf_graph_text_cnn(self.test_model_path) # The 1st convolution layer has 12 neurons. image = np.ones(5) tensor_names = { 'input': 'input_1:0', 'embedding': 'embedding/embedding_lookup/Identity_1:0', 'first_layer': 'conv1d/BiasAdd:0', 'first_layer_relu': 'conv1d/Relu:0', 'logits': 'dense/BiasAdd:0', 'softmax': 'dense/Sigmoid:0', 'weights_layer_1': 'conv1d/conv1d/ExpandDims_1:0', 'biases_layer_1': 'conv1d/BiasAdd/ReadVariableOp:0' } session = utils.restore_model(self.test_model_path) cnn_predictions = session.run( tensor_names, feed_dict={tensor_names['input']: image.reshape(1, 5)}) text_embedding = masking._remove_batch_axis( cnn_predictions['embedding']) z3_mask = [ z3.Int('mask_%d' % i) for i in range(text_embedding.shape[0]) ] masked_input = [] for mask_bit, embedding_row in zip(z3_mask, text_embedding): masked_input.append( [z3.ToReal(mask_bit) * i for i in embedding_row]) first_layer_activations = masking._reorder( masking._remove_batch_axis( cnn_predictions['first_layer'])).reshape(-1) z3_optimizer = masking._formulate_smt_constraints_convolution_layer( z3_optimizer=utils.TextOptimizer(z3_mask=z3_mask), kernels=masking._reshape_kernels( kernels=cnn_predictions['weights_layer_1'], model_type='text_cnn'), biases=cnn_predictions['biases_layer_1'], chosen_indices=first_layer_activations.argsort()[-5:], conv_activations=first_layer_activations, input_activation_maps=[masked_input], output_activation_map_shape=masking._get_activation_map_shape( activation_maps_shape=cnn_predictions['first_layer'].shape, model_type='text_cnn'), strides=1, padding=(0, 0), gamma=0.5) mask, result = z3_optimizer.generate_mask() self.assertEqual(result, 'sat') self.assertEqual(mask.shape, (5, )) session.close()
def test_reshape_kernels(self, model_type, reshaped_dimensions): activation_maps_shape = tuple(np.random.randint(low=1, high=10, size=4)) reshaped_kernel = masking._reshape_kernels( kernels=np.ones(activation_maps_shape), model_type=model_type) self.assertEqual(reshaped_kernel.shape, (activation_maps_shape[reshaped_dimensions[0]], activation_maps_shape[reshaped_dimensions[1]], activation_maps_shape[reshaped_dimensions[2]], activation_maps_shape[reshaped_dimensions[3]]))