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

輪扁斲輪』說何事?書貴活讀,道通其意!

斲輪

莊子‧天道

世之所貴道者,書也。書不過語,語有貴也。語之所貴者,意也,意有所隨。意之所隨者,不可以言傳也,而世因貴言傳書。世雖貴之哉,猶不足貴也,為其貴非其 貴也。故視而可見者,形與色也;聽而可聞者,名與聲也。悲夫!世人以形色名聲為足以得彼之情。夫形色名聲,果不足以得彼之情,則知者不言,言者不知,而世 豈識之哉!

桓 公讀書於堂上,輪扁斲輪於堂下,釋椎鑿而上,問桓公曰:『敢問:公之所讀者,何言邪?』公曰:『聖人之言也。』曰:『聖人在乎?』公曰:『已死矣。』曰: 『然則君之所讀者,古人之糟魄已夫!』桓公曰:『寡人讀書,輪人安得議乎!有說則可,無說則死!』輪扁曰:『臣也以臣之事觀之。斲輪,徐則甘而不固,疾則 苦而不入,不徐不疾,得之於手而應於心,口不能言,有數存焉於其間。臣不能以喻臣之子,臣之子亦不能受之於臣,是以行年七十而老斲輪。古之人與其不可傳也 死矣,然則君之所讀者,古人之糟魄已夫!』

 

於是乎能『得手應心』耶??!!

因想起那個說『得心應手』的蘇軾有詩言︰

宋‧蘇軾《題西林壁》

橫看成嶺側成峰,
遠近高低各不同。
不識廬山真面目,
只緣身在此山中。

 

不知這個七十四行之小程式功夫修煉的如何哩?於是假

numpy.rot90

numpy.rot90(m, k=1)
Rotate an array by 90 degrees in the counter-clockwise direction.

The first two dimensions are rotated; therefore, the array must be at least 2-D.

Parameters:

m : array_like

Array of two or more dimensions.

k : integer

Number of times the array is rotated by 90 degrees.

Returns:

y : ndarray

Rotated array.

See also

fliplr
Flip an array horizontally.
flipud
Flip an array vertically.

───

 

測試一番。既是第五篇,就用五吧︰

>>> img_5 = training_data[0][0].reshape(28,28)
>>> plt.imshow(img_5,cmap='Greys', interpolation='nearest')
<matplotlib.image.AxesImage object at 0x5a76d90>
>>> plt.show()

 

Figure 5

 

【轉九十度】看成 0

>>> img_5r1 = network.np.rot90(img_5,1)
>>> plt.imshow(img_5r1,cmap='Greys', interpolation='nearest')
<matplotlib.image.AxesImage object at 0x552ea90>
>>> plt.show()
>>> img_5r1c = img_5r1.reshape(784,1)
>>> net.feedforward(img_5r1c)
array([[  9.48422668e-01],
       [  8.37509905e-10],
       [  6.24587095e-04],
       [  2.19023783e-10],
       [  5.85194584e-01],
       [  1.46136409e-11],
       [  1.81919304e-07],
       [  1.14141035e-06],
       [  1.71525730e-09],
       [  6.05105236e-03]])
>>> network.np.argmax(net.feedforward(img_5r1c))
0

 

Figure 5r1

 

【轉一百八十度】認作 5

>>> img_5r2 = network.np.rot90(img_5,2)
>>> plt.imshow(img_5r2,cmap='Greys', interpolation='nearest')
<matplotlib.image.AxesImage object at 0x5a71210>
>>> plt.show()
>>> img_5r2c = img_5r2.reshape(784,1)
>>> net.feedforward(img_5r2c)
array([[  4.19025498e-05],
       [  1.76313425e-05],
       [  3.55496816e-04],
       [  5.06004836e-06],
       [  1.39698665e-06],
       [  9.23738397e-01],
       [  6.00778782e-01],
       [  4.66450961e-11],
       [  6.11394572e-05],
       [  1.10825783e-12]])
>>> network.np.argmax(net.feedforward(img_5r2c))
5

 

Figure 5r2

 

【鏡裡觀象】以為 2

>>> img_5lr = network.np.fliplr(img_5)
>>> plt.imshow(img_5lr,cmap='Greys', interpolation='nearest')
<matplotlib.image.AxesImage object at 0x5a683d0>
>>> plt.show()
>>> img_5lrc = img_5lr.reshape(784,1)
>>> net.feedforward(img_5lrc)
array([[  4.82321281e-06],
       [  5.04183015e-06],
       [  9.69179259e-01],
       [  9.44724057e-03],
       [  4.64107830e-10],
       [  3.36075812e-06],
       [  1.55823494e-11],
       [  2.82510514e-06],
       [  2.71304177e-04],
       [  7.13868585e-10]])
>>> network.np.argmax(net.feedforward(img_5lrc))
2
>>> 

 

Figure 5lr

 

到底該說是好還是不好的呢???