clDNN
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
chapter_3.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/memory.hpp>
21 #include <../api/CPP/data.hpp>
22 #include <../api/CPP/topology.hpp>
23 #include <../api/CPP/network.hpp>
24 #include <iostream>
25 
26 #include "helper_functions.h"
27 
36 using namespace cldnn;
37 
38 
39 void chapter_3(engine& engine, topology& topology)
40 {
41  std::cout << std::endl << "-- Chapter 3 --" << std::endl;
42 
43  // Since we have topology and engine, we are ready to create network. Network is compiled graph/topology. During network creation
44  // all kernels are compiled and memory is allocated.
46  // We are almost ready to go. Need to create and set input for network:
47  memory input_mem = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 3, 1 } });
48  set_values(input_mem, { -3.0f, -2.0f, 2.5f });
49  // Set input
50  network.set_input_data("input", input_mem);
51  // Ready to go:
52  auto outputs = network.execute();
53 
54  for (auto it : outputs)
55  {
56  // Print primtive info for all outputs.
57  std::cout << network.get_primitive_info(it.first) << std::endl;
58  // OUTPUT:
59  // id: fc, type : fully connected
60  // input : softmax, count : 3, size : [b:1, f : 1, x : 3, y : 1]
61  // weights id : fc_weights, count : 9, bias id : fc_bias, count : 3
62  // with activation : false
63  // output padding lower size : [b:0, f : 0, x : 0, y : 0]
64  // output padding upper size : [b:0, f : 0, x : 0, y : 0]
65  // output : count : 3, size : [b:1, f : 1, x : 3, y : 1]
66 
67  auto mem_pointer = it.second.get_memory().pointer<float>();
68  for (auto i : mem_pointer)
69  {
70  std::cout << i << " ";
71  }
72  std::cout << std::endl;
73 
74  // As you probably noticed network output is a result of the last one primitive "fc". By the last one we mean, the one that
75  // is not input to any other primitive.
76  }
77 
78  // What if someone may want to look into intermediate results (hidden layers). This will be described in next chapter (4).
79 }
std::map< primitive_id, network_output > execute(const std::vector< event > &dependencies={}) const
Executes network and returns the list of network_output.
Definition: network.hpp:246
void set_input_data(const primitive_id &id, const memory &mem) const
Provides memory for input_layout primitives defined by user in source topology.
Definition: network.hpp:122
User selected list of program outputs.
Network topology to be defined by user.
Definition: topology.hpp:33
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
Executable network allocated from program.
Definition: network.hpp:59
Represents clDNN engine object.
Definition: engine.hpp:110
Represents buffer with particular layout.
Definition: memory.hpp:42