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(