Commit c5f4959c authored by Martin Beseda's avatar Martin Beseda

FIX: Fixed InvalidDimension exception + added dimension check to the add_data_pair() method.

parent 91d4fa62
......@@ -9,6 +9,10 @@
#include "DataSet.h"
InvalidDimension::InvalidDimension() : std::runtime_error("Invalid dimension specified!") {};
InvalidDimension::InvalidDimension(std::string msg) : std::runtime_error(msg.c_str()) {};
DataSet::DataSet(std::string file_path) {
std::ifstream ifs (file_path);
boost::archive::text_iarchive ia(ifs);
......@@ -19,11 +23,22 @@ DataSet::DataSet(std::string file_path) {
DataSet::DataSet(std::vector<std::pair<std::vector<double>, std::vector<double>>> *data_ptr) {
this->n_elements = data_ptr->size();
this->data = *data_ptr;
this->input_dim = std::get<0>(this->data[0]).size();
this->output_dim = std::get<1>(this->data[0]).size();
//TODO check the complete data set for input/output dimensions
}
void DataSet::add_data_pair(std::vector<double> inputs, std::vector<double> outputs) {
if(inputs.size() != this->input_dim) {
throw InvalidDimension("Bad input dimension.");
} else if(outputs.size() != this->output_dim) {
throw InvalidDimension("Bad output dimension.");
}
this->n_elements++;
this->data.push_back(std::make_pair(inputs, outputs));
this->data.emplace_back(std::make_pair(inputs, outputs));
}
std::vector<std::pair<std::vector<double>, std::vector<double>>>* DataSet::get_data() {
......@@ -34,6 +49,14 @@ size_t DataSet::get_n_elements() {
return this->n_elements;
}
unsigned int DataSet::get_input_dim() {
return this->input_dim;
}
unsigned int DataSet::get_output_dim() {
return this->output_dim;
}
void DataSet::print_data() {
if (n_elements) {
for (auto p : this->data) {
......
......@@ -9,6 +9,27 @@
#include <vector>
#include <boost/serialization/base_object.hpp>
#include <boost/range/size_type.hpp>
#include <exception>
#include <string>
/**
* Class representing an error caused by an incorrect
* input/output dimension specification
*/
class InvalidDimension: public std::runtime_error {
public:
/**
* Constructor with the general error message
*/
InvalidDimension();
/**
* Constructor with specific error message
* @param msg Specific error message
*/
explicit InvalidDimension(std::string msg);
};
/**
* Class representing data, which can be used for training
......@@ -23,6 +44,16 @@ private:
*/
size_t n_elements;
/**
* Dimension of the input
*/
unsigned int input_dim = 0;
/**
* Dimension of the output
*/
unsigned int output_dim = 0;
/**
* Stored data in the format of pairs of corresponding
* input and output vectors
......@@ -43,23 +74,29 @@ protected:
ar & this->n_elements;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_tmp;
unsigned int v_size; // Size of the vector which is going to be loaded
double tmp;
/* INPUT dimension */
size_t input_dim;
ar & input_dim;
/* OUTPUT dimension */
size_t output_dim;
ar & output_dim;
for(unsigned int i=0; i < this->n_elements; i++) {
/* INPUT vector */;
ar & v_size;
/* INPUT vector */
std::vector<double> inputs;
for(unsigned int j=0; j < v_size; j++) {
for(unsigned int j=0; j < input_dim; j++) {
ar & tmp;
inputs.push_back(tmp);
}
/* OUTPUT vector */
ar & v_size;
//TODO check vector dimension like in input
std::vector<double> outputs;
for(unsigned int j=0; j < v_size; j++) {
for(unsigned int j=0; j < output_dim; j++) {
ar & tmp;
outputs.push_back(tmp);
}
......@@ -71,19 +108,25 @@ protected:
} else {
/* STORING data */
//TODO check stored vectors dimensions
ar & this->n_elements;
size_t size;
size_t dim_inp, dim_out;
/* INPUT dimension */
dim_inp = std::get<0>(this->data[0]).size();
ar & dim_inp;
/* OUTPUT dimension */
dim_out = std::get<1>(this->data[0]).size();
ar & dim_out;
for(const auto p : this->data) {
/* Input vector */
size = std::get<0>(p).size();
ar & size;
for(auto val : std::get<0>(p)) {
ar & val;
}
/* Output vector */
size = std::get<1>(p).size();
ar & size;
for(auto val : std::get<1>(p)) {
ar & val;
}
......@@ -111,6 +154,19 @@ public:
*/
size_t get_n_elements();
/**
* Returns the input dimension
* @return Input dimension
*/
unsigned int get_input_dim();
/**
* Return the output dimension
* @return Output dimension
*/
unsigned int get_output_dim();
/**
* Getter for the data structure
* @return Vector of data
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment