按源码中注释来说,variant是一个类似C++ std::any的可以保存任意类型对象的容器。

源码在:tensorflow/core/framework/variant.h

tensorflow中的variant是为存储dtype=DT_VARIANT的Tensor而设计的。

使用限制

放入variant中的对象满足如下条件:

  • 有拷贝构造函数
  • 有默认构造函数
  • 是protocol buffer, 或是tensor或者定义了
string TypeName() const;
void Encode(VariantTensorData* data) const;
bool Decode(VariantTensorData data);

如下是一个示例 :tensorflow/learn/test/variant_test.cpp

//#include <any>     //https://en.cppreference.com/w/cpp/utility/any
//#include <variant> //https://en.cppreference.com/w/cpp/utility/variant
//c++17才有 any variant
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/test.h"
#include "tensorflow/core/framework/variant.h"
using namespace tensorflow;
struct MyVariantValue {int x;
};bool DecodeVariant(VariantTensorData* data, MyVariantValue* value)
{return true;
}bool DecodeVariant(std::string* buf, MyVariantValue* value)
{return true;
}
void EncodeVariant(const MyVariantValue& value, VariantTensorData* data)
{}void EncodeVariant(const MyVariantValue& value, std::string* buf)
{}
string DebugStringVariant(const MyVariantValue &value)
{return "debug my variant value";
}
string TypeNameVariant(const MyVariantValue &value)
{return "MyVariant";
}
TEST(VariantTest, Variant)
{LOG(INFO) << "test tensorflow core framework variant";MyVariantValue t;t.x = 3;Variant x = t;LOG(INFO) << x.get<MyVariantValue>()->x;EXPECT_EQ(x.get<MyVariantValue>()->x, 3);
}
//bazel test --test_output all -c opt  //tensorflow/learn:test_variant_test

BUILD文件在tensorflow/learn下

load("//tensorflow:tensorflow.bzl", "tf_custom_op_library")
load("//tensorflow:tensorflow.bzl","tf_cc_test","tf_cc_tests","tf_copts","tf_cuda_library",
)
load("//tensorflow/core/platform:build_config.bzl","tf_kernel_tests_linkstatic","tf_proto_library","tf_pyclif_proto_library",
)tf_cc_tests(name = "learn_test",size = "small",srcs = ["test/variant_test.cpp"],linkopts = select({"//tensorflow:macos": ["-headerpad_max_install_names"],"//conditions:default": [],}),linkstatic = tf_kernel_tests_linkstatic(),visibility = ["//tensorflow:internal","//tensorflow/core:__pkg__",],deps = ["//tensorflow/cc:cc_ops","//tensorflow/cc:cc_ops_internal","//tensorflow/cc:function_ops","//tensorflow/cc:ops","//tensorflow/cc:scope","//tensorflow/cc:sendrecv_ops","//tensorflow/cc:while_loop","//tensorflow/compiler/tf2xla/ops:xla_ops","//tensorflow/core","//tensorflow/core:core_cpu","//tensorflow/core:core_cpu_internal","//tensorflow/core:framework","//tensorflow/core:framework_internal","//tensorflow/core:lib","//tensorflow/core:lib_internal","//tensorflow/core:ops","//tensorflow/core:protos_all_cc","//tensorflow/core:test","//tensorflow/core:test_main","//tensorflow/core:testlib","//tensorflow/core/common_runtime:direct_session_internal","//tensorflow/core/kernels:ops_util","//tensorflow/core/lib/monitoring:cell_reader","//tensorflow/core/platform:regexp","//tensorflow/core/platform:status_matchers","//tensorflow/core/profiler/lib:profiler_session","//tensorflow/core/profiler/protobuf:memory_profile_proto_cc","//tensorflow/core/profiler/utils:xplane_schema","//tensorflow/core/profiler/utils:xplane_visitor","//tensorflow/core/util:protos_test_cc","//third_party/eigen3","@com_google_absl//absl/base","@com_google_absl//absl/memory","@com_google_absl//absl/strings",],
)

运行测试

bazel test --test_output all -c opt  //tensorflow/learn:test_variant_test

