首页 > 其他分享 >tf.api

tf.api

时间:2022-10-21 23:45:40浏览次数:58  
标签:dataset3 print dataset item api tf Dataset

API列表

  • Dataset基础使用

    • tf.data.Dataset.from_tensor_slices 这个api构建Dataset
    • 在这个Dataset上具体调用repeat(重复多少次), batch, interleave, map, shuffle, list_files这些api
  • csv(读取csv的时候)

    • 使用tf.data.TextLineDataset读取文本文件,tf.io.decode_csv解析csv文件
  • Tfrecord(Dataset读取Tfrecord文件) 如下api

    • tf.train.FloatList, tf.train.Int64List, tf.train.BytesList
    • tf.train.Feature, tf.train.Freaturs, tf.train.Example 封装tf.example写到文件中去
    • example.SerialiineToString x序列化
    • tf.io.ParseSingleExample 解析一个具体的Example
    • tf.io.VarLenFeature, tf.io.FixedLenFeature 解析tf.example
    • tf.data.TFRecordDataset 基于tf.record构建dataset, tf.io.TFRecordOptions制定读取文件的类型
  • interleave将每个元素进行处理产生新的结果,利用interleave将处理的结果合并形成新的数据集

  • case:文件dataset-->具体的数据集。遍历数据名中的数据集,通过inter leave合并成新的数据集

# interleave 将每个元素进行处理产生新的结果,利用interleave将处理的结果合并形成新的数据集
# case :文件dataset--> 具体的数据集。遍历数据名中的数据集,通过interleave合并成新的数据集 

dataset2 = dataset.interleave(
    # map_fn,对数据进行怎样的变换
    lambda v: tf.data.Dataset.from_tensor_slices(v), #在这里就【27】中的item  
    cycle_length = 5,    # cycle_length :并行程度,即并行的去同时处理dataset中的多少个元素
    block_length = 5,    # block_length :从上面变换的结果中,每次取多少个结果出来(map_fnz中)
    #block_length:达到一种均匀混合的效果(因为他从每个里面取出来5个,最后89不够从第一个补充,知道没有元素了)

)
for item in dataset2:
    print(item)

x =  np.array([[1,2], [2,3], [3,4]])
y =  np.array(['cat', 'dag', 'fox'])
#两个维度必须相等
dataset3 = tf.data.Dataset.from_tensor_slices((x,y))
print(dataset3)

for item_x, item_y in dataset3:
    print(item_x, item_y)
for item_x, item_y in dataset3:
    print(item_x.numpy(), item_y.numpy())
    
    
x =  np.array([[1,2], [2,3], [3,4]])

y =  np.array(['cat', 'dag', 'fox'])

#两个维度必须相等

dataset3 = tf.data.Dataset.from_tensor_slices((x,y))

print(dataset3)
for item_x, item_y in dataset3:

    print(item_x, item_y)

for item_x, item_y in dataset3:

    print(item_x.numpy(), item_y.numpy())

<TensorSliceDataset shapes: ((2,), ()), types: (tf.int64, tf.string)>
tf.Tensor([1 2], shape=(2,), dtype=int64) tf.Tensor(b'cat', shape=(), dtype=string)
tf.Tensor([2 3], shape=(2,), dtype=int64) tf.Tensor(b'dag', shape=(), dtype=string)
tf.Tensor([3 4], shape=(2,), dtype=int64) tf.Tensor(b'fox', shape=(), dtype=string)
[1 2] b'cat'
[2 3] b'dag'
[3 4] b'fox'

dataset4 = tf.data.Dataset.from_tensor_slices({"feature":x,
                                              "label":y})
for item in dataset4:
    print(item)

for item in dataset4:
    print(item["feature"].numpy(), item["label"].numpy())
    
{'feature': <tf.Tensor: id=160, shape=(2,), dtype=int64, numpy=array([1, 2])>, 'label': <tf.Tensor: id=161, shape=(), dtype=string, numpy=b'cat'>}
{'feature': <tf.Tensor: id=162, shape=(2,), dtype=int64, numpy=array([2, 3])>, 'label': <tf.Tensor: id=163, shape=(), dtype=string, numpy=b'dag'>}
{'feature': <tf.Tensor: id=164, shape=(2,), dtype=int64, numpy=array([3, 4])>, 'label': <tf.Tensor: id=165, shape=(), dtype=string, numpy=b'fox'>}
[1 2] b'cat'
[2 3] b'dag'
[3 4] b'fox'

tf.data.Dataset.list_files

