<ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>


      提示:建議先看day36-38的內(nèi)容

      TensorFlow? 是一個采用數(shù)據(jù)流圖(data flow
      graphs),用于數(shù)值計算的開源軟件庫。節(jié)點(Nodes)在圖中表示數(shù)學操作,圖中的線(edges)則表示在節(jié)點間相互聯(lián)系的多維數(shù)據(jù)數(shù)組,即張量(tensor)。它靈活的架構(gòu)讓你可以在多種平臺上展開計算,例如臺式計算機中的一個或多個CPU(或GPU),服務器,移動設備等等。

      TensorFlow
      最初由Google大腦小組(隸屬于Google機器智能研究機構(gòu))的研究員和工程師們開發(fā)出來,用于機器學習和深度神經(jīng)網(wǎng)絡方面的研究,但這個系統(tǒng)的通用性使其也可廣泛用于其他計算領(lǐng)域。

      1、安裝庫tensorflow


      有些教程會推薦安裝nightly,它適用于在一個全新的環(huán)境下進行TensorFlow的安裝,默認會把需要依賴的庫也一起裝上。我使用的是anaconda,本文我們安裝的是純凈版的tensorflow,非常簡單,只需打開Prompt:
      pip?install?tensorflow
      安裝成功


      導入成功
      #導入keras from?tensorflow?import?keras #導入tensorflow import?tensorflow?as?tf
      注:有些教程中導入Keras用的是import tensorflow.keras as keras會提示No module named
      'tensorflow.keras'

      2、導入mnist數(shù)據(jù)

      在上篇文章中我們已經(jīng)提到過 MNIST 了,用有趣的方式解釋梯度下降算法
      <https://mp.weixin.qq.com/s?__biz=MzA4MjYwMTc5Nw==&mid=2648931235&idx=2&sn=5258d235f155a93a8fecc0d8558a01e4&chksm=8794e989b0e3609fdd82921f218e9c4d4a51316c15e5e38b627bc95171fef230e65391448fc9&token=123511318&lang=zh_CN&scene=21#wechat_redirect>
      它是一個收錄了許多 28 x 28 像素手寫數(shù)字圖片(以灰度值矩陣存儲)及其對應的數(shù)字的數(shù)據(jù)集,可以把它理解成下圖這個樣子:


      由于眾所周知的原因,Keras自帶minist數(shù)據(jù)集下載會報錯,無法下載。博客園崔小秋同學給出了很好的解決方法:

      1、找到本地keras目錄下的mnist.py文件,通常在這個目錄下。


      2、下載mnist.npz文件到本地,下載鏈接如下。
      https://pan.baidu.com/s/1C3c2Vn-_616GqeEn7hQQ2Q
      <https://pan.baidu.com/s/1C3c2Vn-_616GqeEn7hQQ2Q>
      3、修改mnist.py文件為以下內(nèi)容,并保存
      from?__future__?import?absolute_import from?__future__?import?division
      from?__future__?import?print_function from?..utils.data_utils?import?get_file
      import?numpy?as?np def?load_data(path='mnist.npz'):
      ????"""Loads?the?MNIST?dataset. #?Arguments
      ????????path:?path?where?to?cache?the?dataset?locally
      ????????????(relative?to?~/.keras/datasets). ????#?Returns
      ????????Tuple?of?Numpy?arrays:?`(x_train,?y_train),?(x_test,?y_test)`. ????"""
      ????path?=?'E:/Data/Mnist/mnist.npz'?#此處的path為你剛剛存放mnist.py的目錄。注意斜杠
      ????f?=?np.load(path) ????x_train,?y_train?=?f['x_train'],?f['y_train']
      ????x_test,?y_test?=?f['x_test'],?f['y_test'] ????f.close()
      ????return?(x_train,?y_train),?(x_test,?y_test)
      看一下數(shù)據(jù)
      mnist?=?tf.keras.datasets.mnist
      (x_train,?y_train),(x_test,?y_test)?=?mnist.load_data() print(x_train[0].shape)
      (28, 28)
      import?matplotlib.pyplot?as?plt plt.imshow(x_train[0],cmap=plt.cm.binary)
      plt.show()

      print(y_train[0])
      5
      對數(shù)據(jù)進行歸一化處理
      x_train?=?tf.keras.utils.normalize(x_train,?axis=1)
      x_test?=?tf.keras.utils.normalize(x_test,?axis=1)
      再看一下,圖像的像素值被限定在了 [0,1]
      plt.imshow(x_train[0],cmap=plt.cm.binary) plt.show()


      3 構(gòu)建與訓練模型我們使用 Keras 的 Sequential 模型(順序模型),順序模型是多個網(wǎng)絡層的線性堆疊。本文旨在介紹TensorFlow
      及Keras用法,不再展開,有興趣的同學們學習其具體用法,可以參考Keras文檔:
      https://keras.io/zh/getting-started/sequential-model-guide/
      <https://keras.io/zh/getting-started/sequential-model-guide/>
      model?=?tf.keras.models.Sequential()
      model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
      model.add(tf.keras.layers.Dense(128,?activation=tf.nn.relu))
      model.add(tf.keras.layers.Dense(128,?activation=tf.nn.relu))
      model.add(tf.keras.layers.Dense(10,?activation=tf.nn.softmax))
      model.compile(optimizer='adam',
      ??????????????loss='sparse_categorical_crossentropy',
      ??????????????metrics=['accuracy']) model.fit(x_train,?y_train,?epochs=3)
      我們構(gòu)建出的模型大概是這個樣子的,區(qū)別是我們的隱藏層有128個單元


      在訓練的過程中,我們會發(fā)現(xiàn)損失值(loss)在降低,而準確度(accuracy)在提高,最后達到了一個令人滿意的程度。
      Epoch 1/3
      60000/60000 ?- 8s 127us/step - loss: 0.2677 - acc: 0.9211Epoch 2/3
      60000/60000 ?- 8s 130us/step - loss: 0.1106 - acc: 0.9655Epoch 3/3
      60000/60000 ?- 8s 136us/step - loss: 0.0751 - acc: 0.9764

      4 測試模型
      val_loss,?val_acc?=?model.evaluate(x_test,?y_test) print(val_loss)
      print(val_acc)
      10000/10000 ?- 0s 45us/step0.0916121033909265
      0.9713
      損失和準確度看起來還湊合,嘗試識別訓練集
      predictions?=?model.predict(x_test) print(predictions)


      用 argmax 解析一下(就是找出最大數(shù)對應的索引,即為識別出的數(shù)字)
      import?numpy?as?np print(np.argmax(predictions[0]))
      7
      plt.imshow(x_test[0],cmap=plt.cm.binary) plt.show()


      OK,模型可以識別數(shù)字了。
      5、保存模型
      主要用于模型的存儲和恢復。
      model.save('epic_num_reader.model') #?加載保存的模型
      new_model?=?tf.keras.models.load_model('epic_num_reader.model') #?測試保存的模型
      predictions?=?new_model.predict(x_test)print(np.argmax(predictions[0]))


      看到這里的都是真愛,另推薦一個Keras教程

      Colab超火的Keras/TPU深度學習免費實戰(zhàn),有點Python基礎就能看懂的快速課程
      <https://mp.weixin.qq.com/s?__biz=MzA4MjYwMTc5Nw==&mid=2648930407&idx=2&sn=994ebf05f8fc6f98607b4e13cdf9ac81&chksm=8794ea4db0e3635bcbe34ea70f16de5c7c16e49ffd843bae4a2237d5b696c7e9a0636ff1f02c&token=123511318&lang=zh_CN&scene=21#wechat_redirect>

      參考:

      https://www.cnblogs.com/shinny/p/9283372.html
      <https://www.cnblogs.com/shinny/p/9283372.html>

      https://www.cnblogs.com/wj-1314/p/9579490.html
      <https://www.cnblogs.com/wj-1314/p/9579490.html>


      https://github.com/MLEveryday/100-Days-Of-ML-Code/blob/master/Code/Day%2039.ipynb

      <https://github.com/MLEveryday/100-Days-Of-ML-Code/blob/master/Code/Day%2039.ipynb>

      友情鏈接
      ioDraw流程圖
      API參考文檔
      OK工具箱
      云服務器優(yōu)惠
      阿里云優(yōu)惠券
      騰訊云優(yōu)惠券
      京東云優(yōu)惠券
      站點信息
      問題反饋
      郵箱:[email protected]
      QQ群:637538335
      關(guān)注微信

        <ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>
          yin娃sao货调教情趣用品店 | 人人看超碰| 黑人在线 | 国产做a视频 | 老熟女 露脸 嗷嗷叫91 | 免费靠逼视频网站 | 好湿好多水夹得我好紧 | 免费黄片网站视频 | 香蕉伊人在线 | 国产在线一卡 |