tensorFlow2基本操作(三)

本文介绍了TensorFlow2中的数据加载,如CIFAR10/100,以及Keras.datasets的各种数据集。讨论了误差计算中的熵和交叉熵概念,强调了交叉熵在损失函数中的重要性。详细阐述了一阶梯度和二阶梯度下降,激活函数如sigmoid、ReLU及其梯度,以及多层感知机的梯度计算。最后,通过Himmelblau函数优化和手写数字识别实战展示了深度学习的应用。
摘要由CSDN通过智能技术生成

5 数据加载

keras.datasets:

boston housing

mnist/fasion mnist

cifar10/100

imdb

1)载入cifar100

import tensorflow as tf 
from tensorflow import keras
(x, y), (x_test, y_test) = keras.datasets.mnist.load_data()
x.shape
(60000, 28, 28)
y.shape
(60000,)
x.min(),x.max(),x.mean()
(0, 255, 33.318421449829934)
x_test.shape,y_test.shape
((10000, 28, 28), (10000,))
y[:4]
array([5, 0, 4, 1], dtype=uint8)
y_onehot = tf.one_hot(y,depth = 10)
y_onehot[:2]
<tf.Tensor: id=8, shape=(2, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)>

2)载入cifar10/100

(x,y) = (x_test,y_test) = keras.datasets.cifar10.load_data()
#此处本人暂停了,因为下载速度实在是太慢。其中遇到个小问题,网址不对,后来进入python文件更新了网址。
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
  4890624/170498071 [..............................] - ETA: 9:40:41
import tensorflow as tf 
from tensorflow import keras
(x,y),(x_test,y_test) = keras.datasets.cifar10.load_data()       
#因为本人直接在网上下载好了数据集,并且直接拷贝在了目标文件夹‘C:\Users\wanfuchun\.keras\datasets’,因此此处加载特别快
x.shape,y.shape,x_test.shape,y_test.shape
((50000, 32, 32, 3), (50000, 1), (10000, 32, 32, 3), (10000, 1))
x.min(),x.max()
(0, 255)
y[:4]
array([[6],
       [9],
       [9],
       [4]], dtype=uint8)
db = tf.data.Dataset.from_tensor_slices(x_test)
next(iter(db)).shape
TensorShape([32, 32, 3])
db = tf.data.Dataset.from_tensor_slices((x_test,y_test))
next(iter(db))[0].shape
TensorShape([32, 32, 3])
next(iter(db))[1].shape
TensorShape([1])

3)Dataset api的shuffle打散功能

db = db.shuffle(10000)

4).map 数据预处理功能

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
(TensorShape([32, 32, 3]), TensorShape([1, 10]))
res[1][:2] 
<tf.Tensor: id=221, shape=(1, 10), dtype=float32, numpy=array([[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]], dtype=float32)>
db3 = db2.batch(32)
res = next(iter(db3))
res[0].shape,res[1].shape
(TensorShape([32, 32, 32, 3]), TensorShape([32, 1, 10]))

6 误差计算

1)熵的概念

import tensorflow as tf
a = tf.fill([4],0.25)
a * tf.math.log(a)/tf.math.log(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值