clDNN
chapter_2.cpp
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
17 #include <../api/CPP/cldnn_defs.h>
18 #include <../api/CPP/engine.hpp>
19 #include <../api/CPP/input_layout.hpp>
20 #include <../api/CPP/activation.hpp>
21 #include <../api/CPP/softmax.hpp>
22 #include <../api/CPP/memory.hpp>
23 #include <../api/CPP/fully_connected.hpp>
24 #include <../api/CPP/data.hpp>
25 #include <../api/CPP/topology.hpp>
26 #include <iostream>
27 
28 #include "helper_functions.h"
29 
38 using namespace cldnn;
39 
40 topology chapter_2(engine& engine)
41 {
42  std::cout << std::endl << "-- Chapter 2 --" << std::endl;
43 
44  // The most trivial primitive is activation, lets start with that:
45  activation relu(
46  "relu", // primitive identifier
47  "input", // identifier of input ( output of primitive with provided name is input to current )
48  activation_relu);
49 
50  // Softmax is also very easy to create:
52  "softmax", // primitive identifier
53  "relu"); // relu will be input to softmax
54 
55  // Fully connected is little bit more complex.
56  // Need to create weights and biases, that are primitives with 'data' type (chapter 1).
57  // We will have fc layer with 3 inputs and 3 outputs. Weights have to be 3x3:
58  auto weights_mem = memory::allocate(engine, { data_types::f32,format::bfyx,{
59  3, // b - stands for size of the input
60  1, // ignored in fc
61  3, // x - stands for size of output ( number of neurons in fully connected layer )
62  1 } }); // y ignored
63  // Use function to fill data:
64  set_values(weights_mem, { 1.5f, 1.0f, 0.5f, -1.0f, 0.0f, 0.5f, 0.5f, -0.5f, -2.0f });
65  // Create data primitive.
66  data fc_weights("fc_weights", weights_mem);
67 
68  // Biases are optional but we can use those in this example. Create 'data' in the same way:
69  auto bias_mem = memory::allocate(engine, { data_types::f32,format::bfyx,{ spatial(3) } }); // y, b and f will be set to ones by default
70  // Use function to fill data:
71  set_values(bias_mem, { 0.0f, 1.0f, 0.5f });
72  // Create data primitive.
73  data fc_bias("fc_bias", bias_mem);
74 
75  // Now we are ready to create fc primitive.
76  fully_connected fc(
77  "fc", // primitive identifier
78  "softmax", // softmax will be input to fully connected
79  "fc_weights",// weigths identifier
80  "fc_bias" // bias identifier
81  );
82 
83  // Now we have 3 primitives created. Relation is defined by input->output setting. The only thing that we miss to create topology
84  // is input declaration. To declare input we need input_layout(chapter 1):
85  input_layout in_layout("input", layout(data_types::f32, format::bfyx, tensor(spatial(3))));
86  // Now, we are ready to put those into topology.
87  // Don't forget to put all data primitives inside.
89  in_layout,
90  softmax,
91  fc,
92  fc_bias,
93  fc_weights
94  );
95  // If you want to add another primitive to existing topology, you can use add method.
96  topology.add(relu);
97  // Take a look what is inside:
98  std::cout << "Topology contains:" << std::endl;
99  for (auto it : topology.get_primitive_ids())
100  {
101  std::cout << it << std::endl;
102  }
103  return topology;
104 }
Activation using rectified linear unit or parameterized rectified linear unit.
Definition: activation.hpp:39
Performs forward fully connected layer (inner product). Also supports built-in Relu cldnn_activation_...
N-dimensional vector. Mostly used to represent memory size.
Definition: tensor.hpp:256
Provides input data to topology.
Definition: data.hpp:36
Provides input layout for a data to be passed later to network.
Network topology to be defined by user.
Definition: topology.hpp:33
void add(PType const &desc)
Adds a primitive to topology.
Definition: topology.hpp:75
static memory allocate(const engine &engine, const layout &layout)
Allocate memory on engine using specified layout.
Definition: memory.hpp:50
the most common format for activations in clDNN.
Definition: tensor.hpp:81
Normalizes results so they sum to 1.
Definition: softmax.hpp:39
Represents clDNN engine object.
Definition: engine.hpp:110
Describes memory layout.
Definition: layout.hpp:223