Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.15.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Tensor.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <algorithm>
30 #include <cstddef>
31 #include <memory>
32 #include <string>
33 #include <type_traits>
34 
35 #include "open3d/core/Blob.h"
36 #include "open3d/core/DLPack.h"
37 #include "open3d/core/Device.h"
38 #include "open3d/core/Dtype.h"
39 #include "open3d/core/Scalar.h"
40 #include "open3d/core/ShapeUtil.h"
41 #include "open3d/core/SizeVector.h"
43 #include "open3d/core/TensorInit.h"
44 #include "open3d/core/TensorKey.h"
45 
46 namespace open3d {
47 namespace core {
48 
51 class Tensor {
52 public:
53  Tensor() {}
54 
56  Tensor(const SizeVector& shape,
57  Dtype dtype,
58  const Device& device = Device("CPU:0"))
59  : shape_(shape),
60  strides_(shape_util::DefaultStrides(shape)),
61  dtype_(dtype),
62  blob_(std::make_shared<Blob>(shape.NumElements() * dtype.ByteSize(),
63  device)) {
64  data_ptr_ = blob_->GetDataPtr();
65  }
66 
68  template <typename T>
69  Tensor(const std::vector<T>& init_vals,
70  const SizeVector& shape,
71  Dtype dtype,
72  const Device& device = Device("CPU:0"))
73  : Tensor(shape, dtype, device) {
74  // Check number of elements
75 
76  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
78  "Tensor initialization values' size {} does not match the "
79  "shape {}",
80  init_vals.size(), shape_.NumElements());
81  }
82 
83  // Check data types
84  AssertTemplateDtype<T>();
85  if (!std::is_pod<T>()) {
86  utility::LogError("Object must be a POD.");
87  }
88 
89  // Copy data to blob
91  init_vals.data(),
92  init_vals.size() * dtype.ByteSize());
93  }
94 
96  template <typename T>
97  Tensor(const T* init_vals,
98  const SizeVector& shape,
99  Dtype dtype,
100  const Device& device = Device("CPU:0"))
101  : Tensor(shape, dtype, device) {
102  // Check data types
103  AssertTemplateDtype<T>();
104 
105  // Copy data to blob
107  init_vals,
108  shape_.NumElements() * dtype.ByteSize());
109  }
110 
114  Tensor(const SizeVector& shape,
115  const SizeVector& strides,
116  void* data_ptr,
117  Dtype dtype,
118  const std::shared_ptr<Blob>& blob)
119  : shape_(shape),
120  strides_(strides),
121  data_ptr_(data_ptr),
122  dtype_(dtype),
123  blob_(blob) {}
124 
145  Tensor(void* data_ptr,
146  Dtype dtype,
147  const SizeVector& shape,
148  const SizeVector& strides = {},
149  const Device& device = Device("CPU:0"))
150  : shape_(shape), strides_(strides), data_ptr_(data_ptr), dtype_(dtype) {
151  if (strides_.empty()) {
153  }
154  // Blob with no-op deleter.
155  blob_ = std::make_shared<Blob>(device, (void*)data_ptr_, [](void*) {});
156  }
157 
160  Tensor(const Tensor& other) = default;
161 
164  Tensor(Tensor&& other) = default;
165 
168  Tensor& operator=(const Tensor& other) &;
169 
172  Tensor& operator=(Tensor&& other) &;
173 
176  Tensor& operator=(const Tensor& other) &&;
177 
180  Tensor& operator=(Tensor&& other) &&;
181 
187  template <typename T>
188  Tensor& operator=(const T v) && {
189  this->Fill(v);
190  return *this;
191  }
192 
196  template <typename Object>
197  Tensor& AssignObject(const Object& v) && {
198  if (shape_.size() != 0) {
200  "Assignment with scalar only works for scalar Tensor of "
201  "shape ()");
202  }
203  AssertTemplateDtype<Object>();
205  sizeof(Object));
206  return *this;
207  }
208 
211  template <typename S>
212  void Fill(S v);
213 
214  template <typename Object>
215  void FillObject(const Object& v);
216 
218  static Tensor Empty(const SizeVector& shape,
219  Dtype dtype,
220  const Device& device = Device("CPU:0"));
221 
224  static Tensor EmptyLike(const Tensor& other) {
225  return Tensor::Empty(other.shape_, other.dtype_, other.GetDevice());
226  }
227 
229  template <typename T>
230  static Tensor Full(const SizeVector& shape,
231  T fill_value,
232  Dtype dtype,
233  const Device& device = Device("CPU:0")) {
234  Tensor t = Empty(shape, dtype, device);
235  t.Fill(fill_value);
236  return t;
237  }
238 
240  static Tensor Zeros(const SizeVector& shape,
241  Dtype dtype,
242  const Device& device = Device("CPU:0"));
243 
245  static Tensor Ones(const SizeVector& shape,
246  Dtype dtype,
247  const Device& device = Device("CPU:0"));
248 
251  template <typename T>
252  static Tensor Init(const T val, const Device& device = Device("CPU:0")) {
253  Dtype type = Dtype::FromType<T>();
254  std::vector<T> ele_list{val};
255  SizeVector shape;
256  return Tensor(ele_list, shape, type, device);
257  }
258 
261  template <typename T>
262  static Tensor Init(const std::initializer_list<T>& in_list,
263  const Device& device = Device("CPU:0")) {
264  return InitWithInitializerList<T, 1>(in_list, device);
265  }
266 
269  template <typename T>
270  static Tensor Init(
271  const std::initializer_list<std::initializer_list<T>>& in_list,
272  const Device& device = Device("CPU:0")) {
273  return InitWithInitializerList<T, 2>(in_list, device);
274  }
275 
278  template <typename T>
279  static Tensor Init(
280  const std::initializer_list<
281  std::initializer_list<std::initializer_list<T>>>& in_list,
282  const Device& device = Device("CPU:0")) {
283  return InitWithInitializerList<T, 3>(in_list, device);
284  }
285 
287  static Tensor Eye(int64_t n, Dtype dtype, const Device& device);
288 
290  static Tensor Diag(const Tensor& input);
291 
293  static Tensor Arange(const Scalar start,
294  const Scalar stop,
295  const Scalar step = 1,
296  const Dtype dtype = core::Int64,
297  const Device& device = core::Device("CPU:0"));
298 
300  Tensor Reverse() const;
301 
321  Tensor GetItem(const TensorKey& tk) const;
322 
341  Tensor GetItem(const std::vector<TensorKey>& tks) const;
342 
344  Tensor SetItem(const Tensor& value);
345 
361  Tensor SetItem(const TensorKey& tk, const Tensor& value);
362 
377  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
378 
409  Tensor Append(
410  const Tensor& other,
411  const utility::optional<int64_t>& axis = utility::nullopt) const;
412 
414  Tensor Broadcast(const SizeVector& dst_shape) const;
415 
420  Tensor Expand(const SizeVector& dst_shape) const;
421 
434  Tensor Reshape(const SizeVector& dst_shape) const;
435 
456  Tensor Flatten(int64_t start_dim = 0, int64_t end_dim = -1) const;
457 
476  Tensor View(const SizeVector& dst_shape) const;
477 
479  Tensor Clone() const { return To(GetDevice(), /*copy=*/true); }
480 
482  void CopyFrom(const Tensor& other);
483 
488  Tensor To(Dtype dtype, bool copy = false) const;
489 
494  Tensor To(const Device& device, bool copy = false) const;
495 
502  Tensor To(const Device& device, Dtype dtype, bool copy = false) const;
503 
504  std::string ToString(bool with_suffix = true,
505  const std::string& indent = "") const;
506 
508  Tensor operator[](int64_t i) const;
509 
512  Tensor IndexExtract(int64_t dim, int64_t idx) const;
513 
520  Tensor Slice(int64_t dim,
521  int64_t start,
522  int64_t stop,
523  int64_t step = 1) const;
524 
535  Tensor AsRvalue() { return *this; }
536 
538  const Tensor AsRvalue() const { return *this; }
539 
544  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
545 
553  void IndexSet(const std::vector<Tensor>& index_tensors,
554  const Tensor& src_tensor);
555 
560  Tensor Permute(const SizeVector& dims) const;
561 
564  Tensor AsStrided(const SizeVector& new_shape,
565  const SizeVector& new_strides) const;
566 
571  Tensor Transpose(int64_t dim0, int64_t dim1) const;
572 
576  Tensor T() const;
577 
580  double Det() const;
581 
584  template <typename T>
585  T Item() const {
586  if (shape_.NumElements() != 1) {
588  "Tensor::Item() only works for Tensor with exactly one "
589  "element.");
590  }
591  AssertTemplateDtype<T>();
592  T value;
593  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
594  return value;
595  }
596 
598  Tensor Add(const Tensor& value) const;
599  Tensor Add(Scalar value) const;
600  Tensor operator+(const Tensor& value) const { return Add(value); }
601  Tensor operator+(Scalar value) const { return Add(value); }
602 
605  Tensor Add_(const Tensor& value);
606  Tensor Add_(Scalar value);
607  Tensor operator+=(const Tensor& value) { return Add_(value); }
608  Tensor operator+=(Scalar value) { return Add_(value); }
609 
611  Tensor Sub(const Tensor& value) const;
612  Tensor Sub(Scalar value) const;
613  Tensor operator-(const Tensor& value) const { return Sub(value); }
614  Tensor operator-(Scalar value) const { return Sub(value); }
615 
618  Tensor Sub_(const Tensor& value);
619  Tensor Sub_(Scalar value);
620  Tensor operator-=(const Tensor& value) { return Sub_(value); }
621  Tensor operator-=(Scalar value) { return Sub_(value); }
622 
624  Tensor Mul(const Tensor& value) const;
625  Tensor Mul(Scalar value) const;
626  Tensor operator*(const Tensor& value) const { return Mul(value); }
627  Tensor operator*(Scalar value) const { return Mul(value); }
628 
631  Tensor Mul_(const Tensor& value);
632  Tensor Mul_(Scalar value);
633  Tensor operator*=(const Tensor& value) { return Mul_(value); }
634  Tensor operator*=(Scalar value) { return Mul_(value); }
635 
637  Tensor Div(const Tensor& value) const;
638  Tensor Div(Scalar value) const;
639  Tensor operator/(const Tensor& value) const { return Div(value); }
640  Tensor operator/(Scalar value) const { return Div(value); }
641 
644  Tensor Div_(const Tensor& value);
645  Tensor Div_(Scalar value);
646  Tensor operator/=(const Tensor& value) { return Div_(value); }
647  Tensor operator/=(Scalar value) { return Div_(value); }
648 
652  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
653 
657  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
658 
662  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
663 
667  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
668 
672  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
673 
682  Tensor ArgMin(const SizeVector& dims) const;
683 
692  Tensor ArgMax(const SizeVector& dims) const;
693 
695  Tensor Sqrt() const;
696 
698  Tensor Sqrt_();
699 
701  Tensor Sin() const;
702 
704  Tensor Sin_();
705 
707  Tensor Cos() const;
708 
710  Tensor Cos_();
711 
713  Tensor Neg() const;
714 
716  Tensor Neg_();
717 
719  Tensor Exp() const;
720 
722  Tensor Exp_();
723 
725  Tensor Abs() const;
726 
728  Tensor Abs_();
729 
732  Tensor IsNan() const;
733 
736  Tensor IsInf() const;
737 
741  Tensor IsFinite() const;
742 
747  Tensor Clip(Scalar min_val, Scalar max_val) const;
748 
753  Tensor Clip_(Scalar min_val, Scalar max_val);
754 
756  Tensor Floor() const;
757 
759  Tensor Ceil() const;
760 
762  Tensor Round() const;
763 
765  Tensor Trunc() const;
766 
771  Tensor LogicalNot() const;
772 
780 
785  Tensor LogicalAnd(const Tensor& value) const;
786  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
787  Tensor LogicalAnd(Scalar value) const;
788 
795  Tensor LogicalAnd_(const Tensor& value);
796  Tensor LogicalAnd_(Scalar value);
797 
802  Tensor LogicalOr(const Tensor& value) const;
803  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
804  Tensor LogicalOr(Scalar value) const;
805 
812  Tensor LogicalOr_(const Tensor& value);
813  Tensor LogicalOr_(Scalar value);
814 
820  Tensor LogicalXor(const Tensor& value) const;
821  Tensor LogicalXor(Scalar value) const;
822 
829  Tensor LogicalXor_(const Tensor& value);
830  Tensor LogicalXor_(Scalar value);
831 
833  Tensor Gt(const Tensor& value) const;
834  Tensor operator>(const Tensor& value) const { return Gt(value); }
835  Tensor Gt(Scalar value) const;
836 
839  Tensor Gt_(const Tensor& value);
840  Tensor Gt_(Scalar value);
841 
843  Tensor Lt(const Tensor& value) const;
844  Tensor operator<(const Tensor& value) const { return Lt(value); }
845  Tensor Lt(Scalar value) const;
846 
849  Tensor Lt_(const Tensor& value);
850  Tensor Lt_(Scalar value);
851 
854  Tensor Ge(const Tensor& value) const;
855  Tensor operator>=(const Tensor& value) const { return Ge(value); }
856  Tensor Ge(Scalar value) const;
857 
860  Tensor Ge_(const Tensor& value);
861  Tensor Ge_(Scalar value);
862 
865  Tensor Le(const Tensor& value) const;
866  Tensor operator<=(const Tensor& value) const { return Le(value); }
867  Tensor Le(Scalar value) const;
868 
871  Tensor Le_(const Tensor& value);
872  Tensor Le_(Scalar value);
873 
875  Tensor Eq(const Tensor& value) const;
876  Tensor operator==(const Tensor& value) const { return Eq(value); }
877  Tensor Eq(Scalar value) const;
878 
881  Tensor Eq_(const Tensor& value);
882  Tensor Eq_(Scalar value);
883 
885  Tensor Ne(const Tensor& value) const;
886  Tensor operator!=(const Tensor& value) const { return Ne(value); }
887  Tensor Ne(Scalar value) const;
888 
891  Tensor Ne_(const Tensor& value);
892  Tensor Ne_(Scalar value);
893 
897  std::vector<Tensor> NonZeroNumpy() const;
898 
903  Tensor NonZero() const;
904 
914  bool IsNonZero() const;
915 
919  bool All() const;
920 
924  bool Any() const;
925 
937  bool AllEqual(const Tensor& other) const;
938 
956  bool AllClose(const Tensor& other,
957  double rtol = 1e-5,
958  double atol = 1e-8) const;
959 
978  Tensor IsClose(const Tensor& other,
979  double rtol = 1e-5,
980  double atol = 1e-8) const;
981 
985  bool IsSame(const Tensor& other) const;
986 
988  template <typename T>
989  std::vector<T> ToFlatVector() const {
990  AssertTemplateDtype<T>();
991  std::vector<T> values(NumElements());
993  GetDevice(),
994  GetDtype().ByteSize() * NumElements());
995  return values;
996  }
997 
1000  inline bool IsContiguous() const {
1002  }
1003 
1007  Tensor Contiguous() const;
1008 
1011  Tensor Matmul(const Tensor& rhs) const;
1012 
1015  Tensor Solve(const Tensor& rhs) const;
1016 
1019  Tensor LeastSquares(const Tensor& rhs) const;
1020 
1028  std::tuple<Tensor, Tensor, Tensor> LU(const bool permute_l = false) const;
1029 
1042  std::tuple<Tensor, Tensor> LUIpiv() const;
1043 
1053  Tensor Triu(const int diagonal = 0) const;
1054 
1064  Tensor Tril(const int diagonal = 0) const;
1065 
1077  std::tuple<Tensor, Tensor> Triul(const int diagonal = 0) const;
1078 
1081  Tensor Inverse() const;
1082 
1085  std::tuple<Tensor, Tensor, Tensor> SVD() const;
1086 
1089  inline int64_t GetLength() const { return GetShape().GetLength(); }
1090 
1091  inline SizeVector GetShape() const { return shape_; }
1092 
1093  inline const SizeVector& GetShapeRef() const { return shape_; }
1094 
1095  inline int64_t GetShape(int64_t dim) const {
1096  return shape_[shape_util::WrapDim(dim, NumDims())];
1097  }
1098 
1099  inline SizeVector GetStrides() const { return strides_; }
1100 
1101  inline const SizeVector& GetStridesRef() const { return strides_; }
1102 
1103  inline int64_t GetStride(int64_t dim) const {
1104  return strides_[shape_util::WrapDim(dim, NumDims())];
1105  }
1106 
1107  template <typename T>
1108  inline T* GetDataPtr() {
1109  return const_cast<T*>(const_cast<const Tensor*>(this)->GetDataPtr<T>());
1110  }
1111 
1112  template <typename T>
1113  inline const T* GetDataPtr() const {
1114  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1116  "Requested values have type {} but Tensor has type {}. "
1117  "Please use non templated GetDataPtr() with manual "
1118  "casting.",
1119  Dtype::FromType<T>().ToString(), dtype_.ToString());
1120  }
1121  return static_cast<T*>(data_ptr_);
1122  }
1123 
1124  inline void* GetDataPtr() { return data_ptr_; }
1125 
1126  inline const void* GetDataPtr() const { return data_ptr_; }
1127 
1128  inline Dtype GetDtype() const { return dtype_; }
1129 
1130  Device GetDevice() const;
1131 
1132  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
1133 
1134  inline int64_t NumElements() const { return shape_.NumElements(); }
1135 
1136  inline int64_t NumDims() const { return shape_.size(); }
1137 
1138  template <typename T>
1139  void AssertTemplateDtype() const {
1140  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1142  "Requested values have type {} but Tensor has type {}",
1143  Dtype::FromType<T>().ToString(), dtype_.ToString());
1144  }
1145  if (dtype_.ByteSize() != sizeof(T)) {
1146  utility::LogError("Internal error: element size mismatch {} != {}",
1147  dtype_.ByteSize(), sizeof(T));
1148  }
1149  }
1150 
1152  DLManagedTensor* ToDLPack() const;
1153 
1155  static Tensor FromDLPack(const DLManagedTensor* dlmt);
1156 
1158  void Save(const std::string& file_name) const;
1159 
1161  static Tensor Load(const std::string& file_name);
1162 
1164  struct Iterator {
1165  using iterator_category = std::forward_iterator_tag;
1166  using difference_type = std::ptrdiff_t;
1169  using reference = value_type; // Typically Tensor&, but a tensor slice
1170  // creates a new Tensor object with
1171  // shared memory.
1172 
1173  // Iterator must be constructible, copy-constructible, copy-assignable,
1174  // destructible and swappable.
1175  Iterator(pointer tensor, int64_t index);
1176  Iterator(const Iterator&);
1177  ~Iterator();
1178  reference operator*() const;
1179  pointer operator->() const;
1180  Iterator& operator++();
1181  Iterator operator++(int);
1182  bool operator==(const Iterator& other) const;
1183  bool operator!=(const Iterator& other) const;
1184 
1185  private:
1186  struct Impl;
1187  std::unique_ptr<Impl> impl_;
1188  };
1189 
1191  struct ConstIterator {
1192  using iterator_category = std::forward_iterator_tag;
1193  using difference_type = std::ptrdiff_t;
1194  using value_type = const Tensor;
1196  using reference = value_type; // Typically Tensor&, but a tensor slice
1197  // creates a new Tensor object with
1198  // shared memory.
1199 
1200  // ConstIterator must be constructible, copy-constructible,
1201  // copy-assignable, destructible and swappable.
1202  ConstIterator(pointer tensor, int64_t index);
1203  ConstIterator(const ConstIterator&);
1204  ~ConstIterator();
1205  reference operator*() const;
1206  pointer operator->() const;
1209  bool operator==(const ConstIterator& other) const;
1210  bool operator!=(const ConstIterator& other) const;
1211 
1212  private:
1213  struct Impl;
1214  std::unique_ptr<Impl> impl_;
1215  };
1216 
1220  Iterator begin();
1221 
1225  Iterator end();
1226 
1230  ConstIterator cbegin() const;
1231 
1235  ConstIterator cend() const;
1236 
1241  ConstIterator begin() const { return cbegin(); }
1242 
1247  ConstIterator end() const { return cend(); }
1248 
1249 protected:
1250  std::string ScalarPtrToString(const void* ptr) const;
1251 
1252 private:
1254  template <typename T, size_t D>
1255  static Tensor InitWithInitializerList(
1256  const tensor_init::NestedInitializerList<T, D>& nested_list,
1257  const Device& device = Device("CPU:0")) {
1258  SizeVector shape = tensor_init::InferShape(nested_list);
1259  std::vector<T> values =
1260  tensor_init::ToFlatVector<T, D>(shape, nested_list);
1261  return Tensor(values, shape, Dtype::FromType<T>(), device);
1262  }
1263 
1264 protected:
1267 
1276 
1290  void* data_ptr_ = nullptr;
1291 
1294 
1296  std::shared_ptr<Blob> blob_ = nullptr;
1297 }; // namespace core
1298 
1299 template <>
1300 inline Tensor::Tensor(const std::vector<bool>& init_vals,
1301  const SizeVector& shape,
1302  Dtype dtype,
1303  const Device& device)
1304  : Tensor(shape, dtype, device) {
1305  // Check number of elements
1306  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1308  "Tensor initialization values' size {} does not match the "
1309  "shape {}",
1310  init_vals.size(), shape_.NumElements());
1311  }
1312 
1313  // Check data types
1314  AssertTemplateDtype<bool>();
1315 
1316  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1317  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1318  std::vector<uint8_t> init_vals_uchar(init_vals.size());
1319  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1320  [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1321 
1323  init_vals_uchar.data(),
1324  init_vals_uchar.size() * dtype.ByteSize());
1325 }
1326 
1327 template <>
1328 inline std::vector<bool> Tensor::ToFlatVector() const {
1329  AssertTemplateDtype<bool>();
1330  std::vector<bool> values(NumElements());
1331  std::vector<uint8_t> values_uchar(NumElements());
1332  MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1333  GetDevice(),
1334  GetDtype().ByteSize() * NumElements());
1335 
1336  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1337  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1338  std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1339  [](uint8_t v) -> bool { return static_cast<bool>(v); });
1340  return values;
1341 }
1342 
1343 template <>
1344 inline bool Tensor::Item() const {
1345  if (shape_.NumElements() != 1) {
1347  "Tensor::Item only works for Tensor with one element.");
1348  }
1349  AssertTemplateDtype<bool>();
1350  uint8_t value;
1352  sizeof(uint8_t));
1353  return static_cast<bool>(value);
1354 }
1355 
1356 template <typename S>
1357 inline void Tensor::Fill(S v) {
1359  scalar_t casted_v = static_cast<scalar_t>(v);
1360  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1361  GetDtype(), GetDevice());
1362  AsRvalue() = tmp;
1363  });
1364 }
1365 
1366 template <typename Object>
1367 inline void Tensor::FillObject(const Object& v) {
1368  Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1369  GetDevice());
1370  AsRvalue() = tmp;
1371 }
1372 
1373 template <typename T>
1374 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1375  return rhs + scalar_lhs;
1376 }
1377 
1378 template <typename T>
1379 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1380  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1381 }
1382 
1383 template <typename T>
1384 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1385  return rhs * scalar_lhs;
1386 }
1387 
1388 template <typename T>
1389 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1390  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1391 }
1392 
1393 } // namespace core
1394 } // namespace open3d
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:1254
const Tensor AsRvalue() const
Convert to constant rvalue.
Definition: Tensor.h:538
bool operator==(const Iterator &other) const
Definition: Tensor.cpp:246
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:56
int64_t NumElements() const
Definition: Tensor.h:1134
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:813
constexpr nullopt_t nullopt
Definition: Optional.h:171
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:447
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:1243
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:1232
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:754
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:633
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:1417
The common header of DLPack.
int64_t NumDims() const
Definition: Tensor.h:1136
static void MemcpyFromHost(void *dst_ptr, const Device &dst_device, const void *host_ptr, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default src_device.
Definition: MemoryManager.cpp:86
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1170
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:114
static Tensor Init(const std::initializer_list< std::initializer_list< T >> &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:270
const Dtype Int64
Definition: Dtype.cpp:66
ConstIterator end() const
Definition: Tensor.h:1247
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:803
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1534
Tensor Inverse() const
Definition: Tensor.cpp:1867
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1548
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:538
static Tensor Full(const SizeVector &shape, T fill_value, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with specified value.
Definition: Tensor.h:230
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1780
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1296
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:1212
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
Definition: DLPack.h:193
Tensor Solve(const Tensor &rhs) const
Definition: Tensor.cpp:1813
Tensor AsStrided(const SizeVector &new_shape, const SizeVector &new_strides) const
Create a Tensor view of specified shape and strides. The underlying buffer and data_ptr offsets remai...
Definition: Tensor.cpp:998
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1502
void * GetDataPtr()
Definition: Tensor.h:1124
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:1121
Definition: Dtype.h:39
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:1450
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
Definition: TensorKey.h:45
void Save(const std::string &file_name) const
Save tensor to numpy&#39;s npy format.
Definition: Tensor.cpp:1762
static Tensor Arange(const Scalar start, const Scalar stop, const Scalar step=1, const Dtype dtype=core::Int64, const Device &device=core::Device("CPU:0"))
Create a 1D tensor with evenly spaced values in the given interval.
Definition: Tensor.cpp:410
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1785
pointer operator->() const
Definition: Tensor.cpp:230
std::ptrdiff_t difference_type
Definition: Tensor.h:1193
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:1402
Tensor value_type
Definition: Tensor.h:1167
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:876
Tensor(const T *init_vals, const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor from raw host buffer. The memory will be copied.
Definition: Tensor.h:97
reference operator*() const
Definition: Tensor.cpp:226
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:1270
Definition: Device.h:138
const SizeVector & GetStridesRef() const
Definition: Tensor.h:1101
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:602
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1612
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:639
Tensor IsInf() const
Definition: Tensor.cpp:1302
Tensor operator*=(Scalar value)
Definition: Tensor.h:634
Tensor NonZero() const
Definition: Tensor.cpp:1685
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:1087
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:855
int64_t GetLength() const
Definition: SizeVector.cpp:143
Definition: Scalar.h:42
Tensor LogicalNot() const
Definition: Tensor.cpp:1372
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1469
std::tuple< Tensor, Tensor, Tensor > LU(const bool permute_l=false) const
Computes LU factorisation of the 2D square tensor, using A = P * L * U; where P is the permutation ma...
Definition: Tensor.cpp:1833
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:607
static void MemcpyToHost(void *host_ptr, const void *src_ptr, const Device &src_device, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default dst_device.
Definition: MemoryManager.cpp:94
std::tuple< Tensor, Tensor, Tensor > SVD() const
Definition: Tensor.cpp:1875
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1198
std::string ToString() const
Definition: Dtype.h:83
bool IsNonZero() const
Definition: Tensor.cpp:1687
Iterator for Tensor.
Definition: Tensor.h:1164
Tensor Ceil() const
Element-wise ceil value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1347
Tensor Reverse() const
Reverse a Tensor&#39;s elements by viewing the tensor as a 1D array.
Definition: Tensor.cpp:439
Device GetDevice() const
Definition: Tensor.cpp:1365
Definition: SizeVector.h:79
Const iterator for Tensor.
Definition: Tensor.h:1191
std::tuple< Tensor, Tensor > LUIpiv() const
Computes LU factorisation of the 2D square tensor, using A = P * L * U; where P is the permutation ma...
Definition: Tensor.cpp:1841
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1192
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:695
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1165
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:965
SizeVector strides_
Definition: Tensor.h:1275
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:398
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:1804
Tensor operator+(Scalar value) const
Definition: Tensor.h:601
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1205
Dtype GetDtype() const
Definition: Tensor.h:1128
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:224
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:626
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:866
bool Any() const
Definition: Tensor.cpp:1707
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:1103
Definition: Blob.h:56
Tensor Clip_(Scalar min_val, Scalar max_val)
Definition: Tensor.cpp:1328
Iterator end()
Definition: Tensor.cpp:262
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1516
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:1219
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:1005
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter.
Definition: Tensor.cpp:879
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:1016
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:374
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:1383
bool AllEqual(const Tensor &other) const
Definition: Tensor.cpp:1770
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:1259
const Dtype Undefined
Definition: Dtype.cpp:60
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1580
bool All() const
Definition: Tensor.cpp:1700
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1566
Tensor operator &&(const Tensor &value) const
Definition: Tensor.h:786
~Iterator()
Definition: Tensor.cpp:224
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1191
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:1265
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:910
Tensor operator-=(Scalar value)
Definition: Tensor.h:621
Tensor Clip(Scalar min_val, Scalar max_val) const
Definition: Tensor.cpp:1322
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:713
Tensor Contiguous() const
Definition: Tensor.cpp:746
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:341
ConstIterator cbegin() const
Definition: Tensor.cpp:324
Tensor IsNan() const
Definition: Tensor.cpp:1292
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:613
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1662
Definition: Device.h:39
Tensor(void *data_ptr, Dtype dtype, const SizeVector &shape, const SizeVector &strides={}, const Device &device=Device("CPU:0"))
Tensor wrapper constructor from raw host buffer.
Definition: Tensor.h:145
SizeVector shape_
SizeVector of the Tensor. shape_[i] is the legnth of dimension i.
Definition: Tensor.h:1266
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:1132
bool IsContiguous() const
Definition: Tensor.h:1000
std::tuple< Tensor, Tensor > Triul(const int diagonal=0) const
Returns the tuple of upper and lower triangular matrix of the 2D tensor, above and below the given di...
Definition: Tensor.cpp:1861
char type
Definition: FilePCD.cpp:60
const void * GetDataPtr() const
Definition: Tensor.h:1126
Tensor operator+=(Scalar value)
Definition: Tensor.h:608
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1630
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1177
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:1436
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:1155
void AssertTemplateDtype() const
Definition: Tensor.h:1139
int64_t WrapDim(int64_t dim, int64_t max_dim, bool inclusive)
Wrap around negative dim.
Definition: ShapeUtil.cpp:150
void FillObject(const Object &v)
Definition: Tensor.h:1367
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:612
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:197
Tensor IsFinite() const
Definition: Tensor.cpp:1312
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:380
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:86
ConstIterator begin() const
Definition: Tensor.h:1241
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:55
SizeVector InferShape(const L &list)
Definition: TensorInit.h:101
Dtype dtype_
Data type.
Definition: Tensor.h:1293
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor from the source tensor.
Definition: Tensor.cpp:744
static Tensor Load(const std::string &file_name)
Load tensor from numpy&#39;s npy format.
Definition: Tensor.cpp:1766
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1484
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:1053
SizeVector GetShape() const
Definition: Tensor.h:1091
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:600
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1797
int64_t NumElements() const
Definition: SizeVector.cpp:127
Tensor LogicalNot_()
Definition: Tensor.cpp:1378
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:620
Tensor Trunc() const
Element-wise trunc value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1359
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1714
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:1095
Tensor AsRvalue()
Definition: Tensor.h:535
SizeVector GetStrides() const
Definition: Tensor.h:1099
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:645
Definition: PinholeCameraIntrinsic.cpp:35
Iterator begin()
Definition: Tensor.cpp:255
std::ptrdiff_t difference_type
Definition: Tensor.h:1166
Definition: Tensor.h:51
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1136
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:386
Tensor()
Definition: Tensor.h:53
int64_t ByteSize() const
Definition: Dtype.h:77
Tensor Triu(const int diagonal=0) const
Returns the upper triangular matrix of the 2D tensor, above the given diagonal index. [The value of diagonal = col - row, therefore 0 is the main diagonal (row = col), and it shifts towards right for positive values (for diagonal = 1, col - row = 1), and towards left for negative values. The value of the diagonal parameter must be between [-m, n] for a {m,n} shaped tensor.
Definition: Tensor.cpp:1849
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1068
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1102
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:646
bool operator!=(const Iterator &other) const
Definition: Tensor.cpp:251
Tensor Flatten(int64_t start_dim=0, int64_t end_dim=-1) const
Definition: Tensor.cpp:659
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:831
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:1226
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1644
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1598
double Det() const
Compute the determinant of a 2D square tensor.
Definition: Tensor.cpp:1029
Iterator & operator++()
Definition: Tensor.cpp:235
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:886
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1353
Tensor operator-(Scalar value) const
Definition: Tensor.h:614
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:1276
Tensor Append(const Tensor &other, const utility::optional< int64_t > &axis=utility::nullopt) const
Appends the other tensor, along the given axis and returns a copy of the tensor. The other tensors mu...
Definition: Tensor.cpp:596
T * GetDataPtr()
Definition: Tensor.h:1108
static Tensor Init(const T val, const Device &device=Device("CPU:0"))
Definition: Tensor.h:252
Tensor operator/(Scalar value) const
Definition: Tensor.h:640
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:815
Tensor operator*(Scalar value) const
Definition: Tensor.h:627
const T * GetDataPtr() const
Definition: Tensor.h:1113
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:392
Iterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:210
static Tensor Init(const std::initializer_list< std::initializer_list< std::initializer_list< T >>> &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:279
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1676
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:844
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:834
Tensor Tril(const int diagonal=0) const
Returns the lower triangular matrix of the 2D tensor, above the given diagonal index. [The value of diagonal = col - row, therefore 0 is the main diagonal (row = col), and it shifts towards right for positive values (for diagonal = 1, col - row = 1), and towards left for negative values. The value of the diagonal parameter must be between [-m, n] where {m, n} is the shape of input tensor.
Definition: Tensor.cpp:1855
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1281
Tensor(const std::vector< T > &init_vals, const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor with initial values.
Definition: Tensor.h:69
void * data_ptr_
Definition: Tensor.h:1290
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1823
ConstIterator cend() const
Definition: Tensor.cpp:331
int64_t GetLength() const
Definition: Tensor.h:1089
SizeVector DefaultStrides(const SizeVector &shape)
Compute default strides for a shape when a tensor is contiguous.
Definition: ShapeUtil.cpp:233
bool IsObject() const
Definition: Dtype.h:81
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1718
std::vector< T > ToFlatVector() const
Retrive all values as an std::vector, for debugging and testing.
Definition: Tensor.h:989
Tensor & operator=(const T v) &&
Definition: Tensor.h:188
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:1287
T Item() const
Definition: Tensor.h:585
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1034
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1237
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:799
Tensor Clone() const
Copy Tensor to the same device.
Definition: Tensor.h:479
Tensor Floor() const
Element-wise floor value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1341
void Fill(S v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor&#39;s Dtype...
Definition: Tensor.h:1357
#define LogError(...)
Definition: Logging.h:67
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1248
static Tensor Init(const std::initializer_list< T > &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:262
const SizeVector & GetShapeRef() const
Definition: Tensor.h:1093
Tensor operator/=(Scalar value)
Definition: Tensor.h:647