|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Ranking Neural Net |
| 4 | +
|
| 5 | +Author: Daniel Kroening, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include "ranking_net.h" |
| 10 | + |
| 11 | +#include <util/arith_tools.h> |
| 12 | +#include <util/bitvector_types.h> |
| 13 | +#include <util/std_expr.h> |
| 14 | + |
| 15 | +exprt wrap_around(exprt expr) |
| 16 | +{ |
| 17 | + // map negative values to positive ones, wrap around: |
| 18 | + // -1 --> int_max-1, -2 --> int_max-2, etc. |
| 19 | + auto int_max = to_signedbv_type(expr.type()).largest_expr(); |
| 20 | + return if_exprt{sign_exprt{expr}, plus_exprt{int_max, expr}, expr}; |
| 21 | +} |
| 22 | + |
| 23 | +tuple_exprt wrap_around_tuple(tuple_exprt expr) |
| 24 | +{ |
| 25 | + // map negative values to positive ones, wrap around: |
| 26 | + // -1 --> int_max-1, -2 --> int_max-2, etc. |
| 27 | + for(auto &op : expr.operands()) |
| 28 | + op = wrap_around(op); |
| 29 | + |
| 30 | + return expr; |
| 31 | +} |
| 32 | + |
| 33 | +tuple_exprt ranking_nett::forward(const tuple_exprt &inputs) const |
| 34 | +{ |
| 35 | + auto fc1_out = fc1.forward(inputs); |
| 36 | + auto w_out = wrap_around_tuple(fc1_out); |
| 37 | + return w_out; |
| 38 | +} |
| 39 | + |
| 40 | +ranking_nett::lineart::lineart( |
| 41 | + const irep_idt &__name, |
| 42 | + std::size_t in, |
| 43 | + std::size_t out, |
| 44 | + const typet &type) |
| 45 | + : name(__name) |
| 46 | +{ |
| 47 | + neurons.reserve(out); |
| 48 | + |
| 49 | + for(std::size_t i = 0; i < out; i++) |
| 50 | + { |
| 51 | + irep_idt identifier = id2string(__name) + ".n" + std::to_string(i); |
| 52 | + neurons.emplace_back(identifier, in, type); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +ranking_nett::lineart::neuront::neuront( |
| 57 | + const irep_idt &__name, |
| 58 | + std::size_t in, |
| 59 | + const typet &type) |
| 60 | + : name(__name) |
| 61 | +{ |
| 62 | + { |
| 63 | + irep_idt identifier = id2string(__name) + ".b"; |
| 64 | + bias = symbol_exprt(identifier, type); |
| 65 | + } |
| 66 | + |
| 67 | + weights.reserve(in); |
| 68 | + |
| 69 | + for(std::size_t i = 0; i < in; i++) |
| 70 | + { |
| 71 | + irep_idt identifier = id2string(__name) + ".w" + std::to_string(i); |
| 72 | + weights.emplace_back(symbol_exprt{identifier, type}); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +exprt ranking_nett::lineart::neuront::forward(const tuple_exprt &input) const |
| 77 | +{ |
| 78 | + exprt::operandst result; |
| 79 | + result.reserve(weights.size() + 1); // one more for bias |
| 80 | + |
| 81 | + result.push_back(bias); |
| 82 | + |
| 83 | + PRECONDITION(weights.size() == input.operands().size()); |
| 84 | + |
| 85 | + for(std::size_t i = 0; i < weights.size(); i++) |
| 86 | + result.push_back(mult_exprt{weights[i], input.operands()[i]}); |
| 87 | + |
| 88 | + return plus_exprt{std::move(result), bias.type()}; |
| 89 | +} |
| 90 | + |
| 91 | +exprt relu(exprt expr) |
| 92 | +{ |
| 93 | + auto zero = from_integer(0, expr.type()); |
| 94 | + return if_exprt{greater_than_or_equal_exprt{expr, zero}, expr, zero}; |
| 95 | +} |
| 96 | + |
| 97 | +tuple_exprt ranking_nett::lineart::forward(const tuple_exprt &input) const |
| 98 | +{ |
| 99 | + tuple_exprt::operandst result; |
| 100 | + result.reserve(neurons.size()); |
| 101 | + |
| 102 | + for(auto &neuron : neurons) |
| 103 | + result.push_back(neuron.forward(input)); |
| 104 | + |
| 105 | + return tuple_exprt{std::move(result)}; |
| 106 | +} |
0 commit comments