当前位置: 首页>>代码示例>>Python>>正文


Python mnist.MNIST属性代码示例

本文整理汇总了Python中mnist.MNIST属性的典型用法代码示例。如果您正苦于以下问题:Python mnist.MNIST属性的具体用法?Python mnist.MNIST怎么用?Python mnist.MNIST使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在mnist的用法示例。


在下文中一共展示了mnist.MNIST属性的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: import mnist [as 别名]
# 或者: from mnist import MNIST [as 别名]
def __init__(self, loc='data/raw'):
        """
        Dataloader for the MNIST data. Relies on this library
        https://pypi.python.org/pypi/python-mnist/0.3
        :param loc:
        """
        mndata = MNIST(loc)
        self.data = {}

        # train data
        images, labels = mndata.load_training()
        images = np.array(images)
        labels = np.array(labels).astype(np.int64)

        self.data['X_train'] = self.normalize(images)
        self.data['y_train'] = labels

        # test data
        images, labels = mndata.load_testing()
        images = np.array(images)
        labels = np.array(labels).astype(np.int64)

        self.data['X_test'] = self.normalize(images)
        self.data['y_test'] = labels 
开发者ID:RobRomijnders,项目名称:bayes_nn,代码行数:26,代码来源:data_loader.py

示例2: initialize_parameters

# 需要导入模块: import mnist [as 别名]
# 或者: from mnist import MNIST [as 别名]
def initialize_parameters():
    mnist_common = mnist.MNIST(mnist.file_path,
        'mnist_params.txt',
        'keras',
        prog='mnist_mlp',
        desc='MNIST example'
    )

    # Initialize parameters
    gParameters = candle.finalize_parameters(mnist_common)
    csv_logger = CSVLogger('{}/params.log'.format(gParameters))

    return gParameters 
开发者ID:ECP-CANDLE,项目名称:Benchmarks,代码行数:15,代码来源:mnist_mlp_candle.py

示例3: initialize_parameters

# 需要导入模块: import mnist [as 别名]
# 或者: from mnist import MNIST [as 别名]
def initialize_parameters():
    mnist_common = mnist.MNIST(mnist.file_path,
        'mnist_params.txt',
        'keras',
        prog='mnist_cnn',
        desc='MNIST CNN example'
    )

    # Initialize parameters
    gParameters = candle.finalize_parameters(mnist_common)
    csv_logger = CSVLogger('{}/params.log'.format(gParameters))

    return gParameters 
开发者ID:ECP-CANDLE,项目名称:Benchmarks,代码行数:15,代码来源:mnist_cnn_candle.py

示例4: __init__

# 需要导入模块: import mnist [as 别名]
# 或者: from mnist import MNIST [as 别名]
def __init__(self, prob):
        """load mnist dataset"""
        print("Loading MNIST dataset...")
        mndata = MNIST('./data/mnist/')

        mnist_train_images, mnist_train_labels = mndata.load_training()

        mnist_train_images = np.asarray(mnist_train_images)
        mnist_train_images = normalize(mnist_train_images)
        mnist_train_labels = np.asarray(mnist_train_labels)

        """divide datset by label"""
        print("Dividing dataset...")
        sorted_train_images = []
        sorted_train_labels = []        

        for label in range(0,10):
            train_index = np.where(mnist_train_labels == label)
            sorted_train_images.append(mnist_train_images[train_index[0]])
            sorted_train_labels.append(np.asarray([label] * len(train_index[0])))
            
        """add salt_and_pepper noise"""
        print("Adding salt and pepper noise...")
        shape = 28 * 28 ##image shape of mnist
        self.train_images = []
        self.train_labels = sorted_train_labels
        for images in sorted_train_images:
            noise_images = []
            for image in images:
                noise_image = salt_and_pepper(image, prob, shape)
                noise_images.append(noise_image)
            self.train_images.append(noise_images) 
开发者ID:kimhc6028,项目名称:pathnet-pytorch,代码行数:34,代码来源:mnist_dataset.py

示例5: run

# 需要导入模块: import mnist [as 别名]
# 或者: from mnist import MNIST [as 别名]
def run():
  TorchModel = PyTorchHelpers.load_lua_class('torch_model.lua', 'TorchModel')
  torchModel = TorchModel(backend, 28, 10)

  mndata = MNIST('../../data/mnist')
  imagesList, labelsList = mndata.load_training()
  labels = np.array(labelsList, dtype=np.uint8)
  images = np.array(imagesList, dtype=np.float32)
  labels += 1  # since torch/lua labels are 1-based
  N = labels.shape[0]
  print('loaded mnist training data')

  if numTrain > 0:
    N = min(N, numTrain)
  print('numExamples N', N)
  numBatches = N // batchSize
  for epoch in range(numEpochs):
    epochLoss = 0
    epochNumRight = 0
    for b in range(numBatches):
      res = torchModel.trainBatch(
        learningRate,
        images[b * batchSize:(b+1) * batchSize],
        labels[b * batchSize:(b+1) * batchSize])
#      print('res', res)
      numRight = res['numRight']
      loss = res['loss']
      epochNumRight += numRight
      epochLoss += loss
      print('epoch ' + str(epoch) + ' batch ' + str(b) + ' accuracy: ' + str(numRight * 100.0 / batchSize) + '%')
    print('epoch ' + str(epoch) + ' accuracy: ' + str(epochNumRight * 100.0 / N) + '%')

  print('finished training')
  print('loading test data...')
  imagesList, labelsList = mndata.load_testing()
  labels = np.array(labelsList, dtype=np.uint8)
  images = np.array(imagesList, dtype=np.float32)
  labels += 1  # since torch/lua labels are 1-based
  N = labels.shape[0]
  print('loaded mnist testing data')

  numBatches = N // batchSize
  epochLoss = 0
  epochNumRight = 0
  for b in range(numBatches):
    predictions = torchModel.predict(images[b * batchSize:(b+1) * batchSize]).asNumpyTensor().reshape(batchSize)
    labelsBatch = labels[b * batchSize:(b+1) * batchSize]
    numRight = (predictions == labelsBatch).sum()
    epochNumRight += numRight
  print('test results: accuracy: ' + str(epochNumRight * 100.0 / N) + '%') 
开发者ID:hughperkins,项目名称:pytorch,代码行数:52,代码来源:runner.py


注:本文中的mnist.MNIST属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。