42 void pol2cart(
const T phi,
const T r, T& x, T& y)
55 void pol2cart(
const arma::Mat<T> phi,
const arma::Mat<T> r, arma::Mat<T>& x, arma::Mat<T>& y)
69 void cart2pol(
const T x,
const T y, T& phi, T& r)
83 void cart2pol(
const arma::Mat<T> x,
const arma::Mat<T> y, arma::Mat<T>& phi, arma::Mat<T>& r)
97 arma_inline arma::Mat<DNN_Dtype>
row2col(
const arma::Mat<DNN_Dtype>& A)
99 arma::Mat<DNN_Dtype> B=arma::vectorise(A,1);
110 arma_inline arma::Mat<DNN_Dtype>
col2col(
const arma::Mat<DNN_Dtype>& A)
112 return arma::vectorise(A);
125 arma_inline arma::Mat<DNN_Dtype>
mat2mat(
const arma::Mat<DNN_Dtype>& A,
const arma::uword rows,
const arma::uword cols)
127 arma::Mat<DNN_Dtype> B(A.memptr(), rows, cols);
142 arma_inline arma::Cube<DNN_Dtype>
mat2cube(
const arma::Mat<DNN_Dtype>& A,
const arma::uword rows,
const arma::uword cols,
const arma::uword slices)
144 arma::Cube<DNN_Dtype> B(A.memptr(), rows, cols, slices);
158 arma_inline arma::Mat<DNN_Dtype>
cube2mat(arma::Cube<DNN_Dtype>& A,
const arma::uword rows,
const arma::uword cols)
160 arma::Mat<DNN_Dtype> B(A.memptr(),rows,cols);
179 void gen_spiral(arma::Mat<DNN_Dtype> &X,arma::Mat<DNN_Dtype> &T, arma::uword N, arma::uword K,
180 const double ph_var = 0.1,
const double r_min = 0.1,
181 const double ph_max = 7)
185 arma::uvec Tk = arma::randi<arma::uvec>(N,arma::distr_param(1,(
int)K));
186 for(arma::uword n=0; n<N; n++)
189 double r = r_min+ arma::randu()*(1-r_min);
190 double ph = r*ph_max+ ph_var*arma::randn()+ Tk(n)*2.0*arma::datum::pi/K;
192 T(Tk(n)-1,n) =
static_cast<DNN_Dtype>(1.0);
199 void scat_plot(arma::Mat<DNN_Dtype>& _X,arma::Mat<DNN_Dtype>& _T,
int f1,
int f2)
202 std::ostringstream tmp_s;
203 double xmin,xmax,ymin,ymax;
204 xmin = _X.row(f1).min();
205 xmax = _X.row(f1).max();
206 ymin = _X.row(f2).min();
207 ymax = _X.row(f2).max();
209 gp0.window(
"Plot", 10, 10, 600, 600);
213 for(arma::uword c = 0; c<_T.n_rows; c++)
215 arma::Mat<DNN_Dtype> XC = _X.cols(arma::find(_T.row(c)>0.9));
216 arma::Row<DNN_Dtype> x = XC.row(f1);
217 arma::Row<DNN_Dtype> y = XC.row(f2);
218 gp0.plot_add(x,y,
" ",
"points ");
229 rev_i += (i & 0xff) << 24;
230 rev_i += ((i >> 8) & 0xff) << 16;
231 rev_i += ((i >> 16) & 0xff) << 8;
232 rev_i += ((i >> 24) & 0xff);
246 bool read_idx3(std::string fname, arma::Cube<DNN_Dtype>& img,
const double frac=1.0)
248 uint32_t magic_number = 0;
249 uint32_t number_of_images = 0;
250 uint32_t number_of_rows = 0;
251 uint32_t number_of_cols = 0;
254 ifs.open(fname.c_str(), std::ios::binary);
257 std::cout <<
"Could not open " << fname << std::endl;
263 ifs.seekg(0, std::ios::beg);
264 ifs.read(reinterpret_cast<char*>(&magic_number),
sizeof(magic_number));
266 ifs.read(reinterpret_cast< char*>(&number_of_images),
sizeof(number_of_images));
268 ifs.read(reinterpret_cast<char*>(&number_of_rows),
sizeof(number_of_rows));
270 ifs.read(reinterpret_cast<char*>(&number_of_cols),
sizeof(number_of_cols));
273 arma::uword red_nr =
static_cast<arma::uword
>(floor(frac*number_of_images));
275 img.set_size(number_of_rows, number_of_cols, red_nr);
278 for (arma::uword i = 0; i < red_nr; ++i)
280 for (arma::uword r = 0; r < number_of_rows; ++r)
282 for (arma::uword c = 0; c < number_of_cols; ++c)
284 unsigned char tmp = 0;
285 ifs.read((
char*)&tmp,
sizeof(tmp));
305 bool read_idx1(std::string fname, arma::ivec& label,
const double frac=1.0)
308 ifs.open(fname.c_str(), std::ifstream::binary);
311 std::cout <<
"Could not open " << fname << std::endl;
316 uint32_t magic_number = 0;
317 uint32_t number_of_items = 0;
320 ifs.read(reinterpret_cast<char*>(&magic_number), 4);
322 ifs.read(reinterpret_cast<char*>(&number_of_items),4);
326 arma::uword red_nr =
static_cast<arma::uword
>(floor(frac*number_of_items));
328 label.set_size(red_nr);
329 for (arma::uword i = 0; i < red_nr; ++i)
331 unsigned char tmp = 0;
332 ifs.read((
char*)&tmp,
sizeof(tmp));
333 label(i) = (
unsigned int)tmp;
352 void read_MNIST(std::string fname_X, std::string fname_T, arma::Mat<DNN_Dtype>& X, arma::Mat<DNN_Dtype>& T,
double frac=1.0)
354 arma::Cube<DNN_Dtype> img;
360 X.set_size(label.n_elem,img.n_elem_slice);
362 T.set_size(label.n_elem,10);
364 for(arma::uword n=0; n<label.n_elem; n++)
367 arma::Mat<DNN_Dtype> xx = arma::vectorise(img.slice(n)).t();
371 arma::inplace_trans(X);
372 arma::inplace_trans(T);
384 const int length = 20;
385 const int update = 100/length;
386 if(
int(p*100)%update==0)
388 std::cout << str <<
" [";
389 int pos =
static_cast<int>(length * p);
390 for (
int i = 0; i < length; ++i)
399 std::cout <<
"] " << int(p*100)+update <<
" %\r";
412 void progress_bar(
const std::string str, arma::uword num, arma::uword maxnum)
426 std::cout <<
"\r \r";
428 std::cout <<
"\33[2K";
void cart2pol(const T x, const T y, T &phi, T &r)
Cartesian to polar convertion.
arma_inline arma::Mat< DNN_Dtype > row2col(const arma::Mat< DNN_Dtype > &A)
Flattens a matrix row-wise to a one column matrix.
void remove_progress_bar(void)
Clears progress bar.
#define DNN_VERSION_MAJOR
arma_inline arma::Mat< DNN_Dtype > mat2mat(const arma::Mat< DNN_Dtype > &A, const arma::uword rows, const arma::uword cols)
Reshapes a matrix.
#define DNN_VERSION_PATCH
arma_inline arma::Mat< DNN_Dtype > cube2mat(arma::Cube< DNN_Dtype > &A, const arma::uword rows, const arma::uword cols)
Converts a cube to a matrix.
void gen_spiral(arma::Mat< DNN_Dtype > &X, arma::Mat< DNN_Dtype > &T, arma::uword N, arma::uword K, const double ph_var=0.1, const double r_min=0.1, const double ph_max=7)
Dataset generator: a rotating spiral with K classes.
float DNN_Dtype
Data type used in the network (float or double)
bool read_idx1(std::string fname, arma::ivec &label, const double frac=1.0)
Reads a IDX1 file.
#define DNN_VERSION_MINOR
arma_inline arma::Cube< DNN_Dtype > mat2cube(const arma::Mat< DNN_Dtype > &A, const arma::uword rows, const arma::uword cols, const arma::uword slices)
Converts a matrix to a cube.
uint32_t swap_endian(uint32_t i)
Endian converter for IDX1 and IDX3 file.
std::string version_info(void)
Generate DaNNet version string.
void progress_bar(const std::string str, double p)
Console progress bar.
void read_MNIST(std::string fname_X, std::string fname_T, arma::Mat< DNN_Dtype > &X, arma::Mat< DNN_Dtype > &T, double frac=1.0)
Reads a MNIST digit dataset.
arma_inline arma::Mat< DNN_Dtype > col2col(const arma::Mat< DNN_Dtype > &A)
Flattens a matrix col-wise to a one column matrix.
void pol2cart(const T phi, const T r, T &x, T &y)
Polar to cartesian convertion.
bool read_idx3(std::string fname, arma::Cube< DNN_Dtype > &img, const double frac=1.0)
Reads a IDX3 file.