W!o+ 的《小伶鼬工坊演義》︰神經網絡【MNIST】三

若是每次測試那七十四行的程式,都得重新訓練神經網絡之

是由稱為神經元神經細胞所組成的神經系統控制中心,是所有脊椎動物和大部分無脊椎動物都具有的一個器官,只有少數的無脊椎動物沒有腦,例如海綿水母、成年的海鞘海星,它們以分散或者局部的神經網絡代替。

許多動物的腦位於部,通常是靠近主要的感覺器官,例如視覺聽覺前庭系統味覺嗅覺。腦是脊椎動物身體中最複雜的器官 。在普通人類的大腦皮質(腦中最大的部分)中,包含150-330億個神經元,[1]每一個神經元都通過突觸和其他數千個神經元相連接 。這些神經元之間通過稱作軸突原生質纖維進行較長距離互相聯結,可以將一種稱作動作電位的衝動信號,在腦的不同區域之間或者向身體的特定接收細胞傳遞。脊椎動物的腦由顱骨保護。腦與脊髓構成中樞神經系統。中樞神經系統的細胞依靠複雜的聯繫來處理傳遞信息。腦是感情思考、生命得以維持的中樞。它控制和協調行為、身體內穩態(身體功能,例如心跳血壓體溫等)以及精神活動(例如認知情感記憶學習)。

生理上來說,腦的功能就是控制身體的其他器官。腦對其他器官的作用方式,一是調製肌肉的運動模式,二是通過分泌一些稱為荷爾蒙的化學物質。集中的控制方式,可以對環境的變化做出迅速而一致的反應。 一些基本的反應,例如反射,可以通過脊髓或者周邊神經節來控制,然而基於多種感官輸入,有心智、有目的的動作,只有通過腦中樞的整合能力才能控制。

關於單個腦細胞的運作機制,現今已經有了比較詳細的了解;然而數以兆億的神經元如何以集群的方式合作,還是一個未解決的問題 。[2]現代神經科學中,新近的模型將腦看作一種生物計算機,雖然運行的機制和電子計算機很不一樣,但是它們從周圍世界中獲得信息、存儲信息、以多種方式處理信息的功能是類似的,它有點像計算機中的中央處理器(CPU)。

本文會對各種動物的腦進行比較,特別是脊椎動物的腦,而人腦將被作為各種腦的其中一種進行討論。人腦的特別之處會在人腦條目中探討,因為其中很多話題在人腦的前提下討論,內容會豐富得多 。其中最重要的,是腦疾病與腦損傷造成的後果,它會被放在人腦條目中探討,因為人腦的大多數常見疾病並不見於其他物種,即使有,它們的表現形式也可能不同。

220px-Frontal_lobe_animation

人腦立體旋轉剖面圖

───

 

,顯然有點荒謬!所以此處設想如何『保存大腦』?甚或該怎麼『移植』的呢??研究一下『network.py』︰

"""
network.py
~~~~~~~~~~

A module to implement the stochastic gradient descent learning
algorithm for a feedforward neural network.  Gradients are calculated
using backpropagation.  Note that I have focused on making the code
simple, easily readable, and easily modifiable.  It is not optimized,
and omits many desirable features.
"""

#### Libraries
# Standard library
import random

# Third-party libraries
import numpy as np

class Network(object):

    def __init__(self, sizes):
        """The list ``sizes`` contains the number of neurons in the
        respective layers of the network.  For example, if the list
        was [2, 3, 1] then it would be a three-layer network, with the
        first layer containing 2 neurons, the second layer 3 neurons,
        and the third layer 1 neuron.  The biases and weights for the
        network are initialized randomly, using a Gaussian
        distribution with mean 0, and variance 1.  Note that the first
        layer is assumed to be an input layer, and by convention we
        won't set any biases for those neurons, since biases are only
        ever used in computing the outputs from later layers."""
        self.num_layers = len(sizes)
        self.sizes = sizes
        self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
        self.weights = [np.random.randn(y, x)
                        for x, y in zip(sizes[:-1], sizes[1:])]

 

就會發現這個『腦』就是保存在

self.sizes

self.biases

self.weights

裡。如是假使知道『numpy』的 array 檔案存取之法︰

numpy.savez

numpy.savez(file, *args, **kwds)
Save several arrays into a single file in uncompressed .npz format.

