本文整理汇总了Python中keras.datasets.mnist.load_data方法的典型用法代码示例。如果您正苦于以下问题:Python mnist.load_data方法的具体用法?Python mnist.load_data怎么用?Python mnist.load_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.datasets.mnist
的用法示例。
在下文中一共展示了mnist.load_data方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_mnist_data
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def get_mnist_data(binarize=False):
"""Puts the MNIST data in the right format."""
(X_train, y_train), (X_test, y_test) = mnist.load_data()
if binarize:
X_test = np.where(X_test >= 10, 1, -1)
X_train = np.where(X_train >= 10, 1, -1)
else:
X_train = (X_train.astype(np.float32) - 127.5) / 127.5
X_test = (X_test.astype(np.float32) - 127.5) / 127.5
X_train = np.expand_dims(X_train, axis=-1)
X_test = np.expand_dims(X_test, axis=-1)
y_train = np.expand_dims(y_train, axis=-1)
y_test = np.expand_dims(y_test, axis=-1)
return (X_train, y_train), (X_test, y_test)
示例2: get_mnist_data
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def get_mnist_data(binarize=False):
"""Puts the MNIST data in the right format."""
(X_train, y_train), (X_test, y_test) = mnist.load_data()
if binarize:
X_test = np.where(X_test >= 10, 1, -1)
X_train = np.where(X_train >= 10, 1, -1)
else:
X_train = (X_train.astype(np.float32) - 127.5) / 127.5
X_test = (X_test.astype(np.float32) - 127.5) / 127.5
X_train = np.expand_dims(X_train, axis=-1)
X_test = np.expand_dims(X_test, axis=-1)
y_train = np.eye(10)[y_train]
y_test = np.eye(10)[y_test]
return (X_train, y_train), (X_test, y_test)
示例3: setup_mnist
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def setup_mnist(self, img_res):
print ("Setting up MNIST...")
if not os.path.exists('datasets/mnist_x.npy'):
# Load the dataset
(mnist_X, mnist_y), (_, _) = mnist.load_data()
# Normalize and rescale images
mnist_X = self.normalize(mnist_X)
mnist_X = np.array([imresize(x, img_res) for x in mnist_X])
mnist_X = np.expand_dims(mnist_X, axis=-1)
mnist_X = np.repeat(mnist_X, 3, axis=-1)
self.mnist_X, self.mnist_y = mnist_X, mnist_y
# Save formatted images
np.save('datasets/mnist_x.npy', self.mnist_X)
np.save('datasets/mnist_y.npy', self.mnist_y)
else:
self.mnist_X = np.load('datasets/mnist_x.npy')
self.mnist_y = np.load('datasets/mnist_y.npy')
print ("+ Done.")
示例4: nn_model
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def nn_model():
(x_train, y_train), _ = mnist.load_data()
# 归一化
x_train = x_train.reshape(x_train.shape[0], -1) / 255.
# one-hot
y_train = np_utils.to_categorical(y=y_train, num_classes=10)
# constant(value=1.)自定义常数,constant(value=1.)===one()
# 创建模型:输入784个神经元,输出10个神经元
model = Sequential([
Dense(units=200, input_dim=784, bias_initializer=constant(value=1.), activation=tanh),
Dense(units=100, bias_initializer=one(), activation=tanh),
Dense(units=10, bias_initializer=one(), activation=softmax),
])
opt = SGD(lr=0.2, clipnorm=1.) # 优化器
model.compile(optimizer=opt, loss=categorical_crossentropy, metrics=['acc', 'mae']) # 编译
model.fit(x_train, y_train, batch_size=64, epochs=20, callbacks=[RemoteMonitor()])
model_save(model, './model.h5')
示例5: pull_mnist
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def pull_mnist(split=0.1, flatten=True):
learning, testing = mnist.load_data()
X = np.concatenate([learning[0], testing[0]]).astype(typing.floatX)
Y = np.concatenate([learning[1], testing[1]]).astype("uint8")
X -= X.mean()
X /= X.std()
if flatten:
X = X.reshape(-1, 784)
else:
X = X[:, None, ...]
Y = np.eye(10)[Y]
if split:
arg = np.arange(len(X))
np.random.shuffle(arg)
div = int(len(X) * split)
targ, larg = arg[:div], arg[div:]
return X[larg], Y[larg], X[targ], Y[targ]
return X, Y
示例6: main
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def main():
num_classes = 10
num_samples = 3 # number of architecture to sample
metric = 'val_accuracy' # evaluation metric
resource_type = 'epoch'
max_resource = 81 # max resource that a configuration can have
# load and normalize data
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# defining searcher and evaluator
evaluator = SimpleClassifierEvaluator((x_train, y_train), num_classes,
max_num_training_epochs=5)
searcher = se.RandomSearcher(get_search_space(num_classes))
hyperband = SimpleArchitectureSearchHyperBand(searcher, hyperband, metric, resource_type)
(best_config, best_perf) = hyperband.evaluate(max_resource)
print("Best %s is %f with architecture %d" % (metric, best_perf[0], best_config[0]))
示例7: test_cifar
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def test_cifar(self):
print('cifar10')
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
print('cifar100 fine')
(X_train, y_train), (X_test, y_test) = cifar100.load_data('fine')
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
print('cifar100 coarse')
(X_train, y_train), (X_test, y_test) = cifar100.load_data('coarse')
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
示例8: test_imdb
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def test_imdb(self):
print('imdb')
(X_train, y_train), (X_test, y_test) = imdb.load_data()
示例9: get_cifar10
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def get_cifar10():
"""Retrieve the CIFAR dataset and process the data."""
# Set defaults.
nb_classes = 10
batch_size = 64
input_shape = (3072,)
# Get the data.
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.reshape(50000, 3072)
x_test = x_test.reshape(10000, 3072)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)
return (nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test)
示例10: get_mnist
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def get_mnist():
"""Retrieve the MNIST dataset and process the data."""
# Set defaults.
nb_classes = 10
batch_size = 128
input_shape = (784,)
# Get the data.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)
return (nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test)
示例11: mnist_dataset_reader
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def mnist_dataset_reader():
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255 # 归一化
X_test /= 255
digit_indices = [np.where(y_train == i)[0] for i in range(10)]
tr_pairs, tr_y = create_pairs(X_train, digit_indices)
digit_indices = [np.where(y_test == i)[0] for i in range(10)]
te_pairs, te_y = create_pairs(X_test, digit_indices)
input_dim = 784
return input_dim, tr_pairs, tr_y, te_pairs, te_y
示例12: data
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def data():
"""Data providing function:
Make sure to have every relevant import statement included here and return data as
used in model function below. This function is separated from model() so that hyperopt
won't reload data for each evaluation run.
"""
from keras.datasets import mnist
from keras.utils import np_utils
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
nb_classes = 10
y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)
return x_train, y_train, x_test, y_test
示例13: load_mnist
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def load_mnist(size=64):
(train_data, train_labels), (test_data, test_labels) = mnist.load_data()
train_data = normalize(train_data)
test_data = normalize(test_data)
x = np.concatenate((train_data, test_data), axis=0)
# y = np.concatenate((train_labels, test_labels), axis=0).astype(np.int)
seed = 777
np.random.seed(seed)
np.random.shuffle(x)
# np.random.seed(seed)
# np.random.shuffle(y)
# x = np.expand_dims(x, axis=-1)
x = np.asarray([scipy.misc.imresize(x_img, [size, size]) for x_img in x])
x = np.expand_dims(x, axis=-1)
return x
示例14: load_cifar10
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def load_cifar10(size=64) :
(train_data, train_labels), (test_data, test_labels) = cifar10.load_data()
train_data = normalize(train_data)
test_data = normalize(test_data)
x = np.concatenate((train_data, test_data), axis=0)
# y = np.concatenate((train_labels, test_labels), axis=0).astype(np.int)
seed = 777
np.random.seed(seed)
np.random.shuffle(x)
# np.random.seed(seed)
# np.random.shuffle(y)
x = np.asarray([scipy.misc.imresize(x_img, [size, size]) for x_img in x])
return x
示例15: load_mnist
# 需要导入模块: from keras.datasets import mnist [as 别名]
# 或者: from keras.datasets.mnist import load_data [as 别名]
def load_mnist():
"""
load and pre-process the MNIST data
"""
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_last':
x_train = x_train.reshape((x_train.shape[0], 28, 28, 1))
x_test = x_test.reshape((x_test.shape[0], 28, 28, 1))
else:
x_train = x_train.reshape((x_train.shape[0], 1, 28, 28))
x_test = x_test.reshape((x_test.shape[0], 1, 28, 28))
# standardise the dataset:
x_train = np.array(x_train).astype('float32') / 255
x_test = np.array(x_test).astype('float32') / 255
# shuffle the data:
perm = np.random.permutation(x_train.shape[0])
x_train = x_train[perm]
y_train = y_train[perm]
return (x_train, y_train), (x_test, y_test)