pytorch

Форк
0
/
tutorial_blob.cc 
89 строк · 2.5 Кб
1
/**
2
 * Copyright (c) 2016-present, Facebook, Inc.
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 "caffe2/core/blob.h"
18
#include "caffe2/core/init.h"
19
#include "caffe2/core/tensor.h"
20
#include "caffe2/core/logging.h"
21

22
// We will be lazy and just use the whole namespace.
23
using namespace caffe2;
24

25

26
int main(int argc, char** argv) {
27
  caffe2::GlobalInit(&argc, &argv);
28
  caffe2::ShowLogInfoToStderr();
29

30
  LOG(INFO) <<
31
      "This script corresponds to the Blob part of the Caffe2 C++ "
32
      "tutorial.";
33

34
  LOG(INFO) << "Let's create a blob myblob.";
35

36
  Blob myblob;
37

38
  LOG(INFO) << "Let's set it to int and set the value to 10.";
39

40
  int* myint = myblob.GetMutable<int>();
41
  *myint = 10;
42

43
  LOG(INFO)
44
      << "Is the blob type int? "
45
      << myblob.IsType<int>();
46

47
  LOG(INFO)
48
      << "Is the blob type float? "
49
      << myblob.IsType<float>();
50

51
  const int& myint_const = myblob.Get<int>();
52
  LOG(INFO)
53
      << "The value of the int number stored in the blob is: "
54
      << myint_const;
55

56
  LOG(INFO)
57
      << "Let's try to get a float pointer. This will trigger an exception.";
58

59
  try {
60
    const float& myfloat = myblob.Get<float>();
61
    LOG(FATAL) << "This line should never happen.";
62
  } catch (std::exception& e) {
63
    LOG(INFO)
64
        << "As expected, we got an exception. Its content says: "
65
        << e.what();
66
  }
67

68
  LOG(INFO) <<
69
      "However, we can change the content type (and destroy the old "
70
      "content) by calling GetMutable. Let's change it to double.";
71

72
  double* mydouble = myblob.GetMutable<double>();
73
  *mydouble = 3.14;
74

75
  LOG(INFO) << "The new content is: " << myblob.Get<double>();
76

77
  LOG(INFO) <<
78
      "If we have a pre-created object, we can use Reset() to transfer the "
79
      "object to a blob.";
80

81
  std::string* pvec = new std::string();
82
  myblob.Reset(pvec); // no need to release pvec, myblob takes ownership.
83

84
  LOG(INFO) << "Is the blob now of type string? "
85
            << myblob.IsType<std::string>();
86

87
  LOG(INFO) << "This concludes the blob tutorial.";
88
  return 0;
89
}
90

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.