|
| 1 | +#include <Rcpp.h> |
| 2 | +#include "NetworkFactory.h" |
| 3 | +#include "Network.h" |
| 4 | +#include "nodes/Node.h" |
| 5 | +#include "Sequence.h" |
| 6 | +#include "SequenceSet.h" |
| 7 | +#include "trainer/Trainer.h" |
| 8 | +#include "trainer/BackpropTrainer.h" |
| 9 | +#include "trainer/ADAMTrainer.h" |
| 10 | +#include "trainer/RMSPropTrainer.h" |
| 11 | +#include "trainer/RPropTrainer.h" |
| 12 | +#include "trainer/MyRPropTrainer.h" |
| 13 | + |
| 14 | +using namespace Rcpp; |
| 15 | + |
| 16 | +namespace { |
| 17 | +SEXP make_node_xptr(Node* node) { |
| 18 | + XPtr<Node> ptr(node, false); |
| 19 | + return ptr; |
| 20 | +} |
| 21 | + |
| 22 | +SEXP make_sequence_xptr(Sequence* sequence) { |
| 23 | + XPtr<Sequence> ptr(sequence, true); |
| 24 | + return ptr; |
| 25 | +} |
| 26 | + |
| 27 | +SEXP make_trainer_xptr(Trainer* trainer) { |
| 28 | + XPtr<Trainer> ptr(trainer, true); |
| 29 | + return ptr; |
| 30 | +} |
| 31 | +} |
| 32 | + |
| 33 | +extern "C" SEXP bnn_create_feedforward_network(SEXP in_sizeSEXP, SEXP hid_sizeSEXP, SEXP out_sizeSEXP) { |
| 34 | + int in_size = as<int>(in_sizeSEXP); |
| 35 | + int hid_size = as<int>(hid_sizeSEXP); |
| 36 | + int out_size = as<int>(out_sizeSEXP); |
| 37 | + if (in_size <= 0 || hid_size <= 0 || out_size <= 0) stop("All layer sizes must be positive integers."); |
| 38 | + Network* network = NetworkFactory::createFeedForwardNetwork((unsigned int)in_size, (unsigned int)hid_size, (unsigned int)out_size); |
| 39 | + XPtr<Network> ptr(network, true); |
| 40 | + return ptr; |
| 41 | +} |
| 42 | + |
| 43 | +extern "C" SEXP bnn_network_num_nodes(SEXP network_xptr) { |
| 44 | + XPtr<Network> net(network_xptr); |
| 45 | + return wrap((int)net->get_num_nodes()); |
| 46 | +} |
| 47 | + |
| 48 | +extern "C" SEXP bnn_network_node_names(SEXP network_xptr) { |
| 49 | + XPtr<Network> net(network_xptr); |
| 50 | + return wrap(net->get_node_names()); |
| 51 | +} |
| 52 | + |
| 53 | +extern "C" SEXP bnn_network_get_node(SEXP network_xptr, SEXP index_one_basedSEXP) { |
| 54 | + XPtr<Network> net(network_xptr); |
| 55 | + int idx = as<int>(index_one_basedSEXP); |
| 56 | + if (idx < 1 || idx > (int)net->nodes.size()) stop("index_one_based is out of range."); |
| 57 | + return make_node_xptr(net->nodes[(size_t)(idx - 1)]); |
| 58 | +} |
| 59 | + |
| 60 | +extern "C" SEXP bnn_node_name(SEXP node_xptr) { |
| 61 | + XPtr<Node> node(node_xptr); |
| 62 | + return wrap(node->name); |
| 63 | +} |
| 64 | + |
| 65 | +extern "C" SEXP bnn_node_num_incoming(SEXP node_xptr) { |
| 66 | + XPtr<Node> node(node_xptr); |
| 67 | + return wrap((int)node->get_num_incoming_connections()); |
| 68 | +} |
| 69 | + |
| 70 | +extern "C" SEXP bnn_node_num_outgoing(SEXP node_xptr) { |
| 71 | + XPtr<Node> node(node_xptr); |
| 72 | + return wrap((int)node->get_num_outgoing_connections()); |
| 73 | +} |
| 74 | + |
| 75 | +extern "C" SEXP bnn_sequence_create() { |
| 76 | + return make_sequence_xptr(new Sequence()); |
| 77 | +} |
| 78 | + |
| 79 | +extern "C" SEXP bnn_sequence_add(SEXP sequence_xptr, SEXP inputSEXP, SEXP targetSEXP) { |
| 80 | + XPtr<Sequence> sequence(sequence_xptr); |
| 81 | + NumericVector input = as<NumericVector>(inputSEXP); |
| 82 | + NumericVector target = as<NumericVector>(targetSEXP); |
| 83 | + std::vector<weight_t>* in = new std::vector<weight_t>(input.begin(), input.end()); |
| 84 | + std::vector<weight_t>* tar = new std::vector<weight_t>(target.begin(), target.end()); |
| 85 | + sequence->add(in, tar); |
| 86 | + return R_NilValue; |
| 87 | +} |
| 88 | + |
| 89 | +extern "C" SEXP bnn_sequence_size(SEXP sequence_xptr) { |
| 90 | + XPtr<Sequence> sequence(sequence_xptr); |
| 91 | + return wrap((int)sequence->size()); |
| 92 | +} |
| 93 | + |
| 94 | +extern "C" SEXP bnn_sequence_get_input(SEXP sequence_xptr, SEXP index_one_basedSEXP) { |
| 95 | + XPtr<Sequence> sequence(sequence_xptr); |
| 96 | + int idx = as<int>(index_one_basedSEXP); |
| 97 | + if (idx < 1 || idx > (int)sequence->size()) stop("index_one_based is out of range."); |
| 98 | + return wrap(*sequence->get_input((unsigned int)(idx - 1))); |
| 99 | +} |
| 100 | + |
| 101 | +extern "C" SEXP bnn_sequence_get_target(SEXP sequence_xptr, SEXP index_one_basedSEXP) { |
| 102 | + XPtr<Sequence> sequence(sequence_xptr); |
| 103 | + int idx = as<int>(index_one_basedSEXP); |
| 104 | + if (idx < 1 || idx > (int)sequence->size()) stop("index_one_based is out of range."); |
| 105 | + return wrap(*sequence->get_target((unsigned int)(idx - 1))); |
| 106 | +} |
| 107 | + |
| 108 | +extern "C" SEXP bnn_sequenceset_create() { |
| 109 | + XPtr<SequenceSet> ptr(new SequenceSet(), true); |
| 110 | + return ptr; |
| 111 | +} |
| 112 | + |
| 113 | +extern "C" SEXP bnn_sequenceset_add_sequence(SEXP sequenceset_xptr, SEXP sequence_xptr) { |
| 114 | + XPtr<SequenceSet> set(sequenceset_xptr); |
| 115 | + XPtr<Sequence> seq(sequence_xptr); |
| 116 | + set->add_copy_of_sequence(seq.get()); |
| 117 | + return R_NilValue; |
| 118 | +} |
| 119 | + |
| 120 | +extern "C" SEXP bnn_sequenceset_size(SEXP sequenceset_xptr) { |
| 121 | + XPtr<SequenceSet> set(sequenceset_xptr); |
| 122 | + return wrap((int)set->size()); |
| 123 | +} |
| 124 | + |
| 125 | +extern "C" SEXP bnn_sequenceset_get_sequence(SEXP sequenceset_xptr, SEXP index_one_basedSEXP) { |
| 126 | + XPtr<SequenceSet> set(sequenceset_xptr); |
| 127 | + int idx = as<int>(index_one_basedSEXP); |
| 128 | + if (idx < 1 || idx > (int)set->size()) stop("index_one_based is out of range."); |
| 129 | + XPtr<Sequence> ptr(new Sequence(*set->get((unsigned int)(idx - 1))), true); |
| 130 | + return ptr; |
| 131 | +} |
| 132 | + |
| 133 | +extern "C" SEXP bnn_create_trainer(SEXP trainer_typeSEXP, SEXP network_xptr) { |
| 134 | + std::string trainer_type = as<std::string>(trainer_typeSEXP); |
| 135 | + XPtr<Network> network(network_xptr); |
| 136 | + |
| 137 | + if (trainer_type == "backprop") return make_trainer_xptr(new BackpropTrainer(network.get())); |
| 138 | + if (trainer_type == "adam") return make_trainer_xptr(new ADAMTrainer(network.get())); |
| 139 | + if (trainer_type == "rmsprop") return make_trainer_xptr(new RMSPropTrainer(network.get())); |
| 140 | + if (trainer_type == "rprop") return make_trainer_xptr(new RPropTrainer(network.get())); |
| 141 | + if (trainer_type == "myrprop") return make_trainer_xptr(new MyRPropTrainer(network.get())); |
| 142 | + |
| 143 | + stop("Unsupported trainer_type. Use one of: backprop, adam, rmsprop, rprop, myrprop."); |
| 144 | +} |
| 145 | + |
| 146 | +extern "C" SEXP bnn_trainer_name(SEXP trainer_xptr) { |
| 147 | + XPtr<Trainer> trainer(trainer_xptr); |
| 148 | + return wrap(trainer->get_name()); |
| 149 | +} |
| 150 | + |
| 151 | +extern "C" SEXP bnn_trainer_train(SEXP trainer_xptr, SEXP sequenceset_xptr, SEXP iterationsSEXP) { |
| 152 | + XPtr<Trainer> trainer(trainer_xptr); |
| 153 | + XPtr<SequenceSet> set(sequenceset_xptr); |
| 154 | + int iterations = as<int>(iterationsSEXP); |
| 155 | + if (iterations <= 0) stop("iterations must be a positive integer."); |
| 156 | + trainer->train(set.get(), (unsigned int)iterations); |
| 157 | + return R_NilValue; |
| 158 | +} |
0 commit comments