吴裕雄--天生自然TensorFlow2教程:数据加载
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教程:数据加载的更多相关文章
- 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战
import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...
- 吴裕雄--天生自然TensorFlow2教程:数据统计
import tensorflow as tf a = tf.ones([2, 2]) a tf.norm(a) tf.sqrt(tf.reduce_sum(tf.square(a))) a = tf ...
- 吴裕雄--天生自然TensorFlow2教程:张量排序
import tensorflow as tf a = tf.random.shuffle(tf.range(5)) a tf.sort(a, direction='DESCENDING') # 返回 ...
- 吴裕雄--天生自然TensorFlow2教程:维度变换
图片视图 [b, 28, 28] # 保存b张图片,28行,28列(保存数据一般行优先),图片的数据没有被破坏 [b, 28*28] # 保存b张图片,不考虑图片的行和列,只保存图片的数据,不关注图片 ...
- 吴裕雄--天生自然TensorFlow2教程:Tensor数据类型
list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习 tf.Tensor,为了弥补nump ...
- 吴裕雄--天生自然TensorFlow2教程:函数优化实战
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...
- 吴裕雄--天生自然TensorFlow2教程:反向传播算法
- 吴裕雄--天生自然TensorFlow2教程:链式法则
import tensorflow as tf x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.consta ...
- 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...
随机推荐
- 吴裕雄--天生自然JAVA数据库编程:事务处理
DROP TABLE user ; -- 删除表 CREATE TABLE user( id INT AUTO_INCREMENT PRIMARY KEY , name VARCHAR(30) NOT ...
- Kubernetes 集群日志管理【转】
Kubernetes 开发了一个 Elasticsearch 附加组件来实现集群的日志管理.这是一个 Elasticsearch.Fluentd 和 Kibana 的组合.Elasticsearch ...
- POJ 3259:Wormholes bellman_ford判定负环
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 37906 Accepted: 13954 Descr ...
- python的super()以及父类继承
Python中子类调用父类的方法有两种方法能够实现:调用父类构造方法,或者使用super函数(两者不要混用). 使用“super”时经常会出现代码“super(FooChild,self).__ini ...
- openCV 3.4 windows下的配置说明
链接: https://pan.baidu.com/s/1dkS_G8ZSBD8EnhYeEFZxhQ 密码: xu99 (从官网下的, 但360会提示有毒, 哈哈哈) 运行exe, 解压openCV ...
- 《Interest Rate Risk Modeling》阅读笔记——第十章 主成分模型与 VaR 分析
目录 第十章:主成分模型与 VaR 分析 思维导图 一些想法 推导 PCD.PCC 和 KRD.KRC 的关系 PCD 和 KRD PCC 和 KRC 第十章:主成分模型与 VaR 分析 思维导图 一 ...
- CSS - flex 垂直水平居中
display: flex; justify-content: center; /* 水平居中 */ align-items: center; /* 垂直居中 */
- oracle分组后取某组中最大的值
查询username,根据fundcode分组,按照date倒序,取date最大的一条数据 select * from ( select username, row_number() over(par ...
- SpringBoot 上传文件突然报错 Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.1428942566812653608
异常信息 org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request ...
- C# 并行线程调用
参考 一.异步委托开启线程 Action<int, int> a = add; a.BeginInvoke(, , null, null);//前两个是add方法的参数,后两个可以为空 C ...