TensorFlow教程之API DOC 6.1.6 Class tensorflow::Tensor

简介:

本文档为TensorFlow参考文档,本转载已得到TensorFlow中文社区授权。


Class tensorflow::Tensor

Represents an n-dimensional array of values.

Member Summary

Member Details

tensorflow::Tensor::Tensor()

Default Tensor constructor. Creates a 1-dimension, 0-element float tensor.

tensorflow::Tensor::Tensor(DataType type, const TensorShape &shape)

Creates a Tensor of the given type and shape.

The underlying buffer is allocated using a CPUAllocator.

tensorflow::Tensor::Tensor(Allocator *a, DataType type, const TensorShape &shape)

Creates a tensor with the input type and shape, using the allocator a to allocate the underlying buffer.

a must outlive the lifetime of this Tensor .

tensorflow::Tensor::Tensor(DataType type)

Creates an uninitialized Tensor of the given data type.

tensorflow::Tensor::Tensor(const Tensor &other)

tensorflow::Tensor::~Tensor()

Copy constructor.

DataType tensorflow::Tensor::dtype() const

Returns the data type.

const TensorShape& tensorflow::Tensor::shape() const

Returns the shape of the tensor.

int tensorflow::Tensor::dims() const

Convenience accessor for the tensor shape.

For all shape accessors, see comments for relevant methods of TensorShape in tensor_shape.h.

int64 tensorflow::Tensor::dim_size(int d) const

Convenience accessor for the tensor shape.

int64 tensorflow::Tensor::NumElements() const

Convenience accessor for the tensor shape.

bool tensorflow::Tensor::IsSameSize(const Tensor &b) const

bool tensorflow::Tensor::IsInitialized() const

Has this Tensor been initialized?

size_t tensorflow::Tensor::TotalBytes() const

Returns the estimated memory usage of this tensor.

Tensor& tensorflow::Tensor::operator=(const Tensor &other)

Assign operator. This tensor shares other's underlying storage.

bool tensorflow::Tensor::CopyFrom(const Tensor &other, const TensorShape &shape) TF_MUST_USE_RESULT

Copy the other tensor into this tensor and reshape it.

This tensor shares other's underlying storage. Returns true iff other.shape() has the same number of elements of the given shape.

Tensor tensorflow::Tensor::Slice(int64 dim0_start, int64 dim0_limit) const

Slice this tensor along the 1st dimension.

I.e., the returned tensor satisifies returned[i, ...] == this[dim0_start + i, ...]. The returned tensor shares the underlying tensor buffer with this tensor.

NOTE: The returned tensor may not satisfies the same alignment requirement as this tensor depending on the shape. The caller must check the returned tensor's alignment before calling certain methods that have alignment requirement (e.g., flat()tensor()).

REQUIRES: dims() >= 1 REQUIRES: 0 <= dim0_start <= dim0_limit <= dim_size(0)

bool tensorflow::Tensor::FromProto(const TensorProto &other) TF_MUST_USE_RESULT

Parse other and construct the tensor.

Returns true iff the parsing succeeds. If the parsing fails, the state of *this is unchanged.

bool tensorflow::Tensor::FromProto(Allocator *a, const TensorProto &other) TF_MUST_USE_RESULT

void tensorflow::Tensor::AsProtoField(TensorProto *proto) const

Fills in proto with *this tensor's content.

AsProtoField() fills in the repeated field for proto.dtype(), while AsProtoTensorContent() encodes the content in proto.tensor_content() in a compact form.

void tensorflow::Tensor::AsProtoTensorContent(TensorProto *proto) const

TTypes<T>::Vec tensorflow::Tensor::vec()

Return the tensor data as an Eigen::Tensor with the type and sizes of this Tensor.

Use these methods when you know the data type and the number of dimensions of the Tensor and you want an Eigen::Tensor automatically sized to the Tensor sizes. The implementation check fails if either type or sizes mismatch.

Example:

```c++ typedef float T; Tensor my_mat(...built with Shape{rows: 3, cols: 5}...); auto mat = my_mat.matrix(); // 2D Eigen::Tensor, 3 x 5. auto mat = my_mat.tensor(); // 2D Eigen::Tensor, 3 x 5. auto vec = my_mat.vec(); // CHECK fails as my_mat is 2D. auto vec = my_mat.tensor(); // CHECK fails as my_mat is 2D. auto mat = my_mat.matrix();// CHECK fails as type mismatch.,>,>


#### `TTypes<T>::Matrix tensorflow::Tensor::matrix()` <a class="md-anchor" id="TTypes_T_Matrix_tensorflow_Tensor_matrix"></a>





#### `TTypes< T, NDIMS >::Tensor tensorflow::Tensor::tensor()` <a class="md-anchor" id="TTypes_T_NDIMS_Tensor_tensorflow_Tensor_tensor"></a>





#### `TTypes<T>::Flat tensorflow::Tensor::flat()` <a class="md-anchor" id="TTypes_T_Flat_tensorflow_Tensor_flat"></a>

Return the tensor data as an `Eigen::Tensor` of the data type and a specified shape.

These methods allow you to access the data with the dimensions and sizes of your choice. You do not need to know the number of dimensions of the Tensor to call them. However, they `CHECK` that the type matches and the dimensions requested creates an `Eigen::Tensor` with the same number of elements as the tensor.

Example:

```c++ typedef float T;
Tensor my_ten(...built with Shape{planes: 4, rows: 3, cols: 5}...);
// 1D Eigen::Tensor, size 60:
auto flat = my_ten.flat<T>();
// 2D Eigen::Tensor 12 x 5:
auto inner = my_ten.flat_inner_dims<T>();
// 2D Eigen::Tensor 4 x 15:
auto outer = my_ten.shaped<T, 2>({4, 15});
// CHECK fails, bad num elements:
auto outer = my_ten.shaped<T, 2>({4, 8});
// 3D Eigen::Tensor 6 x 5 x 2:
auto weird = my_ten.shaped<T, 3>({6, 5, 2});
// CHECK fails, type mismatch:
auto bad   = my_ten.flat<int32>();

