Beatmup
softmax.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include <vector>
21 #include "operation.h"
22 #include "../gpu/linear_mapping.h"
23 
24 
25 namespace Beatmup {
26  namespace NNets {
27  /**
28  Softmax layer.
29  It does not have output, but acts as a sink. The resulting probabilities are returned by getProbabilities().
30  This operation is executed on CPU.
31  */
32  class Softmax : public CpuOperation {
33  private:
34  std::vector<float> output;
35  std::vector<float> partialSums;
38 
39  int getAmountOfWork() const;
40  void beforeExecute(GraphicPipeline& gpu, const int threadCount);
41  void execute(const int sliceStart, const int sliceStop, const int threadIdx, const int threadCount);
42  void afterExecute(const int threadCount);
43 
44  public:
45  /**
46  Creates a softmax layer.
47  \param[in] name Operation name
48  */
49  Softmax(const std::string& name = "Softmax");
50 
51  inline int getInputCount() const { return 1; }
52  inline int getOutputCount() const { return 0; }
53 
54  inline bool acceptsStorageInput(int index = 0) const { return index == 0; }
55  inline bool acceptsVectorInput(int index = 0) const { return index == 0; }
56 
57  void setInput(Storage::View&& view, int index = 0);
58  void setInput(GL::Vector& vector, int index = 0);
59 
60  std::map<std::string, std::string> serialize() const;
61 
62  void disconnect();
63 
64  const std::vector<float>& getProbabilities() const { return output; }
65 
66  /**
67  Sets up deserialization of the operation.
68  */
69  static bool initDeserializer();
70  };
71 
72  /**
73  \internal
74  Being declared here, this variaable ensures Softmax::initDeserializer() is called with inclusion of this header file.
75  */
77  }
78 }
Real-valued vector usable by GPU.
Internal low-level GPU control API.
Definition: pipeline.h:33
Operation computed on CPU.
Definition: operation.h:434
Softmax layer.
Definition: softmax.h:32
static bool initDeserializer()
Sets up deserialization of the operation.
void beforeExecute(GraphicPipeline &gpu, const int threadCount)
Called right before the operation is executed.
Definition: softmax.cpp:34
std::vector< float > output
Definition: softmax.h:34
std::vector< float > partialSums
Definition: softmax.h:35
Softmax(const std::string &name="Softmax")
Creates a softmax layer.
Definition: softmax.cpp:26
Storage::View inputView
Definition: softmax.h:36
bool acceptsVectorInput(int index=0) const
Returns true if the operation can take a GL::Vector at a specific input.
Definition: softmax.h:55
bool acceptsStorageInput(int index=0) const
Returns true if the operation can take a Storage::View at a specific input.
Definition: softmax.h:54
void afterExecute(const int threadCount)
Called right after the operation is executed.
Definition: softmax.cpp:57
void execute(const int sliceStart, const int sliceStop, const int threadIdx, const int threadCount)
Executes the operation body within a specific CPU thread.
Definition: softmax.cpp:48
GL::Vector * inputVector
Definition: softmax.h:37
void disconnect()
Assigns empty inputs and outputs.
Definition: softmax.cpp:114
int getAmountOfWork() const
Returns amount of work in arbitrary units to be splitted among threads.
Definition: softmax.cpp:29
int getInputCount() const
Returns number of operation inputs.
Definition: softmax.h:51
const std::vector< float > & getProbabilities() const
Definition: softmax.h:64
int getOutputCount() const
Returns number of operation outputs.
Definition: softmax.h:52
void setInput(Storage::View &&view, int index=0)
Definition: softmax.cpp:70
std::map< std::string, std::string > serialize() const
Returns a serialized representation of th operation;.
Definition: softmax.cpp:106
Maps a 3D tensor onto a storage.
Definition: storage.h:308
static bool SOFTMAX_OP_DESERIALIZABLE
Definition: softmax.h:76
jlong jint index