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. firewalld学习-zone

    原文地址:http://www.excelib.com/article/290/show firewalld默认提供了九个zone配置文件: block.xml.dmz.xml.drop.xml.ex ...

  2. 微信小程序—页面跳转

    问题: 实现页面跳转:index页面通过按钮跳转到next页面 方法: 1.页面index.wxml增加一个按钮 // index.wxml <button bindtap="jump ...

  3. 035、Java中自增之++在后面的写法

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

  4. jQuery事件 - toggle() 方法

    1.切换元素的显示与隐藏状态 实例 切换 <p> 元素的显示与隐藏状态: $(".btn1").click(function(){ $("p").h ...

  5. mysq 事务管理入门

    设置隔离级别:

  6. SpringBoot-属性配置yaml自定义属性和值

    SpringBoot-属性配置yaml自定义属性和值 SpringBoot-属性配置yaml自定义属性和值 在SpringBoot中yml/yaml文件可以自定义一些属性,以供注入给自定义bean对象 ...

  7. 实验吧-杂项-pilot-logic、ROT-13变身了

    1.pilot-logic 题上说password藏在文件里,直接丢到Winhex里,搜索pass就拿到flag了. 有的大佬提供了另一种方法,题上说是一个磁盘文件,有一个处理磁盘文件的软件autop ...

  8. (三)微信小程序配置

    小程序官方文档 全局配置

  9. Golang函数-不定参函数

    Golang函数-不定参函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  10. WFP之WFP简介

    ·过滤引擎是WFP的核心组成部分,过滤引擎分为两大层:用户态基础过滤引擎和内核态过滤引擎.基础过滤引擎会与内核过滤引擎交互.·内核态过滤引擎是整个过滤引擎的主体,内部分为多个分层,每分层都代表着网络协 ...