TTypes<T>::UnalignedFlat tensorflow::Tensor::unaligned_flat()

TTypes<T>::Matrix tensorflow::Tensor::flat_inner_dims()

Returns the data as an Eigen::Tensor with 2 dimensions, collapsing all Tensor dimensions but the last one into the first dimension of the result.

TTypes<T>::Matrix tensorflow::Tensor::flat_outer_dims()

Returns the data as an Eigen::Tensor with 2 dimensions, collapsing all Tensor dimensions but the first one into the last dimension of the result.

TTypes< T, NDIMS >::Tensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes)

TTypes< T, NDIMS >::UnalignedTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes)

TTypes< T >::Scalar tensorflow::Tensor::scalar()

Return the Tensor data as a TensorMap of fixed size 1: TensorMap<TensorFixedSize<T, 1>>.

Using scalar() allows the compiler to perform optimizations as the size of the tensor is known at compile time.

TTypes<T>::ConstVec tensorflow::Tensor::vec() const

Const versions of all the methods above.

TTypes<T>::ConstMatrix tensorflow::Tensor::matrix() const

TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::tensor() const

TTypes<T>::ConstFlat tensorflow::Tensor::flat() const

TTypes<T>::UnalignedConstFlat tensorflow::Tensor::unaligned_flat() const

TTypes<T>::ConstMatrix tensorflow::Tensor::flat_inner_dims() const

TTypes<T>::ConstMatrix tensorflow::Tensor::flat_outer_dims() const

TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes) const

TTypes< T, NDIMS >::UnalignedConstTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes) const

TTypes< T >::ConstScalar tensorflow::Tensor::scalar() const

string tensorflow::Tensor::SummarizeValue(int64 max_entries) const

Render the first max_entries values in *this into a string.

string tensorflow::Tensor::DebugString() const

A human-readable summary of the tensor suitable for debugging.

void tensorflow::Tensor::FillDescription(TensorDescription *description) const

Fill in the TensorDescription proto with metadata about the tensor that is useful for monitoring and debugging.

StringPiece tensorflow::Tensor::tensor_data() const

Returns a StringPiece mapping the current tensor's buffer.

The returned StringPiece may point to memory location on devices that the CPU cannot address directly.

NOTE: The underlying tensor buffer is refcounted, so the lifetime of the contents mapped by the StringPiece matches the lifetime of the buffer; callers should arrange to make sure the buffer does not get destroyed while the StringPiece is still used.

REQUIRES: DataTypeCanUseMemcpy( dtype() ).

相关文章
|
1月前
|
数据采集 监控 安全
各种业务场景调用API代理的API接口教程
API代理的API接口在各种业务场景中具有广泛的应用,本文将介绍哪些业务场景可以使用API代理的API接口,并提供详细的调用教程和代码演示,同时,我们还将讨论在不同场景下使用API代理的API接口所带来的好处。
|
4月前
|
API
免费节假日api接口使用教程-聚合数据
免费节假日api接口使用教程-聚合数据
854 0
免费节假日api接口使用教程-聚合数据
|
4月前
|
机器学习/深度学习 JSON JavaScript
图文讲解 Stable Diffusion API 调用教程
Stable Diffusion 是一个先进的深度学习模型,用于创造和修改图像。这个模型能够基于文本描述来生成图像,让机器理解和实现用户的创意。使用这项技术的关键在于掌握其 API,通过编程来操控图像生成的过程。
|
9天前
|
机器学习/深度学习 API TensorFlow
TensorFlow的高级API:tf.keras深度解析
【4月更文挑战第17天】本文深入解析了TensorFlow的高级API `tf.keras`,包括顺序模型和函数式API的模型构建,以及模型编译、训练、评估和预测的步骤。`tf.keras`结合了Keras的易用性和TensorFlow的性能,支持回调函数、模型保存与加载等高级特性,助力提升深度学习开发效率。
|
1月前
|
安全 API 数据安全/隐私保护
email api接口配置教程步骤详解
Email API是用于程序化访问邮件服务的工具,让开发者能集成邮件功能到应用中。配置Email API包括选择供应商(如SendGrid、Mailgun、AokSend),注册获取API密钥,配置API参数,及测试邮件发送。使用Email API能提升邮件发送的可靠性和效率,便于邮件管理及营销活动。AokSend支持大量验证码发送,适合高效邮件运营。
|
2月前
|
存储 负载均衡 API
部署大模型API的实战教程
部署大模型API的实战教程可以分为以下步骤:
|
2月前
|
机器学习/深度学习 人工智能 API
人工智能应用工程师技能提升系列2、——TensorFlow2——keras高级API训练神经网络模型
人工智能应用工程师技能提升系列2、——TensorFlow2——keras高级API训练神经网络模型
34 0
|
4月前
|
存储 API 数据安全/隐私保护
2021年最新最全Flink系列教程__Flink高级API(四)
2021年最新最全Flink系列教程__Flink高级API(四)
36 0
|
4月前
|
消息中间件 API 数据安全/隐私保护
2021年最新最全Flink系列教程__Flink高级API(三)
2021年最新最全Flink系列教程__Flink高级API(三)
18 0
|
4月前
|
消息中间件 Kafka API
2021年最新最全Flink系列教程_Flink原理初探和流批一体API(二.五)
2021年最新最全Flink系列教程_Flink原理初探和流批一体API(二.五)
40 0

热门文章

最新文章