最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python MNIST手寫識別數(shù)據(jù)調(diào)用API的方法

 更新時間:2018年08月08日 10:54:35   作者:caichao08  
這篇文章主要介紹了python MNIST手寫識別數(shù)據(jù)調(diào)用API的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

MNIST數(shù)據(jù)集比較小,一般入門機(jī)器學(xué)習(xí)都會采用這個數(shù)據(jù)集來訓(xùn)練

下載地址:yann.lecun.com/exdb/mnist/

有4個有用的文件:
train-images-idx3-ubyte: training set images
train-labels-idx1-ubyte: training set labels
t10k-images-idx3-ubyte: test set images
t10k-labels-idx1-ubyte: test set labels

The training set contains 60000 examples, and the test set 10000 examples. 數(shù)據(jù)集存儲是用binary file存儲的,黑白圖片。

下面給出load數(shù)據(jù)集的代碼:

import os
import struct
import numpy as np
import matplotlib.pyplot as plt

def load_mnist():
  '''
  Load mnist data
  http://yann.lecun.com/exdb/mnist/

  60000 training examples
  10000 test sets

  Arguments:
    kind: 'train' or 'test', string charater input with a default value 'train'

  Return:
    xxx_images: n*m array, n is the sample count, m is the feature number which is 28*28
    xxx_labels: class labels for each image, (0-9)
  '''

  root_path = '/home/cc/deep_learning/data_sets/mnist'

  train_labels_path = os.path.join(root_path, 'train-labels.idx1-ubyte')
  train_images_path = os.path.join(root_path, 'train-images.idx3-ubyte')

  test_labels_path = os.path.join(root_path, 't10k-labels.idx1-ubyte')
  test_images_path = os.path.join(root_path, 't10k-images.idx3-ubyte')

  with open(train_labels_path, 'rb') as lpath:
    # '>' denotes bigedian
    # 'I' denotes unsigned char
    magic, n = struct.unpack('>II', lpath.read(8))
    #loaded = np.fromfile(lpath, dtype = np.uint8)
    train_labels = np.fromfile(lpath, dtype = np.uint8).astype(np.float)

  with open(train_images_path, 'rb') as ipath:
    magic, num, rows, cols = struct.unpack('>IIII', ipath.read(16))
    loaded = np.fromfile(train_images_path, dtype = np.uint8)
    # images start from the 16th bytes
    train_images = loaded[16:].reshape(len(train_labels), 784).astype(np.float)

  with open(test_labels_path, 'rb') as lpath:
    # '>' denotes bigedian
    # 'I' denotes unsigned char
    magic, n = struct.unpack('>II', lpath.read(8))
    #loaded = np.fromfile(lpath, dtype = np.uint8)
    test_labels = np.fromfile(lpath, dtype = np.uint8).astype(np.float)

  with open(test_images_path, 'rb') as ipath:
    magic, num, rows, cols = struct.unpack('>IIII', ipath.read(16))
    loaded = np.fromfile(test_images_path, dtype = np.uint8)
    # images start from the 16th bytes
    test_images = loaded[16:].reshape(len(test_labels), 784)  

  return train_images, train_labels, test_images, test_labels

再看看圖片集是什么樣的:

def test_mnist_data():
  '''
  Just to check the data

  Argument:
    none

  Return:
    none
  '''
  train_images, train_labels, test_images, test_labels = load_mnist()
  fig, ax = plt.subplots(nrows = 2, ncols = 5, sharex = True, sharey = True)
  ax =ax.flatten()
  for i in range(10):
    img = train_images[i][:].reshape(28, 28)
    ax[i].imshow(img, cmap = 'Greys', interpolation = 'nearest')
    print('corresponding labels = %d' %train_labels[i])

if __name__ == '__main__':
  test_mnist_data()

跑出的結(jié)果如下:


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python collections模塊的使用

    python collections模塊的使用

    這篇文章主要介紹了python collections模塊的使用,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-10-10
  • Python爬取視頻時長場景實踐示例

    Python爬取視頻時長場景實踐示例

    這篇文章主要為大家介紹了Python獲取視頻時長場景實踐示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 使用python腳本自動生成K8S-YAML的方法示例

    使用python腳本自動生成K8S-YAML的方法示例

    這篇文章主要介紹了使用python腳本自動生成K8S-YAML的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • pytorch + visdom CNN處理自建圖片數(shù)據(jù)集的方法

    pytorch + visdom CNN處理自建圖片數(shù)據(jù)集的方法

    這篇文章主要介紹了pytorch + visdom CNN處理自建圖片數(shù)據(jù)集的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • 解決BN和Dropout共同使用時會出現(xiàn)的問題

    解決BN和Dropout共同使用時會出現(xiàn)的問題

    這篇文章主要介紹了解決BN和Dropout共同使用時會出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python鍵值互換的實現(xiàn)示例

    Python鍵值互換的實現(xiàn)示例

    Python鍵值互換是一種對Python字典類型中鍵值對進(jìn)行反轉(zhuǎn)的技術(shù),有時候,我們需要以值作為鍵,以鍵作為值來操作字典,這時候就需要用到鍵值互換的技術(shù),本文主要介紹了Python鍵值互換的實現(xiàn)示例,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 深入詳解Python中Micawber庫的使用

    深入詳解Python中Micawber庫的使用

    Python Micawber庫就是一個用于解析和嵌入媒體資源的工具,它可以自動識別各種媒體資源的URL,下面就跟隨小編一起來看看它的具體使用吧
    2022-06-06
  • Python: glob匹配文件的操作

    Python: glob匹配文件的操作

    這篇文章主要介紹了Python: glob匹配文件的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • python Django模板的使用方法

    python Django模板的使用方法

    這篇文章主要為大家介紹了python Django模板的使用方法,代碼很詳細(xì),感興趣的小伙伴們可以參考一下
    2016-01-01
  • 一文帶你了解Python與svg之間的操作

    一文帶你了解Python與svg之間的操作

    svgwrite是一個?Python?庫,用于生成簡單的?SVG?圖片。它提供了一組類似于繪圖的?API,使用者可以在?SVG?畫布上畫線、矩形、圓等圖形。本文主要介紹了如何利用svgwrite進(jìn)行SVG圖片的操作,需要的可以參考一下
    2023-01-01

最新評論

文化| 新乡县| 通城县| 凤凰县| 新兴县| 宣化县| 定襄县| 南开区| 永清县| 游戏| 渭南市| 佛学| 江城| 白水县| 泸定县| 广水市| 张家界市| 茶陵县| 苏州市| 嘉义市| 岑巩县| 大田县| 木里| 河南省| 酒泉市| 东台市| 岳普湖县| 巢湖市| 佛冈县| 岚皋县| 桐乡市| 自贡市| 台南市| 九台市| 铜川市| 岳普湖县| 寻乌县| 大新县| 沙坪坝区| 固镇县| 武陟县|