# Eigen Tensors
Tensors are multidimensional arrays of elements. Elements are typically scalars,
but more complex types such as strings are also supported.
[TOC]
## Tensor Classes
You can manipulate a tensor with one of the following classes. They all are in
the namespace ```::Eigen.```
### Class Tensor<data_type, rank>
This is the class to use to create a tensor and allocate memory for it. The
class is templatized with the tensor datatype, such as float or int, and the
tensor rank. The rank is the number of dimensions, for example rank 2 is a
matrix.
Tensors of this class are resizable. For example, if you assign a tensor of a
different size to a Tensor, that tensor is resized to match its new value.
#### Constructor Tensor<data_type, rank>(size0, size1, ...)
Constructor for a Tensor. The constructor must be passed ```rank``` integers
indicating the sizes of the instance along each of the the ```rank```
dimensions.
// Create a tensor of rank 3 of sizes 2, 3, 4. This tensor owns
// memory to hold 24 floating point values (24 = 2 x 3 x 4).
Tensor<float, 3> t_3d(2, 3, 4);
// Resize t_3d by assigning a tensor of different sizes, but same rank.
t_3d = Tensor<float, 3>(3, 4, 3);
#### Constructor Tensor<data_type, rank>(size_array)
Constructor where the sizes for the constructor are specified as an array of
values instead of an explicitly list of parameters. The array type to use is
```Eigen::array<Eigen::Index>```. The array can be constructed automatically
from an initializer list.
// Create a tensor of strings of rank 2 with sizes 5, 7.
Tensor<string, 2> t_2d({5, 7});
### Class TensorFixedSize<data_type, Sizes<size0, size1, ...>>
Class to use for tensors of fixed size, where the size is known at compile
time. Fixed sized tensors can provide very fast computations because all their
dimensions are known by the compiler. FixedSize tensors are not resizable.
If the total number of elements in a fixed size tensor is small enough the
tensor data is held onto the stack and does not cause heap allocation and free.
// Create a 4 x 3 tensor of floats.
TensorFixedSize<float, Sizes<4, 3>> t_4x3;
### Class TensorMap<Tensor<data_type, rank>>
This is the class to use to create a tensor on top of memory allocated and
owned by another part of your code. It allows to view any piece of allocated
memory as a Tensor. Instances of this class do not own the memory where the
data are stored.
A TensorMap is not resizable because it does not own the memory where its data
are stored.
#### Constructor TensorMap<Tensor<data_type, rank>>(data, size0, size1, ...)
Constructor for a Tensor. The constructor must be passed a pointer to the
storage for the data, and "rank" size attributes. The storage has to be
large enough to hold all the data.
// Map a tensor of ints on top of stack-allocated storage.
int storage[128]; // 2 x 4 x 2 x 8 = 128
TensorMap<int, 4> t_4d(storage, 2, 4, 2, 8);
// The same storage can be viewed as a different tensor.
// You can also pass the sizes as an array.
TensorMap<int, 2> t_2d(storage, 16, 8);
// You can also map fixed-size tensors. Here we get a 1d view of
// the 2d fixed-size tensor.
Tensor<float, Sizes<4, 5>> t_4x3;
TensorMap<float, 1> t_12(t_4x3, 12);
#### Class TensorRef
See Assigning to a TensorRef below.
## Accessing Tensor Elements
#### <data_type> tensor(index0, index1...)
Return the element at position ```(index0, index1...)``` in tensor
```tensor```. You must pass as many parameters as the rank of ```tensor```.
The expression can be used as an l-value to set the value of the element at the
specified position. The value returned is of the datatype of the tensor.
// Set the value of the element at position (0, 1, 0);
Tensor<float, 3> t_3d(2, 3, 4);
t_3d(0, 1, 0) = 12.0f;
// Initialize all elements to random values.
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 4; ++k) {
t_3d(i, j, k) = ...some random value...;
}
}
}
// Print elements of a tensor.
for (int i = 0; i < 2; ++i) {
LOG(INFO) << t_3d(i, 0, 0);
}
## TensorLayout
The tensor library supports 2 layouts: ```ColMajor``` (the default) and
```RowMajor```. Only the default column major layout is currently fully
supported, and it is therefore not recommended to attempt to use the row major
layout at the moment.
The layout of a tensor is optionally specified as part of its type. If not
specified explicitly column major is assumed.
Tensor<float, 3, ColMajor> col_major; // equivalent to Tensor<float, 3>
TensorMap<Tensor<float, 3, RowMajor> > row_major(data, ...);
All the arguments to an expression must use the same layout. Attempting to mix
different layouts will result in a compilation error.
It is possible to change the layout of a tensor or an expression using the
```swap_layout()``` method. Note that this will also reverse the order of the
dimensions.
Tensor<float, 2, ColMajor> col_major(2, 4);
Tensor<float, 2, RowMajor> row_major(2, 4);
Tensor<float, 2> col_major_result = col_major; // ok, layouts match
Tensor<float, 2> col_major_result = row_major; // will not compile
// Simple layout swap
col_major_result = row_major.swap_layout();
eigen_assert(col_major_result.dimension(0) == 4);
eigen_assert(col_major_result.dimension(1) == 2);
// Swap the layout and preserve the order of the dimensions
array<int, 2> shuffle(1, 0);
col_major_result = row_major.swap_layout().shuffle(shuffle);
eigen_assert(col_major_result.dimension(0) == 2);
eigen_assert(col_major_result.dimension(1) == 4);
## Tensor Operations
The Eigen Tensor library provides a vast library of operations on Tensors:
numerical operations such as addition and multiplication, geometry operations
such as slicing and shuffling, etc. These operations are available as methods
of the Tensor classes, and in some cases as operator overloads. For example
the following code computes the elementwise addition of two tensors:
Tensor<float, 3> t1(2, 3, 4);
...set some values in t1...
Tensor<float, 3> t2(2, 3, 4);
...set some values in t2...
// Set t3 to the element wise sum of t1 and t2
Tensor<float, 3> t3 = t1 + t2;
While the code above looks easy enough, it is important to understand that the
expression ```t1 + t2``` is not actually adding the values of the tensors. The
expression instead constructs a "tensor operator" object of the class
TensorCwiseBinaryOp<scalar_sum>, which has references to the tensors
```t1``` and ```t2```. This is a small C++ object that knows how to add
```t1``` and ```t2```. It is only when the value of the expression is assigned
to the tensor ```t3``` that the addition is actually performed. Technically,
this happens through the overloading of ```operator=()``` in the Tensor class.
This mechanism for computing tensor expressions allows for lazy evaluation and
optimizations which are what make the tensor library very fast.
Of course, the tensor operators do nest, and the expression ```t1 + t2 *
0.3f``` is actually represented with the (approximate) tree of operators:
TensorCwiseBinaryOp<scalar_sum>(t1, TensorCwiseUnaryOp<scalar_mul>(t2, 0.3f))
### Tensor Operations and C++ "auto"
Because Tensor operations create tensor operators, the C++ ```auto``` keyword
does not have its intuitive meaning. Consider these 2 lines of code:
Tensor<float, 3> t3 = t1 + t2;
auto t4 = t1 + t2;
In the first line we allocate the tensor ```t3``` and it will contain the
result of the addition of ```t1``` and ```t2```. In the second line, ```t4```
is actually the tree of tensor operators that will compute the addition of
```t1``` and ```t2```. In fact, ```t4``` is *not* a tensor and you cannot get
the values of its elements:
Tensor<float, 3> t3 = t1 + t2;
cout << t3(0, 0, 0
没有合适的资源?快使用搜索试试~ 我知道了~
Android开发APP实现实时识别手写数字+手机使用tensorflow训练+opencv实时图像识别+源码+使用文档说明
共2000个文件
md:875个
h:530个
py:507个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 2 下载量 101 浏览量
2024-04-02
22:00:20
上传
评论 2
收藏 231.25MB ZIP 举报
温馨提示
Android开发APP实现实时识别手写数字+手机使用tensorflow训练+opencv实时图像识别+源码+使用文档说明,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ Android开发APP实现实时识别手写数字+手机使用tensorflow训练+opencv实时图像识别+源码+使用文档说明,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 项目简介: Android+opencv+tensorflow,使用Android studio开发app,实时识别手写数字,使用tensorflow训练的模型给手机使用,将tensorflow移植到手机上,利用Android端的opencv对摄像头实时获取的图片进行处理识别
资源推荐
资源详情
资源评论
收起资源包目录
Android开发APP实现实时识别手写数字+手机使用tensorflow训练+opencv实时图像识别+源码+使用文档说明 (2000个子文件)
blas.h 113KB
stream.h 77KB
dnn.h 52KB
op_kernel.h 51KB
eigen_spatial_convolutions.h 43KB
c_api.h 32KB
stream_executor_pimpl.h 31KB
eigen_pooling.h 26KB
cuda_driver.h 26KB
eigen_backward_cuboid_convolutions.h 25KB
tensor.h 24KB
eigen_backward_spatial_convolutions.h 21KB
random_distributions.h 20KB
grpc_worker_service_impl.h 20KB
cwise_ops.h 20KB
sparse_tensor.h 19KB
tensor_array.h 19KB
inlined_vector.h 19KB
kernel.h 19KB
cwise_ops_common.h 18KB
graph.h 18KB
cuda_dnn.h 17KB
mirror_pad_op.h 16KB
function.h 16KB
int_type.h 15KB
kernel_spec.h 15KB
stream_executor_internal.h 15KB
env.h 14KB
bfc_allocator.h 14KB
gcuda.h 14KB
device_description.h 14KB
shape_inference.h 14KB
ctc_beam_search.h 14KB
ctc_loss_calculator.h 14KB
pooling_ops_common.h 13KB
top_n.h 13KB
array_slice.h 12KB
tensor_shape.h 12KB
eigen_patch_3d.h 12KB
op.h 12KB
device_memory.h 12KB
allocator.h 11KB
eigen_cuboid_convolution.h 11KB
direct_session.h 11KB
logging.h 11KB
sparse_matmul_op.h 11KB
conv_2d.h 11KB
scope.h 11KB
cuda_gpu_executor.h 11KB
eigen_attention.h 10KB
resource_mgr.h 10KB
reduction_ops_common.h 10KB
ops.h 10KB
strcat.h 9KB
grpc_call.h 9KB
fft.h 9KB
manual_constructor.h 9KB
depthwise_conv_op.h 9KB
machine_manager.h 9KB
strided_slice_op_impl.h 9KB
range_sampler.h 9KB
ops_testutil.h 9KB
aggregate_ops.h 9KB
array_slice_internal.h 9KB
sparse_xent_op.h 8KB
session.h 8KB
executor.h 8KB
grpc_master_service_impl.h 8KB
linalg_ops_common.h 8KB
tracing.h 8KB
grpc_serialization_traits.h 8KB
file_system.h 8KB
cuda_kernel_helper.h 8KB
statusor.h 8KB
pending_counts.h 8KB
tensor_slice_util.h 8KB
thread_annotations.h 8KB
node_def_util.h 8KB
base_rendezvous_mgr.h 8KB
feature_util.h 8KB
platform.h 8KB
saved_tensor_slice_util.h 8KB
tensor_slice.h 8KB
tensor_slice_writer.h 7KB
scanner.h 7KB
device_base.h 7KB
pool_allocator.h 7KB
philox_random.h 7KB
graph_def_builder.h 7KB
common_shape_fns.h 7KB
types.h 7KB
testlib.h 7KB
tensor_slice_reader.h 7KB
tensor_testutil.h 7KB
simple_graph_execution_state.h 7KB
plugin_registry.h 7KB
image_resizer_state.h 7KB
node_def_builder.h 7KB
multi_platform_manager.h 7KB
example_proto_helper.h 7KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- qq_549684412024-04-12资源质量不错,和资源描述一致,内容详细,对我很有用。
梦回阑珊
- 粉丝: 2544
- 资源: 657
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功