yuji38kwmt
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `Tensorflow` `学習記録` --- # Tensorflow Tutorialを試してみて by yuji38kwmt 2017/08/19 --- <!-- .slide: data-background="#383838" --> # 学んだこと ---- <!-- .slide: data-background="#383838" --> # 参考にしたサイト [MNIST For ML Beginners(公式)](https://www.tensorflow.org/get_started/mnist/beginners) [TensorFlowチュートリアル - ML初心者のためのMNIST(翻訳)](http://qiita.com/KojiOhki/items/ff6ae04d6cf02f1b6edf) ---- <!-- .slide: data-background="#383838" --> ## 学んだ用語など * MNIST:Mixed National Institute of Standards and Technology. 手書き数字画像データベース * 訓練データで学習(適切なパラメータを決定)し、テストデータでモデルの精度を確認する。 --- <!-- .slide: data-background="#383838" --> # ソースコードの解説 下記サイトのコードを実行した。 [Tensorflow Tutorial mnist_softmax.py](https://github.com/tensorflow/tensorflow/blob/r0.7/tensorflow/examples/tutorials/mnist/mnist_softmax.py) ※一部修正 ---- <!-- .slide: data-background="#383838" --> ## モデルの生成 ```python # xは特定の値ではない # 数値「784」: MNISデータ1個のサイズ(28px×28px=784) x = tf.placeholder(tf.float32, [None, 784]) # 重みをゼロで初期化 # 数値「10」:画像から判断する結果の数(数字0~9) W = tf.Variable(tf.zeros([784,10])) # バイアスをゼロで初期化 b = tf.Variable(tf.zeros([10])) # モデルの実装 y = tf.nn.softmax(tf.matmul(x, W) + b) ``` ---- <!-- .slide: data-background="#383838" --> ### モデルの生成の解説 * モデルの数式: $\boldsymbol{y}=\text{softmax}(\boldsymbol{Wx}+\boldsymbol{b})$ * $y[i]$: `[0.2, 0.7, 0, 0, 0, 0, 0, 0, 0, 0.1 ]`のような配列。画像から判定した数字の確率を表す。この場合、数字「1」の確率が0.7で最も高い。 * softmax関数の中身: $\text{softmax}(x)_i = \frac{\exp(x_i)}{\sum_j \exp(x_j)}$ ---- <!-- .slide: data-background="#383838" --> ## 誤差と最適化方法を決定 ```python # 交差エントロピー誤算を算出 y_ = tf.placeholder(tf.float32, [None, 10]) cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) # 最適化方法を定義。降下勾配法。 train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) ``` ---- <!-- .slide: data-background="#383838" --> ## 誤差と最適化方法の解説 * 交差エントロピー誤差の数式: $H_{y'}(y) = -\sum_i y'_i \log(y_i)$ * 降下勾配法を使って、交差エントロピー誤差を最小化するパラメータを探す。 * "0.01"は学習率。どれだけパラメータを更新するかを決める。 ---- <!-- .slide: data-background="#383838" --> ## 訓練(学習) ```python # セッションを定義して、パラメータを初期化する sess = tf.InteractiveSession() tf.global_variables_initializer().run() # 訓練ステップを 1000 回実行する # 訓練セットから 100 個のランダムなデータポイントの「バッチ」を取得する # 確率的訓練 for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) ``` * 訓練データ60,000個の中から、毎回ランダムに100個のデータ(バッチ)抽出して、それを最適化する。確率的訓練法。 ---- <!-- .slide: data-background="#383838" --> ## テストデータを使って精度を検証 ```python # テストデータを使って、精度を検証 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels})) ``` ---- <!-- .slide: data-background="#383838" --> ## 誤差と精度のグラフを表示 精度の計算には訓練データを用いる。 ```python # 精度のモデルを定義 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 訓練開始 loss_list = [] #誤差のリスト accuracy_list = [] #精度のリスト for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) # 誤差と精度(訓練データ)を計算 loss_val, accuracy_val = sess.run([cross_entropy, accuracy], feed_dict={x: batch_xs, y_: batch_ys}) loss_list.append(loss_val) accuracy_list.append(accuracy_val) import matplotlib.pylab as plt # 誤差のグラフを表示 plt.plot(loss_list) plt.show() # 精度のグラフを表示 plt.plot(accuracy_list) plt.show() ``` ---- <!-- .slide: data-background="#383838" --> ### 誤差の推移 ![](https://i.imgur.com/JFbK2eL.png) ---- <!-- .slide: data-background="#383838" --> ### 精度(訓練データ使用)の推移 ![](https://i.imgur.com/qqGH2AE.png) * テストデータでの精度:0.9153 ---- <!-- .slide: data-background="#383838" --> ## Adams法で計算 >AdamOptimizerも確率的勾配降下法のひとつであるが、他の方法より新しい(2015年に発表) >特徴的なことは、収束が指数関数的ではないことである。 下記サイトより引用 [TensorFlowのOptimizerを比較する(ベジェ曲線編)](http://qiita.com/cnloni/items/ad7dcb7521b936d9fc18) ```python # 学習率を決める必要がない train_step = tf.train.AdamOptimizer().minimize(cross_entropy) ``` ---- <!-- .slide: data-background="#383838" --> ## Adams法の誤差の推移 ![](https://i.imgur.com/fe9Xn9W.png) ---- <!-- .slide: data-background="#383838" --> ## Adams法の精度の推移 ![](https://i.imgur.com/RuNNMaN.png) * テストデータでの精度:0.9118 ---- <!-- .slide: data-background="#383838" --> ## Adams法の評価 * 降下勾配法より、誤差がゆるやかに減衰している ---- <!-- .slide: data-background="#383838" --> ## 実行したソース Gistにアップしました。 [mnist_降下勾配法.py](https://gist.githubusercontent.com/yuji38kwmt/20194cc0b8b4415b5faeaa910fbb0dde/raw/5d14ee5f00b80532e3e53371257d79537eb512a0/mnist_%25E7%25A2%25BA%25E7%258E%2587%25E7%259A%2584%25E9%2599%258D%25E4%25B8%258B%25E5%258B%25BE%25E9%2585%258D%25E6%25B3%2595.py)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully