removing function from neuron
This commit is contained in:
@@ -18,11 +18,13 @@ namespace NeuralNetwork
|
|||||||
class NeuronInterface
|
class NeuronInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NeuronInterface(const unsigned long &_id=0): id(_id) {
|
NeuronInterface(const unsigned long &_id=0): id(_id), weights(1),_output(1),
|
||||||
|
_value(0) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NeuronInterface(const NeuronInterface &r): id(r.id), weights(r.weights),_output(r._output) {
|
NeuronInterface(const NeuronInterface &r): id(r.id), weights(r.weights),_output(r._output),
|
||||||
|
_value(r._value) {
|
||||||
weights=weights;
|
weights=weights;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,20 +39,36 @@ namespace NeuralNetwork
|
|||||||
*/
|
*/
|
||||||
virtual std::string stringify(const std::string &prefix="") const =0;
|
virtual std::string stringify(const std::string &prefix="") const =0;
|
||||||
|
|
||||||
inline virtual float weight(const NeuronInterface &n) const final {
|
/**
|
||||||
return weights[n.id];
|
* @brief getter for neuron weight
|
||||||
|
* @param &neuron is neuron it's weight is returned
|
||||||
|
*/
|
||||||
|
inline virtual float weight(const NeuronInterface &neuron) const final {
|
||||||
|
return weights[neuron.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline virtual float weight(const std::size_t &n) const final {
|
/**
|
||||||
return weights[n];
|
* @brief getter for neuron weight
|
||||||
|
* @param &neuronID is id of neuron
|
||||||
|
*/
|
||||||
|
inline virtual float weight(const std::size_t &neuronID) const final {
|
||||||
|
return weights[neuronID];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline virtual float& weight(const NeuronInterface &n) final {
|
/**
|
||||||
return weights[n.id];
|
* @brief This is a virtual function for storing network
|
||||||
|
* @returns json describing network and it's state
|
||||||
|
*/
|
||||||
|
inline virtual float& weight(const NeuronInterface &neuron) final {
|
||||||
|
return weights[neuron.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline virtual float& weight(const std::size_t &n) final {
|
/**
|
||||||
return weights[n];
|
* @brief getter for neuron weight
|
||||||
|
* @param &neuronID is id of neuron
|
||||||
|
*/
|
||||||
|
inline virtual float& weight(const std::size_t &neuronID) final {
|
||||||
|
return weights[neuronID];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -62,22 +80,15 @@ namespace NeuralNetwork
|
|||||||
/**
|
/**
|
||||||
* @brief Returns input of neuron
|
* @brief Returns input of neuron
|
||||||
*/
|
*/
|
||||||
virtual float value() const=0;
|
inline virtual float value() const final {
|
||||||
|
return _value;
|
||||||
/**
|
};
|
||||||
* @brief Function sets bias for neuron
|
|
||||||
* @param bias is new bias (initial value for neuron)
|
|
||||||
*/
|
|
||||||
virtual void setBias(const float &bias)=0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Function returns bias for neuron
|
|
||||||
*/
|
|
||||||
virtual float getBias() const=0;
|
|
||||||
|
|
||||||
virtual float operator()(const std::vector<float>& inputs) =0;
|
virtual float operator()(const std::vector<float>& inputs) =0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief function resizes weighs to desired size
|
||||||
|
*/
|
||||||
inline virtual void setInputSize(const std::size_t &size) final {
|
inline virtual void setInputSize(const std::size_t &size) final {
|
||||||
if(weights.size()<size) {
|
if(weights.size()<size) {
|
||||||
weights.resize(size);
|
weights.resize(size);
|
||||||
@@ -89,21 +100,24 @@ namespace NeuralNetwork
|
|||||||
*/
|
*/
|
||||||
virtual NeuronInterface* clone() const = 0;
|
virtual NeuronInterface* clone() const = 0;
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* @brief getter for basis function of neuron
|
* @brief getter for basis function of neuron
|
||||||
*/
|
*/
|
||||||
virtual BasisFunction::BasisFunction& getBasisFunction() =0;
|
virtual BasisFunction::BasisFunction& getBasisFunction() =0;
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* @brief getter for activation function of neuron
|
* @brief getter for activation function of neuron
|
||||||
*/
|
*/
|
||||||
virtual ActivationFunction::ActivationFunction& getActivationFunction() =0;
|
virtual ActivationFunction::ActivationFunction& getActivationFunction() =0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief id is identificator if neuron
|
||||||
|
*/
|
||||||
const unsigned long id;
|
const unsigned long id;
|
||||||
protected:
|
protected:
|
||||||
std::vector<float> weights= std::vector<float>(1);
|
std::vector<float> weights;
|
||||||
float _output=1.0;
|
float _output;
|
||||||
|
float _value;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -115,13 +129,11 @@ namespace NeuralNetwork
|
|||||||
public:
|
public:
|
||||||
Neuron(unsigned long _id=0, const ActivationFunction::ActivationFunction &activationFunction=ActivationFunction::Sigmoid(-4.9)):
|
Neuron(unsigned long _id=0, const ActivationFunction::ActivationFunction &activationFunction=ActivationFunction::Sigmoid(-4.9)):
|
||||||
NeuronInterface(_id), basis(new BasisFunction::Linear),
|
NeuronInterface(_id), basis(new BasisFunction::Linear),
|
||||||
activation(activationFunction.clone()),
|
activation(activationFunction.clone()) {
|
||||||
_value(0) {
|
|
||||||
_output=0.0;
|
_output=0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Neuron(const Neuron &r): NeuronInterface(r), basis(r.basis->clone()), activation(r.activation->clone()),
|
Neuron(const Neuron &r): NeuronInterface(r), basis(r.basis->clone()), activation(r.activation->clone()) {
|
||||||
_value(r._value) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~Neuron() {
|
virtual ~Neuron() {
|
||||||
@@ -133,28 +145,6 @@ namespace NeuralNetwork
|
|||||||
|
|
||||||
virtual std::string stringify(const std::string &prefix="") const override;
|
virtual std::string stringify(const std::string &prefix="") const override;
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Returns input of neuron
|
|
||||||
*/
|
|
||||||
virtual float value() const override {
|
|
||||||
return _value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Function sets bias for neuron
|
|
||||||
* @param _bias is new bias (initial value for neuron)
|
|
||||||
*/
|
|
||||||
virtual void setBias(const float &_bias) override {
|
|
||||||
weights[0]=_bias;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Function returns bias for neuron
|
|
||||||
*/
|
|
||||||
virtual float getBias() const override {
|
|
||||||
return weights[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
float operator()(const std::vector<float>& inputs) {
|
float operator()(const std::vector<float>& inputs) {
|
||||||
//compute value
|
//compute value
|
||||||
_value=basis->operator()(weights,inputs);
|
_value=basis->operator()(weights,inputs);
|
||||||
@@ -181,8 +171,6 @@ namespace NeuralNetwork
|
|||||||
|
|
||||||
BasisFunction::BasisFunction *basis;
|
BasisFunction::BasisFunction *basis;
|
||||||
ActivationFunction::ActivationFunction *activation;
|
ActivationFunction::ActivationFunction *activation;
|
||||||
|
|
||||||
float _value;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class BiasNeuron: public NeuronInterface {
|
class BiasNeuron: public NeuronInterface {
|
||||||
@@ -200,14 +188,8 @@ namespace NeuralNetwork
|
|||||||
std::string text;
|
std::string text;
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual float getBias() const override { return 0; };
|
|
||||||
|
|
||||||
virtual void setBias(const float&) override{ }
|
|
||||||
|
|
||||||
virtual std::string stringify(const std::string& prefix = "") const override { return prefix+"{ \"class\" : \"NeuralNetwork::BiasNeuron\" }"; }
|
virtual std::string stringify(const std::string& prefix = "") const override { return prefix+"{ \"class\" : \"NeuralNetwork::BiasNeuron\" }"; }
|
||||||
|
|
||||||
virtual float value() const override { return 1.0; }
|
|
||||||
|
|
||||||
virtual float operator()(const std::vector< float >&) override { return 1.0; }
|
virtual float operator()(const std::vector< float >&) override { return 1.0; }
|
||||||
|
|
||||||
virtual BiasNeuron* clone() const { return new BiasNeuron(); }
|
virtual BiasNeuron* clone() const { return new BiasNeuron(); }
|
||||||
@@ -241,14 +223,8 @@ namespace NeuralNetwork
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual float getBias() const override { return 0; };
|
|
||||||
|
|
||||||
virtual void setBias(const float&) override{ }
|
|
||||||
|
|
||||||
virtual std::string stringify(const std::string& prefix = "") const override { return prefix+"{ \"class\" : \"NeuralNetwork::InputNeuron\", \"id\": "+std::to_string(id)+" }"; }
|
virtual std::string stringify(const std::string& prefix = "") const override { return prefix+"{ \"class\" : \"NeuralNetwork::InputNeuron\", \"id\": "+std::to_string(id)+" }"; }
|
||||||
|
|
||||||
virtual float value() const override { return 1.0; }
|
|
||||||
|
|
||||||
virtual float operator()(const std::vector< float >&) override { return 1.0; }
|
virtual float operator()(const std::vector< float >&) override { return 1.0; }
|
||||||
|
|
||||||
virtual InputNeuron* clone() const { return new InputNeuron(id); }
|
virtual InputNeuron* clone() const { return new InputNeuron(id); }
|
||||||
|
|||||||
@@ -74,6 +74,5 @@ void NeuralNetwork::Learning::BackPropagation::teach(FeedForward::Network &netwo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,6 @@ std::string NeuralNetwork::Neuron::stringify(const std::string &prefix) const {
|
|||||||
out << prefix << "{\n";
|
out << prefix << "{\n";
|
||||||
out << prefix << "\t\"class\": \"NeuralNetwork::Neuron\",\n";
|
out << prefix << "\t\"class\": \"NeuralNetwork::Neuron\",\n";
|
||||||
out << prefix << "\t\"id\": " << id << ",\n";
|
out << prefix << "\t\"id\": " << id << ",\n";
|
||||||
out << prefix << "\t\"bias\": " << getBias() << ",\n";
|
|
||||||
out << prefix << "\t\"output\": " << output() << ",\n";
|
out << prefix << "\t\"output\": " << output() << ",\n";
|
||||||
out << prefix << "\t\"value\": " << value() << ",\n";
|
out << prefix << "\t\"value\": " << value() << ",\n";
|
||||||
out << prefix << "\t\"activationFunction\": " << activation->stringify() <<",\n";
|
out << prefix << "\t\"activationFunction\": " << activation->stringify() <<",\n";
|
||||||
|
|||||||
Reference in New Issue
Block a user