首页 > 解决方案 > 是否可以只加载 mnist 或其他元组的一部分

问题描述

我正忙着用 构建 OCR MNIST,但是由于 in 出现错误,我在上传数据集TensorflowKeras遇到了问题。我可以只上传前几项而不设置错误吗MNISTMNIST

标签: pythontensorflowkerasmnist

解决方案


你的问题不是很清楚。然而,下面是如何使用 TensorFlow 和 Keras 中的简单函数加载 MNIST 的数据样本。

1)。使用 TensorFlow 加载 MNIST 的一部分。

from tensorflow.examples.tutorials.mnist import input_data

data = input_data.read_data_sets('./tmp/mnist_data', one_hot = True)

data_slice = 3000
train_x = data.train.images[:data_slice,:]
train_y = data.train.labels[:data_slice,:]
test_x = data.test.images[:data_slice,:]
test_y = data.test.labels[:data_slice,:]

train_x.shape
'Output': (3000, 784)

2).使用 Keras 加载 MNIST 的一部分。

import keras

# import dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# flatten the features from 28*28 pixel to 784 wide vector
x_train = np.reshape(x_train, (-1, 784)).astype('float32')
x_test = np.reshape(x_test, (-1, 784)).astype('float32')

# one-hot encode the targets
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)

data_slice = 3000
x_train = x_train[:data_slice,:]
y_train = y_train[:data_slice,:]
x_test = x_test[:data_slice,:]
y_test = y_test[:data_slice,:]

x_train.shape
'Output': (3000, 784)

y_train.shape
'Output': (3000, 10)

推荐阅读