If arguments are passed in with no keywords, the corresponding variable names, in the .npz file, are ‘arr_0’, ‘arr_1’, etc. If keyword arguments are given, the corresponding variable names, in the .npz file will match the keyword names.

Parameters:

file : str or file

Either the file name (string) or an open file (file-like object) where the data will be saved. If file is a string, the .npz extension will be appended to the file name if it is not already there.

args : Arguments, optional

Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside savez, the arrays will be saved with names “arr_0”, “arr_1”, and so on. These arguments can be any expression.

kwds : Keyword arguments, optional

Arrays to save to the file. Arrays will be saved in the file with the keyword names.

Returns:

None

See also

save
Save a single array to a binary file in NumPy format.
savetxt
Save an array to a file as plain text.
savez_compressed
Save several arrays into a compressed .npz archive

───

 

以及

numpy.load

numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding=’ASCII’)
Load arrays or pickled objects from .npy, .npz or pickled files.

Parameters:

file : file-like object or string

The file to read. File-like objects must support the seek() and read() methods. Pickled files require that the file-like object support the readline() method as well.

mmap_mode : {None, ‘r+’, ‘r’, ‘w+’, ‘c’}, optional

If not None, then memory-map the file, using the given mode (see numpy.memmap for a detailed description of the modes). A memory-mapped array is kept on disk. However, it can be accessed and sliced like any ndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory.

allow_pickle : bool, optional

Allow loading pickled object arrays stored in npy files. Reasons for disallowing pickles include security, as loading pickled data can execute arbitrary code. If pickles are disallowed, loading object arrays will fail. Default: True

fix_imports : bool, optional

Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. If fix_imports is True, pickle will try to map the old Python 2 names to the new names used in Python 3.

encoding : str, optional

What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. Values other than ‘latin1’, ‘ASCII’, and ‘bytes’ are not allowed, as they can corrupt numerical data. Default: ‘ASCII’

Returns:

result : array, tuple, dict, etc.

Data stored in the file. For .npz files, the returned instance of NpzFile class must be closed to avoid leaking file descriptors.

Raises:

IOError

If the input file does not exist or cannot be read.

ValueError

The file contains an object array, but allow_pickle=False given.

See also

save, savez, savez_compressed, loadtxt

memmap
Create a memory-map to an array stored in a file on disk.

───

 

再輔之以 list → array 轉換之法︰

Array creation

Introduction

There are 5 general mechanisms for creating arrays:

  1. Conversion from other Python structures (e.g., lists, tuples)
  2. Intrinsic numpy array array creation objects (e.g., arange, ones, zeros, etc.)
  3. Reading arrays from disk, either from standard or custom formats
  4. Creating arrays from raw bytes through the use of strings or buffers
  5. Use of special library functions (e.g., random)

This section will not cover means of replicating, joining, or otherwise expanding or mutating existing arrays. Nor will it cover creating object arrays or structured arrays. Both of those are covered in their own sections.

Converting Python array_like Objects to Numpy Arrays

In general, numerical data arranged in an array-like structure in Python can be converted to arrays through the use of the array() function. The most obvious examples are lists and tuples. See the documentation for array() for details for its use. Some objects may support the array-protocol and allow conversion to arrays this way. A simple way to find out if the object can be converted to a numpy array using array() is simply to try it interactively and see if it works! (The Python Way).

Examples:

>>> x = np.array([2,3,1,0])
>>> x = np.array([2, 3, 1, 0])
>>> x = np.array([[1,2.0],[0,0],(1+1j,3.)]) # note mix of tuple and lists,
    and types
>>> x = np.array([[ 1.+0.j, 2.+0.j], [ 0.+0.j, 0.+0.j], [ 1.+1.j, 3.+0.j]])

 

果可保存『大腦』乎??!!

>>> s = network.np.array(net.sizes)
>>> w1 = net.weights[0]
>>> w2 = net.weights[1]
>>> b1 = net.biases[0]
>>> b2 = net.biases[1]
>>> network.np.savez("swb",s=s,w1=w1,w2=w2,b1=b1,b2=b2)
>>> npzfile = network.np.load("swb.npz")
>>> npzfile.files
['s', 'b2', 'w2', 'w1', 'b1']
>>> list(npzfile["s"])
[784, 30, 10] 
>>>