LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
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
 
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
 
std::string m_name
 

Detailed Description

Definition at line 212 of file keras_model.h.

Constructor & Destructor Documentation

keras::LayerDense::LayerDense ( )
inline

Definition at line 214 of file keras_model.h.

References fin.

214 : Layer("Dense") {}
Layer(std::string name)
Definition: keras_model.h:133

Member Function Documentation

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

Implements keras::Layer.

Definition at line 381 of file keras_model.cc.

References keras::DataChunk::get_1d(), keras::DataChunkFlat::get_1d_rw(), util::size(), and w.

382 {
383  //cout << "weights: input size " << m_weights.size() << endl;
384  //cout << "weights: neurons size " << m_weights[0].size() << endl;
385  //cout << "bias " << m_bias.size() << endl;
386  size_t size = m_weights[0].size();
387  size_t size8 = size >> 3;
388 
389  keras::DataChunkFlat* out = new DataChunkFlat(size, 0);
390  float* y_ret = out->get_1d_rw().data();
391 
392  auto const& im = dc->get_1d();
393 
394  for (size_t j = 0; j < m_weights.size(); ++j) { // iter over input
395  const float* w = m_weights[j].data();
396  float p = im[j];
397  size_t k = 0;
398  for (size_t i = 0; i < size8; ++i) { // iter over neurons
399  y_ret[k] += w[k] * p; // vectorize if you can
400  y_ret[k + 1] += w[k + 1] * p;
401  y_ret[k + 2] += w[k + 2] * p;
402  y_ret[k + 3] += w[k + 3] * p;
403  y_ret[k + 4] += w[k + 4] * p;
404  y_ret[k + 5] += w[k + 5] * p;
405  y_ret[k + 6] += w[k + 6] * p;
406  y_ret[k + 7] += w[k + 7] * p;
407  k += 8;
408  }
409  while (k < size) {
410  y_ret[k] += w[k] * p;
411  ++k;
412  }
413  }
414  for (size_t i = 0; i < size; ++i) { // add biases
415  y_ret[i] += m_bias[i];
416  }
417 
418  return out;
419 }
std::vector< std::vector< float > > m_weights
Definition: keras_model.h:218
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
std::vector< float > & get_1d_rw()
Definition: keras_model.h:111
std::vector< float > m_bias
Definition: keras_model.h:219
Float_t w
Definition: plot.C:20
virtual std::vector< float > const & get_1d() const
Definition: keras_model.h:48
virtual unsigned int keras::LayerDense::get_input_cols ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 222 of file keras_model.h.

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

Implements keras::Layer.

Definition at line 221 of file keras_model.h.

221 { return 1; } // flat, just one row
std::string keras::Layer::get_name ( )
inlineinherited

Definition at line 140 of file keras_model.h.

140 { return m_name; }
std::string m_name
Definition: keras_model.h:141
virtual unsigned int keras::LayerDense::get_output_units ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 223 of file keras_model.h.

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

Implements keras::Layer.

Definition at line 98 of file keras_model.cc.

References n.

99 {
100  fin >> m_input_cnt >> m_neurons;
101  float tmp_float;
102  char tmp_char = ' ';
103  for (int i = 0; i < m_input_cnt; ++i) {
104  vector<float> tmp_n;
105  fin >> tmp_char; // for '['
106  for (int n = 0; n < m_neurons; ++n) {
107  fin >> tmp_float;
108  tmp_n.push_back(tmp_float);
109  }
110  fin >> tmp_char; // for ']'
111  m_weights.push_back(tmp_n);
112  }
113  cout << "weights " << m_weights.size() << endl;
114  fin >> tmp_char; // for '['
115  for (int n = 0; n < m_neurons; ++n) {
116  fin >> tmp_float;
117  m_bias.push_back(tmp_float);
118  }
119  fin >> tmp_char; // for ']'
120  cout << "bias " << m_bias.size() << endl;
121 }
TString fin
Definition: Style.C:24
std::vector< std::vector< float > > m_weights
Definition: keras_model.h:218
std::vector< float > m_bias
Definition: keras_model.h:219
Char_t n[5]

Member Data Documentation

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

Definition at line 219 of file keras_model.h.

int keras::LayerDense::m_input_cnt

Definition at line 225 of file keras_model.h.

std::string keras::Layer::m_name
inherited

Definition at line 141 of file keras_model.h.

int keras::LayerDense::m_neurons

Definition at line 226 of file keras_model.h.

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

Definition at line 218 of file keras_model.h.


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