def test_subdomain(self): World.reset() """Checking caching mechanism works""" # Program Model nn = tfl.functions.FeedForwardNN(input_shape=[2], output_size=2) images = tfl.Domain(label="Images", data=[[0., 0], [1, 1], [0.2, 0.3]]) supervised_images = tfl.Domain(label="SupImages", data=[[0., 0], [1, 1]], father=images) zero = tfl.Predicate(label="zero", domains=["Images"], function=tfl.functions.Slice(nn, 0)) one = tfl.Predicate(label="one", domains=["Images"], function=tfl.functions.Slice(nn, 1)) tfl.setTNorm(id=tfl.PRODUCT, p=None) # Constraint 1 c1 = tfl.constraint("zero(x) and one(x)", {"x": supervised_images}) x = tfl.variable(supervised_images, name="x") c2 = tfl.and_n(zero(x), one(x)) sess = tf.Session() sess.run(tf.global_variables_initializer()) assert len(sess.run(c1)) == supervised_images.size assert np.equal(sess.run(c1), sess.run(c2)).all()
def test_parser_forall(self): World.reset() World._evaluation_mode = tfl.LOGIC_MODE tfl.setTNorm(id=tfl.SS, p=1) sess = tf.Session() def inside(x, y): centers_distance = tf.sqrt( tf.reduce_sum(tf.squared_difference(x[:, 0:2], y[:, 0:2]), axis=1) + 1e-6) return tf.cast((centers_distance + x[:, 2]) < y[:, 2], tf.float32) circles = tfl.Domain(label="Circles", data=[[0., 0, 1], [0, 0, 2], [0, 0, 3]]) inside = tfl.Predicate(label="inside", domains=["Circles", "Circles"], function=inside) x = tfl.variable(circles, "x") y = tfl.variable(circles, "y") symmetric_formula = tfl.forall( x, tfl.forall(y, tfl.and_n(inside(x, y), inside(y, x)))) symmetric_parsed = tfl.constraint( "forall x: forall y: inside(x,y) and inside(y,x)") assert np.equal(sess.run(symmetric_formula), sess.run(symmetric_parsed))
def test_parser_and(self): World.reset() World._evaluation_mode = tfl.LOGIC_MODE def inside(x, y): centers_distance = tf.sqrt( tf.reduce_sum(tf.squared_difference(x[:, 0:2], y[:, 0:2]), axis=1) + 1e-6) return tf.cast((centers_distance + x[:, 2]) < y[:, 2], tf.float32) tfl.setTNorm(id=tfl.SS, p=1) circles = tfl.Domain(label="Circles", data=[[0., 0, 1], [0, 0, 2], [0, 0, 3]]) inside = tfl.Predicate(label="inside", domains=["Circles", "Circles"], function=inside) x = tfl.variable(circles, "x") y = tfl.variable(circles, "y") a = tfl.atom(inside, (x, y)) b = tfl.atom(inside, (y, x)) f = tfl.and_n(a, b) tensor = tfl.constraint("inside(x,y) and inside(y,x)") sess = tf.Session() assert np.equal(sess.run(tensor), sess.run(f)).all()
def test_slice_caching(self): World.reset() """Checking caching mechanism works""" # Program Model nn = tfl.functions.FeedForwardNN(input_shape=[2], output_size=2) images = tfl.Domain(label="Images", data=[[0., 0], [1, 1], [0.2, 0.3]]) zero = tfl.Predicate(label="zero", domains=["Images"], function=tfl.functions.Slice(nn, 0)) one = tfl.Predicate(label="one", domains=["Images"], function=tfl.functions.Slice(nn, 1)) close = tfl.Predicate( label="close", domains=["Images", "Images"], function=lambda x: tf.reduce_sum(tf.abs(x[0] - x[1]), axis=1)) tfl.setTNorm(id=tfl.PRODUCT, p=None) # Constraint 1 c1 = tfl.constraint("forall x: zero(x) and one(x) and one(y)")
tfl.World.reset() tfl.World._evaluation_mode = tfl.LOSS_MODE tfl.setTNorm(id=tfl.SS, p=1) # Domains Definition images = tfl.Domain("Images", data=x_final, size=minibatch_size) # Predicates Definition for k,v in predicates_dict.items(): tfl.Predicate(k, domains=("Images",), function=tfl.functions.Slice(cifar, v)) # Constraints constraints = [] constraints.append(tfl.constraint("forall x: transport(x) <-> airplane(x) or ship(x) or onroad(x)")) constraints.append(tfl.constraint("forall x: onroad(x) <-> automobile(x) or truck(x)")) constraints.append(tfl.constraint("forall x: animal(x) <-> bird(x) or frog(x) or mammal(x)")) constraints.append(tfl.constraint("forall x: mammal(x) <-> cat(x) or deer(x) or dog(x) or horse(x)")) constraints.append(tfl.constraint("forall x: otherleaves(x) <-> bird(x) or frog(x) or ship(x) or airplane(x)")) constraints.append(tfl.constraint("forall x: flies(x) <-> bird(x) or airplane(x)")) constraints.append(tfl.constraint("forall x: notflies(x) <-> cat(x) or dog(x) or horse(x) or deer(x) or truck(x) or ship(x) or automobile(x) or frog(x)")) # --------------------- LEARNING -----------------------------# print("Creating learning operations graph...") y_pred = cifar_log(x_tr)[:,:original_num_classes] sup_loss = tf.losses.softmax_cross_entropy(logits=y_pred, onehot_labels=y_tr)
num_points_ph = tf.placeholder(tf.int64) X_ph = tf.placeholder(tf.float32, shape=(None, 2)) Points = tfl.Domain(label="Points", data=X_ph, size=num_points_ph) SPoints = tfl.Domain(label="SPoints", data=X_sup_np, father=Points) R1 = tfl.Predicate("A", domains=[ "Points", ], function=is_A) R2 = tfl.Predicate("isClose", domains=["Points", "Points"], function=is_close) R3 = tfl.Predicate("SA", domains=[ "Points", ], function=lambda x: tf.squeeze(y_sup)) c_m = tfl.constraint("forall p: forall q: isClose(p,q) -> (A(p) <-> A(q))") c_s = tfl.constraint("forall p: SA(p) <-> A(p)", {"p": SPoints}) loss_pre_vincoli = c_s activate_rules = tf.placeholder(dtype=tf.bool, shape=[]) lr = tf.placeholder(dtype=tf.float32, shape=[]) loss_post_vincoli = loss_pre_vincoli + 0.001 * c_m loss = tf.cond(activate_rules, lambda: loss_post_vincoli, lambda: loss_pre_vincoli) train_op = tf.train.AdamOptimizer(lr).minimize(loss) test_outputs = is_A(X_test)[:, 0:1] test_predictions = tf.where(test_outputs > 0.5, tf.ones_like(test_outputs, dtype=tf.float32), tf.zeros_like(test_outputs, dtype=tf.float32))