def get_CIFAR10_data(num_training=5000, num_validation=500, num_test=500): cifar10_dir = r'D:\daima\cifar-10-python\cifar-10-batches-py' X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir) print(X_train.shape) mask = range(num_training, num_training + num_validation) X_val = X_train[mask] y_val = y_train[mask] mask = range(num_training) X_train = X_train[mask] y_train = y_train[mask] mask = range(num_test) X_test = X_test[mask] y_test = y_test[mask] mean_image = np.mean(X_train, axis=0) X_train -= mean_image X_val -= mean_image X_test -= mean_image X_train = X_train.transpose(0, 3, 1, 2).copy() X_val = X_val.transpose(0, 3, 1, 2).copy() X_test = X_test.transpose(0, 3, 1, 2).copy() return { 'X_train': X_train, 'y_train': y_train, 'X_val': X_val, 'y_val': y_val, 'X_test': X_test, 'y_test': y_test, }这是一个加载cifar10数据集的函数,如何修改使其能加载mnist数据集
时间: 2023-12-24 18:15:06 浏览: 36
keras model.fit 解决validation_spilt=num 的问题
要加载MNIST数据集,可以使用以下代码修改上述函数:
```
import tensorflow as tf
from tensorflow.keras.datasets import mnist
import numpy as np
def get_MNIST_data(num_training=50000, num_validation=10000, num_test=10000):
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = np.expand_dims(X_train, axis=-1)
X_test = np.expand_dims(X_test, axis=-1)
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
mean_image = np.mean(X_train, axis=0)
X_train -= mean_image
X_test -= mean_image
mask = range(num_training, num_training + num_validation)
X_val = X_train[mask]
y_val = y_train[mask]
mask = range(num_training)
X_train = X_train[mask]
y_train = y_train[mask]
mask = range(num_test)
X_test = X_test[mask]
y_test = y_test[mask]
return {
'X_train': X_train, 'y_train': y_train,
'X_val': X_val, 'y_val': y_val,
'X_test': X_test, 'y_test': y_test,
}
```
此代码使用 TensorFlow 的 MNIST 数据集加载函数,将数据集的维度转换为 (num_samples, height, width, depth) 的形式,并对图像像素值进行归一化。
阅读全文