當前位置: 首頁>>代碼示例>>Python>>正文


Python mnist.load_data方法代碼示例

本文整理匯總了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) 
開發者ID:codekansas,項目名稱:gandlf,代碼行數:21,代碼來源:mnist_gan.py

示例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) 
開發者ID:codekansas,項目名稱:gandlf,代碼行數:21,代碼來源:reversing_gan.py

示例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.") 
開發者ID:eriklindernoren,項目名稱:Keras-GAN,代碼行數:26,代碼來源:data_loader.py

示例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') 
開發者ID:jtyoui,項目名稱:Jtyoui,代碼行數:20,代碼來源:HandWritingRecognition.py

示例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 
開發者ID:csxeba,項目名稱:brainforge,代碼行數:22,代碼來源:xp_elm.py

示例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])) 
開發者ID:negrinho,項目名稱:deep_architect,代碼行數:21,代碼來源:test_hyperband.py

示例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) 
開發者ID:lllcho,項目名稱:CAPTCHA-breaking,代碼行數:23,代碼來源:test_datasets.py

示例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() 
開發者ID:lllcho,項目名稱:CAPTCHA-breaking,代碼行數:5,代碼來源:test_datasets.py

示例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) 
開發者ID:harvitronix,項目名稱:super-simple-distributed-keras,代碼行數:23,代碼來源:datasets.py

示例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) 
開發者ID:harvitronix,項目名稱:super-simple-distributed-keras,代碼行數:23,代碼來源:datasets.py

示例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 
開發者ID:liuguiyangnwpu,項目名稱:MassImageRetrieval,代碼行數:20,代碼來源:DataSampler.py

示例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 
開發者ID:maxpumperla,項目名稱:elephas,代碼行數:22,代碼來源:hyperparam_optimization.py

示例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 
開發者ID:taki0112,項目名稱:Self-Attention-GAN-Tensorflow,代碼行數:20,代碼來源:utils.py

示例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 
開發者ID:taki0112,項目名稱:Self-Attention-GAN-Tensorflow,代碼行數:19,代碼來源:utils.py

示例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) 
開發者ID:dsgissin,項目名稱:DiscriminativeActiveLearning,代碼行數:27,代碼來源:main.py


注:本文中的keras.datasets.mnist.load_data方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。