Apollo 11.0
自动驾驶开放平台
apollo::prediction::network::LSTM类 参考

Long-Short Term Memory unit - Hochreiter 1997. 更多...

#include <net_layer.h>

类 apollo::prediction::network::LSTM 继承关系图:
apollo::prediction::network::LSTM 的协作图:

Public 成员函数

bool Load (const apollo::prediction::LayerParameter &layer_pb) override
 Load the layer parameter from a pb message
 
void Run (const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
 Compute the layer output from inputs
 
void ResetState () override
 Reset the internal state and memory cell state as zero-matrix
 
void SetState (const std::vector< Eigen::MatrixXf > &states) override
 Set the internal state and memory cell state
 
void State (std::vector< Eigen::MatrixXf > *states) const override
 Access to the internal state and memory cell state
 
- Public 成员函数 继承自 apollo::prediction::network::Layer
 Layer ()=default
 Constructor
 
virtual ~Layer ()=default
 Destructor
 
std::string Name () const
 Name of a layer
 
int OrderNumber () const
 Order number of a layer in a network
 

详细描述

Long-Short Term Memory unit - Hochreiter 1997.

For a step-by-step description of the algorithm, see this tutorial.

在文件 net_layer.h322 行定义.

成员函数说明

◆ Load()

bool apollo::prediction::network::LSTM::Load ( const apollo::prediction::LayerParameter &  layer_pb)
overridevirtual

Load the layer parameter from a pb message

参数
Apb message contains the parameters
返回
True is loaded successively, otherwise False

重载 apollo::prediction::network::Layer .

在文件 net_layer.cc319 行定义.

319 {
320 if (!Layer::Load(layer_pb)) {
321 AERROR << "Fail to Load the layer parameters!";
322 return false;
323 }
324 LSTMParameter lstm_pb = layer_pb.lstm();
325 if (!lstm_pb.has_units()) {
326 ADEBUG << "Fail to Load the number of units.";
327 return false;
328 } else {
329 units_ = lstm_pb.units();
330 }
331 if (!lstm_pb.has_return_sequences()) {
332 ADEBUG << "Set return_sequences at default.";
333 return_sequences_ = false;
334 } else {
335 return_sequences_ = lstm_pb.return_sequences();
336 }
337 if (!lstm_pb.has_stateful()) {
338 ADEBUG << "Set stateful at default.";
339 stateful_ = false;
340 } else {
341 stateful_ = lstm_pb.stateful();
342 }
343 if (!lstm_pb.has_activation()) {
344 ADEBUG << "Set activation function as tanh.";
345 kactivation_ = serialize_to_function("tanh");
346 } else {
347 kactivation_ = serialize_to_function(lstm_pb.activation());
348 }
349 if (!lstm_pb.has_recurrent_activation()) {
350 ADEBUG << "Set recurrent_activation function as hard_tanh.";
351 krecurrent_activation_ = serialize_to_function("hard_tanh");
352 } else {
353 krecurrent_activation_ =
354 serialize_to_function(lstm_pb.recurrent_activation());
355 }
356 if (!lstm_pb.has_use_bias()) {
357 ADEBUG << "Set use_bias as true.";
358 use_bias_ = true;
359 } else {
360 use_bias_ = lstm_pb.use_bias();
361 }
362 if (!lstm_pb.has_unit_forget_bias()) {
363 ADEBUG << "Set unit forget bias as true.";
364 unit_forget_bias_ = true;
365 } else {
366 unit_forget_bias_ = lstm_pb.unit_forget_bias();
367 }
368 if (!lstm_pb.has_weights_input() ||
369 !LoadTensor(lstm_pb.weights_input(), &wi_)) {
370 AERROR << "Fail to Load input weights!";
371 return false;
372 }
373 if (!lstm_pb.has_weights_forget() ||
374 !LoadTensor(lstm_pb.weights_forget(), &wf_)) {
375 AERROR << "Fail to Load forget weights!";
376 return false;
377 }
378 if (!lstm_pb.has_weights_cell() ||
379 !LoadTensor(lstm_pb.weights_cell(), &wc_)) {
380 AERROR << "Fail to Load cell weights!";
381 return false;
382 }
383 if (!lstm_pb.has_weights_output() ||
384 !LoadTensor(lstm_pb.weights_output(), &wo_)) {
385 AERROR << "Fail to Load output weights!";
386 return false;
387 }
388 if (!lstm_pb.has_bias_input() || !LoadTensor(lstm_pb.bias_input(), &bi_)) {
389 AERROR << "Fail to Load input bias!";
390 return false;
391 }
392 if (!lstm_pb.has_bias_forget() || !LoadTensor(lstm_pb.bias_forget(), &bf_)) {
393 AERROR << "Fail to Load forget bias!";
394 return false;
395 }
396 if (!lstm_pb.has_bias_cell() || !LoadTensor(lstm_pb.bias_cell(), &bc_)) {
397 AERROR << "Fail to Load cell bias!";
398 return false;
399 }
400 if (!lstm_pb.has_bias_output() || !LoadTensor(lstm_pb.bias_output(), &bo_)) {
401 AERROR << "Fail to Load output bias!";
402 return false;
403 }
404 if (!lstm_pb.has_recurrent_weights_input() ||
405 !LoadTensor(lstm_pb.recurrent_weights_input(), &r_wi_)) {
406 AERROR << "Fail to Load recurrent input weights!";
407 return false;
408 }
409 if (!lstm_pb.has_recurrent_weights_forget() ||
410 !LoadTensor(lstm_pb.recurrent_weights_forget(), &r_wf_)) {
411 AERROR << "Fail to Load recurrent forget weights!";
412 return false;
413 }
414 if (!lstm_pb.has_recurrent_weights_cell() ||
415 !LoadTensor(lstm_pb.recurrent_weights_cell(), &r_wc_)) {
416 AERROR << "Fail to Load recurrent cell weights!";
417 return false;
418 }
419 if (!lstm_pb.has_recurrent_weights_output() ||
420 !LoadTensor(lstm_pb.recurrent_weights_output(), &r_wo_)) {
421 AERROR << "Fail to Load recurrent output weights!";
422 return false;
423 }
424 ResetState();
425 return true;
426}
void ResetState() override
Reset the internal state and memory cell state as zero-matrix
Definition net_layer.cc:464
virtual bool Load(const apollo::prediction::LayerParameter &layer_pb)
Load layer parameters from a protobuf message
Definition net_layer.cc:32
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
std::function< float(float)> serialize_to_function(const std::string &str)
translate a string into a network activation function
Definition net_util.cc:56
bool LoadTensor(const TensorParameter &tensor_pb, Eigen::MatrixXf *matrix)
load matrix value from a protobuf message
Definition net_util.cc:66

◆ ResetState()

void apollo::prediction::network::LSTM::ResetState ( )
overridevirtual

Reset the internal state and memory cell state as zero-matrix

重载 apollo::prediction::network::Layer .

在文件 net_layer.cc464 行定义.

464 {
465 ht_1_.resize(1, units_);
466 ct_1_.resize(1, units_);
467 ht_1_.fill(0.0);
468 ct_1_.fill(0.0);
469}

◆ Run()

void apollo::prediction::network::LSTM::Run ( const std::vector< Eigen::MatrixXf > &  inputs,
Eigen::MatrixXf *  output 
)
overridevirtual

Compute the layer output from inputs

参数
Inputsto a network layer
Outputof a network layer will be returned

实现了 apollo::prediction::network::Layer.

在文件 net_layer.cc448 行定义.

449 {
450 CHECK_EQ(inputs.size(), 1U);
451 Eigen::MatrixXf sequences(inputs[0].rows(), units_);
452 Eigen::MatrixXf temp;
453 for (int i = 0; i < inputs[0].rows(); ++i) {
454 Step(inputs[0].row(i), &temp, &ht_1_, &ct_1_);
455 sequences.row(i) = temp.row(0);
456 }
457 if (return_sequences_) {
458 *output = sequences;
459 } else {
460 *output = temp.row(0);
461 }
462}

◆ SetState()

void apollo::prediction::network::LSTM::SetState ( const std::vector< Eigen::MatrixXf > &  states)
overridevirtual

Set the internal state and memory cell state

参数
Avector of Eigen::MatrixXf

重载 apollo::prediction::network::Layer .

在文件 net_layer.cc477 行定义.

477 {
478 CHECK_EQ(states.size(), 2U);
479 CHECK_EQ(states[0].rows(), 1);
480 CHECK_EQ(states[1].rows(), 1);
481 CHECK_EQ(states[0].cols(), units_);
482 CHECK_EQ(states[1].cols(), units_);
483 ht_1_ = states[0];
484 ct_1_ = states[1];
485}

◆ State()

void apollo::prediction::network::LSTM::State ( std::vector< Eigen::MatrixXf > *  states) const
overridevirtual

Access to the internal state and memory cell state

返回
State in a vector of Eigen::MatrixXf

重载 apollo::prediction::network::Layer .

在文件 net_layer.cc471 行定义.

471 {
472 states->resize(2);
473 states->at(0) = ht_1_;
474 states->at(1) = ct_1_;
475}

该类的文档由以下文件生成: