Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Node.h
- #ifndef NODE_H
- #define NODE_H
- #include <vector>
- #include <fstream>
- #include <iostream>
- class Node
- {
- public:
- Node();
- Node(std::vector<double> weights, double bias);
- virtual ~Node();
- double output(std::vector<double> input);
- friend std::ostream& operator << (std::ostream& out, const Node& node);
- friend std::ofstream& operator << (std::ofstream& out, const Node& node);
- friend std::ifstream& operator >> (std::ifstream& in, Node& node);
- protected:
- private:
- std::vector<double> weights;
- double bias;
- double z(std::vector<double> input);
- };
- void test_node();
- #endif // NODE_H
- // ------------ Node.cpp -------------------------
- #include "Node.h"
- #include <stdexcept>
- #include <string>
- #include <cmath>
- double dot(std::vector<double> v1, std::vector<double> v2){
- if(v1.size() != v2.size())
- {
- throw std::length_error(std::string("dot product: invalid vector lengths"));
- }
- double dot_product;
- for(size_t i = 0; i < v1.size(); ++i){
- dot_product += v1[i] * v2[i];
- }
- return dot_product;
- }
- double sigmoid(double z){
- if(z < 0){
- double s = 1 - 1 / ( 1 + exp(z));
- return s;
- }
- else{
- double s = 1 / ( 1 + exp(-z));
- return s;
- }
- }
- Node::Node(){
- }
- Node::Node(std::vector<double> iweights, double ibias) : weights(iweights), bias(ibias)
- {
- }
- Node::~Node()
- {
- //dtor
- }
- double Node::z(std::vector<double> input)
- {
- return dot(input, weights) + bias;
- }
- double Node::output(std::vector<double> input)
- {
- return sigmoid(z(input));
- }
- std::ostream& operator << (std::ostream& out, const Node& node)
- {
- out << "Node [weights(" << node.weights.size() << ")";
- if(node.weights.size() > 0)
- {
- out << ": {";
- for(auto w = node.weights.begin(); w != (node.weights.end() - 1); ++w)
- {
- out << *w << ", ";
- }
- out << * (node.weights.end() - 1) << "}";
- }
- out << " bias: " << node.bias << "]";
- return out;
- }
- std::ofstream& operator << (std::ofstream& out, const Node& node){
- out << node.bias;
- out << (size_t) node.weights.size();
- for(auto w : node.weights)
- {
- out << w;
- }
- return out;
- }
- std::ifstream& operator >> (std::ifstream& in, Node& node)
- {
- in >> node.bias;
- std::cout << node.bias << std::endl;
- size_t sz;
- in >> sz;
- std::vector<double> weights;
- for(size_t i = 0; i < sz; ++i){
- double w;
- in >> w;
- weights.push_back(w);
- }
- node.weights = weights;
- return in;
- }
- void test_node(){
- std::vector<double> v{1, 2, 3, 4};
- std::cout << v.size() << std::endl;
- Node n(v, 10);
- std::cout << n << std::endl;
- std::cout << "Writing to file..." << std::endl;
- std::ofstream node_file_out("./test/node_file.ndf", std::ios::out|std::ios::binary);
- if(node_file_out.is_open())
- {
- std::cout << "hello" << std::endl;
- node_file_out << n;
- node_file_out.flush();
- node_file_out.close();
- }
- Node n2;
- std::ifstream node_file_in("./test/node_file.ndf", std::ios::in|std::ios::binary);
- node_file_in >> n2;
- node_file_in.close();
- std::cout << n2 << std::endl;
- }
Add Comment
Please, Sign In to add comment