分享好友 人工智能首页 频道列表

tensorFlow2.1下的tf.data.Dataset.from_tensor_slices()的用法

tensorflow教程  2023-02-09 19:187890

一、总结

一句话总结:

将输入的张量的第一个维度看做样本的个数,沿其第一个维度将tensor切片,得到的每个切片是一个样本数据。实现了输入张量的自动切片。
# from_tensor_slices 为输入张量的每一行创建一个带有单独元素的数据集
ts = tf.constant([[1, 2], [3, 4]])
ds = tf.data.Dataset.from_tensor_slices(ts)   # [1, 2], [3, 4]

 

 

 

二、tensorFlow2.1下的tf.data.Dataset.from_tensor_slices()的用法

转自或参考:tensorFlow2.1下的tf.data.Dataset.from_tensor_slices()和batch()的用法
https://www.pythonheidong.com/blog/article/287038/

 

1、tf.data.Dataset.from_tensor_slices

它的作用是切分传入Tensor的第一个维度,生成相应的dataset。

将输入的张量的第一个维度看做样本的个数,沿其第一个维度将tensor切片,得到的每个切片是一个样本数据。实现了输入张量的自动切片。

可以是numpy格式,也可以是tensorflow的tensor的格式,函数会自动将numpy格式转为tensorflow的tensor格式

输入可以是一个tensor  一个tensor字典(字典的每个key对应的value是一个tensor,要求各tensor的第一个维度相等)  一个tensor tuple(tuple 的每个元素是一个tensor,要求各tensor的第一个维度相等)。

示例 1:

import tensorflow as tf
t = tf.range(10.)[:, None]
t = tf.data.Dataset.from_tensor_slices(t)
#<TensorSliceDataset shapes: (1,), types: tf.float32>
for i in t:
    print(i.numpy())

# [0.]
# [1.]
# [2.]
# [3.]
# [4.]
# [5.]
# [6.]
# [7.]
# [8.]
# [9.]

示例2:

# from_tensor_slices 为输入张量的每一行创建一个带有单独元素的数据集
ts = tf.constant([[1, 2], [3, 4]])
ds = tf.data.Dataset.from_tensor_slices(ts)   # [1, 2], [3, 4]

2、batch函数

用示例看下batch函数

batch_t = t.batch(3)
for i in batch_t:
    print(i.numpy())

[[0.]
 [1.]
 [2.]]
[[3.]
 [4.]
 [5.]]
[[6.]
 [7.]
 [8.]]
[[9.]]
 



 

 

 

 

查看更多关于【tensorflow教程】的文章

展开全文
相关推荐
反对 0
举报 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
tensorflow2.0——LSTM,GRU(Sequential层版)
前面都是写的cell版本的GRU和LSTM,比较底层,便于理解原理。下面的Sequential版不用自定义state参数的形状,使用更简便: import tensorflow as tfimport osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'assert tf.__version__.startswith('2.')# 设置相关底层

0评论2023-02-10907

TensorFlow基础笔记(7) 图像风格化效果与性能优化进展
参考 http://hacker.duanshishi.com/?p=1693http://blog.csdn.net/hungryof/article/details/53981959http://blog.csdn.net/hungryof/article/details/61195783http://blog.csdn.net/wyl1987527/article/details/70245214https://www.ctolib.com/AdaIN-style.

0评论2023-02-09559

Tensorflow报错总结 TensorFlow文档
输入不对应报错内容:WARNING:tensorflow:Model was constructed with shape (None, 79) for input Tensor("genres:0", shape=(None, 79), dtype=float32), but it was called on an input with incompatible shape (128, 5).定义模型的输入和训练时候传入的in

0评论2023-02-09636

深度学习框架之TensorFlow的概念及安装(ubuntu下基于pip的安装,IDE为Pycharm)
2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源。TensorFlow 是使用数据流图进行数值计算的开源软件库。也就是说,TensorFlow 使用图(graph)来表示计算任务。图中的节点表示数学运算,边表示运算之间用来交流的多维数组(也就是tensor,张量)

0评论2023-02-09857

TensorFlow源码分析——Tensor与Eigen tensorflow 开源
TensorFlow底层操作的数据结构是Tensor(张量),可以表示多维的数据,其实现在core/framework/tensor.h中,对于tensor的理解主要分两大块:1.Tensor的组成成分2.Tensor是如何进行数学运算的(TensorFlow本质就是处理大量训练数据集,在底层要实现深度学习常

0评论2023-02-09693

tensorflow scope的作用
  我们在使用tensorflow的时候,当你想复用一个函数的模块,调试时候回提示你变量已经出现,提示你是否重用。那我们当然是不重用的,因为每一个变量都是我们需要的。  要体现不同,就在不同的变量中使用name scope限定,那么其中的重复名字就不会出现问题

0评论2023-02-09697

TensorFlow——LinearRegression简单模型代码
代码函数详解tf.random.truncated_normal()函数tf.truncated_normal函数随机生成正态分布的数据,生成的数据是截断的正态分布,截断的标准是2倍的stddev。zip()函数zip() 函数用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些

0评论2023-02-09381

TensorFlow基础笔记(14) 网络模型的保存与恢复_mnist数据实例
http://blog.csdn.net/huachao1001/article/details/78502910http://blog.csdn.net/u014432647/article/details/75276718https://zhuanlan.zhihu.com/p/32887066#coding:utf-8#http://blog.csdn.net/zhuiqiuk/article/details/53376283#http://blog.csdn.net/

0评论2023-02-091030

更多推荐