Tensorflow2(二)tf.data输入模块
代码和其他资料在 github
一、tf.data模块
- 数据分割
import tensorflow as tf
dataset = tf.data.Dataset.from_tensor_slices([1,2,3,4,5,6,7]) #1维
dataset2 = tf.data.Dataset.from_tensor_slices([[1,2],[3,4],[5,6]]) #2维
dataset_dic = tf.data.Dataset.from_tensor_slices({'a':[1,2,3,4],'b':[6,7,8,9], 'c':[12,13,14,15]}) #字典
tf.data.Dataset.from_tensor_slices()
数据切割,并且转化为 Tensor
类型。
dataset
for ele in dataset:
print(ele)
输入:
<TensorSliceDataset shapes: (), types: tf.int32>
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(5, shape=(), dtype=int32)
tf.Tensor(6, shape=(), dtype=int32)
tf.Tensor(7, shape=(), dtype=int32)
for ele in dataset:
print(ele.numpy())
输入:
1
2
3
4
5
6
7
dataset2
for ele2 in dataset2:
print(ele2.numpy())
输入:
<TensorSliceDataset shapes: (2,), types: tf.int32>
[1 2]
[3 4]
[5 6]
dataset_dic
for ele_dic in dataset_dic:
print(ele_dic)
输入:
<TensorSliceDataset shapes: {a: (), b: (), c: ()}, types: {a: tf.int32, b: tf.int32, c: tf.int32}>
{'a': <tf.Tensor: shape=(), dtype=int32, numpy=1>, 'b': <tf.Tensor: shape=(), dtype=int32, numpy=6>, 'c': <tf.Tensor: shape=(), dtype=int32, numpy=12>}
{'a': <tf.Tensor: shape=(), dtype=int32, numpy=2>, 'b': <tf.Tensor: shape=(), dtype=int32, numpy=7>, 'c': <tf.Tensor: shape=(), dtype=int32, numpy=13>}
{'a': <tf.Tensor: shape=(), dtype=int32, numpy=3>, 'b': <tf.Tensor: shape=(), dtype=int32, numpy=8>, 'c': <tf.Tensor: shape=(), dtype=int32, numpy=14>}
{'a': <tf.Tensor: shape=(), dtype=int32, numpy=4>, 'b': <tf.Tensor: shape=(), dtype=int32, numpy=9>, 'c': <tf.Tensor: shape=(), dtype=int32, numpy=15>}
- 其他常用操作
for ele_np in dataset_np.take(4): # 取出前四个
print(ele_np)
dataset_np = dataset_np.shuffle(7) # 打乱顺序
dataset_np = dataset_np.repeat(count = 3) #重复3次,为None无限循环
dataset = dataset.map(tf.square) # 取平方
二、手写识别实例
import tensorflow as tf
(train_images,train_labels),(test_images,test_labels) = tf.keras.datasets.mnist.load_data()
train_images = train_images / 255
test_images = test_images / 255
ds_train_img = tf.data.Dataset.from_tensor_slices(train_images)
ds_train_lab = tf.data.Dataset.from_tensor_slices(train_labels)
ds_train = tf.data.Dataset.zip((ds_train_img,ds_train_lab)) # 数据合并
ds_train = ds_train.shuffle(buffer_size = 10000).repeat().batch(64)
ds_test = tf.data.Dataset.from_tensor_slices((test_images,test_labels))
ds_test = ds_test.batch(64)
model = tf.keras.Sequential([tf.keras.layers.Flatten(input_shape = (28,28)),tf.keras.layers.Dense(128,activation = 'relu'),tf.keras.layers.Dense(10,activation = 'softmax')])
model.compile(optimizer = 'adam',loss = 'sparse_categorical_crossentropy',metrics = ['accuracy'])
steps_per_epoch = train_images.shape[0] // 64 # 每个epoch的步数
model.fit(ds_train,epochs = 5,steps_per_epoch = steps_per_epoch,validation_data = ds_test,validation_steps = 10000 // 64)
Tensorflow2(二)tf.data输入模块的更多相关文章
- tf.data(二) —— 并行化 tf.data.Dataset 生成器
在处理大规模数据时,数据无法全部载入内存,我们通常用两个选项 使用tfrecords 使用 tf.data.Dataset.from_generator() tfrecords的并行化使用前文已经有过 ...
- tf.data
以往的TensorFLow模型数据的导入方法可以分为两个主要方法,一种是使用feed_dict另外一种是使用TensorFlow中的Queues.前者使用起来比较灵活,可以利用Python处理各种输入 ...
- python3 zip 与tf.data.Data.zip的用法
###python自带的zip函数 与 tf.data.Dataset.zip函数 功能用法相似 ''' zip([iterator1,iterator2,]) 将可迭代对象中对应的元素打包成一个元祖 ...
- 使用Open xml 操作Excel系列之二--从data table导出数据到Excel
由于Excel中提供了透视表PivotTable,许多项目都使用它来作为数据分析报表. 在有些情况下,我们需要在Excel中设计好模板,包括数据源表,透视表等, 当数据导入到数据源表时,自动更新透视表 ...
- tensorflow2:tf.app.run()
在很多TensorFlow公布的Demo中,都有这样的代码存在,如下,这是干什么的呢? 我们来看一下源代码: # tensorflow/tensorflow/python/platform/defau ...
- angular学习(二)—— Data Binding
转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/51182106 Data Binding 在angular中.model和vie ...
- <Spring Data JPA>二 Spring Data Jpa
1.pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- 关于TensorFlow2的tf.function()和AutoGraph的一些问题解决
在使用TensorFlow的AutoGraph的时候出现了一些问题,特此记录 AutoGraph did not convert this function. Try decorating it di ...
- [CAMCOCO][C#]我的系统架构.服务器端.(二)----DATA层
这一层在园子里有很多很多的介绍了,这层写好之后老胡也没多研究,基本上就是参考的园子里大咖们的写法,具体的说明老胡也细说不了了,把接口和思路简单描述一下就好,如果有问题还是那句话,感谢您不吝赐教,老胡这 ...
随机推荐
- 删除GIT中的.DS_Store
转载自:https://www.jianshu.com/p/fdaa8be7f6c3 .DS_Store 是什么 使用 Mac 的用户可能会注意到,系统经常会自动在每个目录生成一个隐藏的 .DS_St ...
- gc 模块常用函数
""" 1.gc.set_debug(flags) 设置gc的debug日志,一般设置为gc.DEBUG_LEAK 2.gc.collect([generation]) ...
- ZROI 提高十连测 DAY2
总结:入题尽量快,想到做法要先证明是否正确是否有不合法的情况,是否和题目中描述的情景一模一样. 不要慌 反正慌也拿不了多少分,多分析题目的性质如果不把题目的性质分析出来的话,暴力也非常的难写,有 ...
- static关键字设计原理
语法只是表象,原理才是关键!!! 灵魂static关键字 Java规定:方法只能由对象来调用. 换句话来说,在面向对象的思维下,方法与对象存在一种强耦合. 方法在没有对象的情况下无法调用,于是上帝派来 ...
- 【SDOI2010】猪国杀 题解(模拟)
前言:嗅到了一丝头秃的味道…… ------------------ 题目链接 题目实在太长,变量也很多.建议至少读个三五遍再做题.不要忽略任何细节,不要想当然.(因为真正玩三国杀肯定不像猪一样出牌啊 ...
- 【转载-学习】[一个前端必会的 Nginx免费教程 - 技术胖]
本文学习资料:https://www.jspang.com/detailed?id=39 Nginx官网:https://www.nginx.com/ Nginx:轻量级HTTP服务器: 采用事件驱动 ...
- slots属性(省内存,限制属性的定义)
class Foo: __slots__=['name','age'] #{'name':None,'age':None} # __slots__='name' #{'name':None,'age' ...
- 东哥学Node的故事——内存管理
前言 东哥是一个平凡的前端攻城狮,北邮网研院研二在读,刚接触Node不久,心里充满了对Node的好奇和崇拜,只听噗通一声,掉入了Node的坑... 于是东哥开始疯狂地看Node相关的书籍,这不,就学到 ...
- 用python写网路爬虫 PDF高清完整版免费下载 Python基础教程免费电子书 python入门书籍免费下载
<用python写网路爬虫PDF免费下载>PDF书籍下载 内容简介 作为一种便捷地收集网上信息并从中抽取出可用信息的方式,网络爬虫技术变得越来越有用.使用Python这样的简单编程语言,你 ...
- 理解HTTP的POST和PUT的区别
1.HTTP Methods HTTP Methods GET POST PUT HEAD DELETE PATCH OPTIONS GET is used to request data from ...