tf.io.decode_csv(str, record_defaults) 解析 record_defaults字符串的类型是什么
参数:
records:一个string类型的Tensor。每个字符串都是csv中的记录/行,所有记录都应具有相同的格式;
record_defaults:具有特定类型的Tensor对象列表。可接受的类型有float32,float64,int32,int64,string。输入记录的每列一个张量,具有该列的标量默认值或者如果需要该列则为空向量;
field_delim=',':可选的string。默认为","。用于分隔记录中字段的char分隔符;
use_quote_delim=True:可选的bool。默认为True。如果为false,则将双引号视为字符串字段内的常规字符;
na_value='':要识别为NA/NaN的附加字符串;
select_cols=None:可选的列索引的可选排序列表。如果指定,则仅解析并返回此列的子集;
name=None:操作的名称

tf.stack()变成向量

aa1=tf.constant([1,2,3])
aa2=tf.constant([4,5,6])
ff=tf.stack([aa1,aa2],axis=1) #tf.stack()是一个矩阵拼接函数,会根据函数中对应的参数调整拼接的维度。 axis=0,表示在第一个维度及逆行数据的拼接,如1x3和1x3的数据拼接会形成一个形状为2x3的数据。axis=1表示在第二维的数据进行拼接。
print(ff)

是做随机采样使用的缓冲大小, buffer_size的值是相对于batch_ size而言的 tensorflow中的数据集类Dataset有一个shuffle方法,用来打乱数据集中数据顺序,训练时非常常用。 其中shuffle方法有一个参 数buffer_size,非常令人费解,文档的解释如下:

shuffle函数中的参数buffer_size

dataset = dataset.map() 把dataset中的参数进行map()中的操作在返回一个dataset

.take()沿着坐标轴返回给定位置索引中的元素

标签:dataset3,print,dataset,item,api,tf,Dataset
From: https://www.cnblogs.com/aohongchang/p/16815101.html

相关文章

  • [GWCTF 2019]枯燥的抽奖
    BUUCTF刷题目录BUUCTF刷题相关知识:mt_rand——通过梅森旋转随机数生成器生成随机值解题过程:伪随机数:mt_rand存在的安全问题:php_mt_seed——mt_rand的克星:php_mt_seed安......
  • Opengl___API解读
    glLoadIdentity()转换为没有进行矩阵变换的状态。即对角线为1的单位矩阵。OpenGL函数思考-glLoadIdentityglPushMatrix()和glPopMatrix()glPushMatrix();对之前的矩阵......
  • robotframework自动化测试框架实战教程:创建及使用监听器(listener)接口
    RobotFramework提供了一个监听器(listener)接口可以用来接收测试执行过程中的通知. 监听器通过在命令行中设置选项 --listener 来启用,和导入测试库类似,你也可以指定......
  • 微服务架构学习与思考(11):开源 API 网关02-以 Java 为基础的 API 网关详细介绍
    微服务架构学习与思考(11):开源API网关02-以Java为基础的API网关详细介绍上一篇关于网关的文章:微服务架构学习与思考(10):微服务网关和开源API网关01-以Nginx为......
  • Apifox
    1、Apifox定位Apifox=Postman+Swagger+Mock+JMeterApifox是API文档、API调试、APIMock、API自动化测试一体化协作平台。通过一套系统、一份数据,解决多个......
  • .net core 配置Swagger 摆脱PostMan,你值得拥有这样的api调试方式
    废话不多说直接来看第一步:安装nuget包:Swashbuckle.AspNetCore.Swagger            Swashbuckle.AspNetCore.SwaggerGen         ......
  • ModuleNotFoundError: No module named 'pandas'
     001、问题ModuleNotFoundError:Nomodulenamed'pandas'  002、解决方法pipinstallpandas-ihttp://pypi.douban.com/simple/--trusted-hostpypi.dou......
  • python抓取Prometheus的数据(使用prometheus-api-client库)
    python抓取Prometheus的数据(使用prometheus-api-client库)0、写在前面我们要想抓取Prometheus的数据,一般想到的就是requests请求,爬虫的方式来抓取,这是可行的,当然,还有一个......
  • web share api 分享
    概述Navigator.share() 方法通过调用本机的共享机制作为WebShareAPI的一部分。如果不支持WebShareAPI,则此方法为 undefined。此项功能仅在一些支持的浏览器的......
  • 用好 DIV 和 API,在前端系统中轻松嵌入数据分析模块
    在数字化转型潮流席卷各大行业的今天,越来越多的企业开始重视BI(商业智能)技术的部署和应用,期望从不断增长的数据资源中获得更多业务价值,从而提升利润、控制风险、降低成本。B......