tensorflow 之variant相关推荐

  1. cnn 预测过程代码_FPN的Tensorflow代码详解——特征提取

    @TOC   特征金字塔网络最早于2017年发表于CVPR,与Faster RCNN相比其在多池度特征预测的方式使得其在小目标预测上取得了较好的效果.FPN也作为mmdeteciton的Neck模块, ...

  2. Tensorflow遇到的问题InvalidArgumentError: Graph execution error:2 root error(s) found.解决方法

    文章目录 遇到问题 解决 参考 遇到问题 平台google colab 使用GPU 训练dl模型时,遇到报错 --------------------------------------------- ...

  3. TensorFlow学习笔记(二十六)CNN的9大模型之LeNet5的原理讲解

    在机器视觉,图像处理领域,卷积神经网络取得了巨大的成功.本文将参考UFLDL和DEEPLEARNING.NET的教程,结合自己的理解,梳理一下卷积神经网络的构成以及其BP算法的求解.虽然利用thean ...

  4. CNN: TensorFlow 1.14.0 更新

    版本contribute发布:https://fossies.org/linux/tensorflow/RELEASE.md https://github.com/tensorflow/tensorf ...

  5. 使用TensorFlow 2.0+和Keras实现AlexNet CNN架构

    技术 (Technical) 介绍 (Introduction) The main content of this article will present how the AlexNet Convo ...

  6. 【TensorFlow实战】TensorFlow实现经典卷积神经网络之ResNet

    ResNet ResNet(Residual Neural Network)通过使用Residual Unit成功训练152层深的神经网络,在ILSVRC 2015比赛中获得冠军,取得3.57%的to ...

  7. GraphSAGE NIPS 2017 代码分析(Tensorflow版)

    文章目录 数据集 ppi数据集信息 toy-ppi-G.json 图的信息 toy-ppi-class_map.json toy-ppi-id_map.json toy-ppi-walks.txt t ...

  8. CNN网络架构学习:Chapter-5-ResNet(附代码tensorflow)

    ResNet(Residual Neural Network)由微软研究院的Kaiming He等人提出,通过使用ResNet Unit成功训练出了152层的神经网络,并在ILSVRC2015比赛中取 ...

  9. ubantu + anaconda + TensorFlow 1.13.1

    tensorflow2.0尝鲜版本发布了,但是tensorflow1.13.1毕竟是稳定版本,由于网络问题安装.更新一直失败. 系统:ubantu 18 anaconda 3 首先查询版本: (bas ...

最新文章

  1. 最小生成树(Kruskal )
  2. C++中的sta::atomic<bool>和auto类型
  3. 中国联通与阿里宣布相互开放云计算资源
  4. Java基础day6
  5. 在C#代码中应用Log4Net系列教程(附源代码)
  6. miui12 android系统耗电,miui12耗电严重怎么办,miui12续航优化方法
  7. 计算机科学家论文引用排名:LeCun终于晋身三巨头
  8. OSI参考模型(1)
  9. U盘FAT32转换NTFS格式
  10. 用曲率,挠率反求曲线方程!(作业捷径篇 续集)
  11. 使用 ffmpeg 进行视频(ts)合并
  12. python mysql扩展,python-sql-faker:轻量级、易拓展的数据库智能填充开源库(Python实现版)...
  13. 区块链技术在银行业的应用
  14. 半导体二极管的伏安特性和电流方程
  15. 【连载之一】那些公众号不会告诉你的职业真相
  16. Web服务器集群——Apache网页与安全优化
  17. 什么是FDM 3D打印技术?
  18. 咫尺天涯   三世桃花
  19. linux和华为云计算区别,华为云计算HCIE-Cloud面试宝典(九)
  20. Substance Painter 服饰材质制作 中间刮痕 - 肩带3

热门文章

  1. 成都真的挺好的,但是求求你们别来了
  2. 系统资源是什么,以及线程什么时候不会释放 共享资源
  3. ASP.Net MVC开发基础学习笔记:四、校验、AJAX与过滤器
  4. Cannot read properties of undefined (reading ‘parentNode‘)
  5. 自然对数e是干啥的?
  6. 性能测试实战(十):全链路性能测试
  7. windows7 mysql 5.6使用_MySQL5.6 windows7下安装及基本操作
  8. 格莱富笔记本电脑教你如何隐藏磁盘驱动器保护隐私文件
  9. 计算机毕业设计springboot汽车销售卖车管理系统【前后端分离·新项目·代码讲解·安装调试·文档指导】
  10. redis初步认识-基本操作-Sring get and sett示例(一)