interval-bound-propagation icon indicating copy to clipboard operation
interval-bound-propagation copied to clipboard

Can VerifiableModelWrapper be used out of the box?

Open altostratous opened this issue 4 years ago • 2 comments

I am trying to apply your losses and optimizer to other models and applications. What are the steps to do? Is it just wrapping my model with this VerifiableModelWrapper or should I do more?

altostratous avatar Dec 31 '20 02:12 altostratous

Please see the train.py example. There is also an example for a language model here.

The steps consist of (i) wrapping your Sonnet model using VerifiableModelWrapper, (ii) defining the input bounds (example) and (iii) computing your loss using propagated bounds.

I hope this helps.

sgowal avatar Jan 06 '21 09:01 sgowal

I am trying to directly get the propagated output bounds. Is there a way to extract that? For example, input_interval_bounds is the input bounds, I pass it into the predictor with predictor.propagate_bounds(input_interval_bounds), how can I get the output_interval_bounds?

Here is my program:

import interval_bound_prop as ibp
import sonnet as snt
import tensorflow as tf
class SymbolicActor(snt.AbstractModule):
  def __init__(self, s_dim, a_dim, h1_shape, h2_shape, action_scale=1.0, name='actor'):
    super(SymbolicActor, self).__init__(name=name)
    self.s_dim = s_dim
    self.a_dim = a_dim
    self.name = name
    self.action_scale = action_scale
    self.h1_shape = h1_shape
    self.h2_shape = h2_shape

  def _build(self, s, is_training=True): #, s_eps=0.0, is_symbolic=False):
    with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):
      m = snt.Linear(output_size=self.h1_shape, use_bias=True, name='fc1')
      h1 = m(s)
      m = snt.BatchNorm(name='bn1')
      h1 = m(h1, is_training=is_training)
      h1 = tf.nn.leaky_relu(h1)

      m = snt.Linear(output_size=self.h2_shape, use_bias=True, name='fc2')
      h2 = m(h1)
      m = snt.BatchNorm(name='bn2')
      h2 = m(h2, is_training=is_training)
      h2 = tf.nn.leaky_relu(h2)

      m = snt.Linear(output_size=self.a_dim, use_bias=True, name='fc1')
      h3 = m(h2)
      output = tf.nn.tanh(h3)

      scale_output = tf.multiply(output, self.action_scale)

    return scale_output

import numpy as np

state_dim = 7
s_dim = state_dim
a_dim = 1
s0 = tf.placeholder(tf.float32, shape=[None, s_dim], name='s0')
s0_eps = tf.placeholder(tf.float32, shape=[None, s_dim], name='s0_eps')
is_training = tf.placeholder(tf.bool, name='Actor_is_training')
is_symbolic = tf.placeholder(tf.bool, name='Actor_using_symbolic')
symbolic_actor = SymbolicActor(
    s_dim,
    a_dim, 
    action_scale=1.0,
    h1_shape=256,
    h2_shape=256
)
model_wrapper = ibp.VerifiableModelWrapper
predictor = model_wrapper(symbolic_actor)

concrete_actor_out = predictor(s0, is_training=is_training)
input_interval_bounds = ibp.IntervalBounds(s0 - 0.1, s0 + 0.1)
print(f"input_interval_bounds: {input_interval_bounds}")
symbolic_actor_out = predictor.propagate_bounds(input_interval_bounds)

def create_input_op_shape(obs, tensor):
    input_shape = [x or -1 for x in tensor.shape.as_list()]
    return np.reshape(obs, input_shape)

s = np.zeros([s_dim])
s0_rec_buffer = np.zeros([s_dim])
s0_rec_buffer[-1*7:] = s
tfconfig = tf.ConfigProto(allow_soft_placement=True)
sess = tf.Session(config=tf_config)
init = tf.global_variables_initializer()
sess.run(init)
fd = {s0: create_input_op_shape(s0_rec_buffer, s0), is_training:True}
for _ in range(5):
    symbolic_actor_out = sess.run([concrete_actor_out], feed_dict=fd)
    print(symbolic_actor_out) # I want this output to be the interval bounds as well. But now it is just the concrete values.

Can you help me check which part I should modify?

Thanks, Chenxi

chenxi-yang avatar Mar 06 '24 19:03 chenxi-yang