Public Member Functions | Public Attributes | List of all members
keras::LayerDense Class Reference

#include <keras_model.h>

Inheritance diagram for keras::LayerDense:
keras::Layer

Public Member Functions

 LayerDense ()
 
void load_weights (std::ifstream &fin)
 
keras::DataChunkcompute_output (keras::DataChunk *)
 
virtual unsigned int get_input_rows () const
 
virtual unsigned int get_input_cols () const
 
virtual unsigned int get_output_units () const
 
- Public Member Functions inherited from keras::Layer
 Layer (std::string name)
 
virtual ~Layer ()
 
std::string get_name ()
 

Public Attributes

std::vector< std::vector< float > > m_weights
 
std::vector< float > m_bias
 
int m_input_cnt
 
int m_neurons
 
- Public Attributes inherited from keras::Layer
std::string m_name
 

Detailed Description

Definition at line 197 of file keras_model.h.

Constructor & Destructor Documentation

keras::LayerDense::LayerDense ( )
inline

Definition at line 199 of file keras_model.h.

199 : Layer("Dense") {}
Layer(std::string name)
Definition: keras_model.h:124

Member Function Documentation

keras::DataChunk * keras::LayerDense::compute_output ( keras::DataChunk dc)
virtual

Implements keras::Layer.

Definition at line 363 of file keras_model.cc.

363  {
364  //cout << "weights: input size " << m_weights.size() << endl;
365  //cout << "weights: neurons size " << m_weights[0].size() << endl;
366  //cout << "bias " << m_bias.size() << endl;
367  size_t size = m_weights[0].size();
368  size_t size8 = size >> 3;
369 
370  keras::DataChunkFlat *out = new DataChunkFlat(size, 0);
371  float * y_ret = out->get_1d_rw().data();
372 
373  auto const & im = dc->get_1d();
374 
375  for (size_t j = 0; j < m_weights.size(); ++j) { // iter over input
376  const float * w = m_weights[j].data();
377  float p = im[j];
378  size_t k = 0;
379  for (size_t i = 0; i < size8; ++i) { // iter over neurons
380  y_ret[k] += w[k] * p; // vectorize if you can
381  y_ret[k+1] += w[k+1] * p;
382  y_ret[k+2] += w[k+2] * p;
383  y_ret[k+3] += w[k+3] * p;
384  y_ret[k+4] += w[k+4] * p;
385  y_ret[k+5] += w[k+5] * p;
386  y_ret[k+6] += w[k+6] * p;
387  y_ret[k+7] += w[k+7] * p;
388  k += 8;
389  }
390  while (k < size) { y_ret[k] += w[k] * p; ++k; }
391  }
392  for (size_t i = 0; i < size; ++i) { // add biases
393  y_ret[i] += m_bias[i];
394  }
395 
396  return out;
397 }
std::vector< std::vector< float > > m_weights
Definition: keras_model.h:203
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
p
Definition: test.py:223
std::vector< float > & get_1d_rw()
Definition: keras_model.h:102
std::vector< float > m_bias
Definition: keras_model.h:204
virtual std::vector< float > const & get_1d() const
Definition: keras_model.h:45
virtual unsigned int keras::LayerDense::get_input_cols ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 207 of file keras_model.h.

207 { return m_input_cnt; }
virtual unsigned int keras::LayerDense::get_input_rows ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 206 of file keras_model.h.

206 { return 1; } // flat, just one row
virtual unsigned int keras::LayerDense::get_output_units ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 208 of file keras_model.h.

208 { return m_neurons; }
void keras::LayerDense::load_weights ( std::ifstream &  fin)
virtual

Implements keras::Layer.

Definition at line 91 of file keras_model.cc.

91  {
92  fin >> m_input_cnt >> m_neurons;
93  float tmp_float;
94  char tmp_char = ' ';
95  for(int i = 0; i < m_input_cnt; ++i) {
96  vector<float> tmp_n;
97  fin >> tmp_char; // for '['
98  for(int n = 0; n < m_neurons; ++n) {
99  fin >> tmp_float;
100  tmp_n.push_back(tmp_float);
101  }
102  fin >> tmp_char; // for ']'
103  m_weights.push_back(tmp_n);
104  }
105  cout << "weights " << m_weights.size() << endl;
106  fin >> tmp_char; // for '['
107  for(int n = 0; n < m_neurons; ++n) {
108  fin >> tmp_float;
109  m_bias.push_back(tmp_float);
110  }
111  fin >> tmp_char; // for ']'
112  cout << "bias " << m_bias.size() << endl;
113 
114 }
std::vector< std::vector< float > > m_weights
Definition: keras_model.h:203
std::void_t< T > n
std::vector< float > m_bias
Definition: keras_model.h:204
QTextStream & endl(QTextStream &s)

Member Data Documentation

std::vector<float> keras::LayerDense::m_bias

Definition at line 204 of file keras_model.h.

int keras::LayerDense::m_input_cnt

Definition at line 210 of file keras_model.h.

int keras::LayerDense::m_neurons

Definition at line 211 of file keras_model.h.

std::vector<std::vector<float> > keras::LayerDense::m_weights

Definition at line 203 of file keras_model.h.


The documentation for this class was generated from the following files: