import tensorflow as tf
from tensorflow import keras # train: 60k | test: 10k
(x, y), (x_test, y_test) = keras.datasets.mnist.load_data() x.shape
y.shape
# 0纯黑、255纯白
x.min(), x.max(), x.mean()
x_test.shape, y_test.shape
# 0-9有10种分类结果
y_onehot = tf.one_hot(y, depth=10)
y_onehot[:2]
# train: 50k | test: 10k
(x, y), (x_test, y_test) = keras.datasets.cifar10.load_data()
x.shape, y.shape, x_test.shape, y_test.shape
x.min(), x.max()
db = tf.data.Dataset.from_tensor_slices(x_test)
next(iter(db)).shape
db = tf.data.Dataset.from_tensor_slices((x_test, y_test))
next(iter(db))[0].shape
打乱数据
db = tf.data.Dataset.from_tensor_slices((x_test, y_test))
db = db.shuffle(10000)
数据预处理
def preprocess(x, y):
x = tf.cast(x, dtype=tf.float32) / 255.
y = tf.cast(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
return x, y
db2 = db.map(preprocess)
res = next(iter(db2))
res[0].shape, res[1].shape
一次性得到多张照片
db3 = db2.batch(32)
res = next(iter(db3))
res[0].shape, res[1].shape
db_iter = iter(db3)
while True:
next(db_iter)
repeat()
# 迭代不退出
db4 = db3.repeat()
# 迭代两次退出
db3 = db3.repeat(2)
def prepare_mnist_features_and_labels(x, y):
x = tf.cast(x, tf.float32) / 255.
y = tf.cast(y, tf.int64)
return x, y def mnist_dataset():
(x, y), (x_val, y_val) = datasets.fashion_mnist.load_data()
y = tf.one_hot(y, depth=10)
y_val = tf.one_hot(y_val, depth=10) ds = tf.data.Dataset.from_tensor_slices((x, y))
ds = ds.map(prepare_mnist_features_and_labels)
ds = ds.shffle(60000).batch(100)
ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.map(prepare_mnist_features_and_labels)
ds_val = ds_val.shuffle(10000).batch(100)
return ds, ds_val

吴裕雄--天生自然TensorFlow2教程:数据加载的更多相关文章

  1. 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战

    import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...

  2. 吴裕雄--天生自然TensorFlow2教程:数据统计

    import tensorflow as tf a = tf.ones([2, 2]) a tf.norm(a) tf.sqrt(tf.reduce_sum(tf.square(a))) a = tf ...

  3. 吴裕雄--天生自然TensorFlow2教程:张量排序

    import tensorflow as tf a = tf.random.shuffle(tf.range(5)) a tf.sort(a, direction='DESCENDING') # 返回 ...

  4. 吴裕雄--天生自然TensorFlow2教程:维度变换

    图片视图 [b, 28, 28] # 保存b张图片,28行,28列(保存数据一般行优先),图片的数据没有被破坏 [b, 28*28] # 保存b张图片,不考虑图片的行和列,只保存图片的数据,不关注图片 ...

  5. 吴裕雄--天生自然TensorFlow2教程:Tensor数据类型

    list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习 tf.Tensor,为了弥补nump ...

  6. 吴裕雄--天生自然TensorFlow2教程:函数优化实战

    import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...

  7. 吴裕雄--天生自然TensorFlow2教程:反向传播算法

  8. 吴裕雄--天生自然TensorFlow2教程:链式法则

    import tensorflow as tf x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.consta ...

  9. 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度

    import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:将图片变为圆形 (IE8 不支持)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. firewalld学习--service的使用和配置

    service配置文件 firewalld默认给我们提供的ftp的service配置文件ftp.xml <?xml version="1.0" encoding=" ...

  3. linux环境基于python语言docx转pdf

    windows平台因借助win32com具有多种方法将word转为pdf,但linux环境不具备此环境,win32com包也将import失败,那该如何做呢? # -*- coding: utf-8 ...

  4. vCenter 导入Windows Server 2003/XP自定义规范失败

    1.vcsa 切换到/etc/vmware-vpx/sysprep目录下,会有很多个目录,根据Windows Server 2003的版本,64位找到 svr2003-64 这个目录,32位找到svr ...

  5. 041、Java中逻辑运算之普通或运算“|”

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  6. SQL——左连接(Left join)右连接(Right join)内连接(Inner join)

    概念(定义) ​首先还是介绍一下这三个的定义 ​Left join:即左连接,是以左表为基础,根据ON后给出的两表的条件将两表连接起来.结果会将左表所有的查询信息列出,而右表只列出ON后条件与左表满足 ...

  7. 安装mysql server5.5 到start service未响应解决方法

    打开C盘,然后修改    "组织"  =>  "查看"(如下图)  里面的  "隐藏受保护的操作系统文件"  (系统这是会弹出警告,不 ...

  8. [题解] LuoguP4091 [HEOI2016/TJOI2016]求和

    传送门 首先我们来看一下怎么求\(S(m,n)\). 注意到第二类斯特林数的组合意义就是将\(m\)个不同的物品放到\(n\)个没有区别的盒子里,不允许有空盒子的方案数. 那么将\(m\)个不同的物品 ...

  9. 吴裕雄--天生自然java开发常用类库学习笔记:Map接口使用的注意事项

    import java.util.HashMap ; import java.util.Map ; import java.util.Set ; import java.util.Iterator ; ...

  10. Java中默认方法

    默认方法是JDK8新特性,指的是接口也可以提供具体方法了,而不像以前,只能提供抽象方法,Mortal 这个接口,增加了一个默认方法 r,这个方法有实现体,并且被声明为了default,如以下代码: 这 ...