1_kerasを使用するためのimport文
差分
このページの2つのバージョン間の差分を表示します。
| 次のリビジョン | 前のリビジョン | ||
| 1_kerasを使用するためのimport文 [2017/10/27] – 作成 adash333 | 1_kerasを使用するためのimport文 [2019/08/04] (現在) – [2. Import文の入力(Copy and Paste)] adash333 | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| - | ===== (1)Chainer2を使用するためのimport文 ===== | + | ===== (1)Kerasを使用するためのimport文 ===== |
| - | <ChainerでMNIST目次> | + | <wrap hi> |
| - | [[Chainer2プログラミングの全体図|(0)Chainer2プログラミングの全体図]]\\ | + | [[Kerasプログラミングの全体図]] |
| - | (1)Chainer2を使用するためのimport文\\ | + | -[[(1)Kerasを使用するためのimport文]] < |
| - | [[(2)データの準備・設定]]\\ | + | -[[(2)データ準備(Keras)]] |
| - | [[(3)モデルの記述]]\\ | + | -[[(3)モデル設定(Keras)]] |
| - | [[(4)モデルと最適化アルゴリズムの設定]]\\ | + | -[[(4)モデル学習(Keras)]] |
| - | [[(5)学習(Trainerを利用する場合)]]\\ | + | -[[(5)結果の出力(Keras)]] |
| - | [[(6)結果の出力]]\\ | + | -[[(6)学習結果の保存(Keras)]] |
| + | -[[(7)推測(Keras)]] | ||
| - | 最初に以下をコピペするだけです。 | + | 最初に以下をコピペするだけですが、プログラムごとに、さらにimport文の追加が必要になる場合があります。 |
| < | < | ||
| + | import keras | ||
| + | from keras.models import Sequential | ||
| + | from keras.layers import Dense, Dropout | ||
| + | from keras.optimizers import RMSprop | ||
| + | from keras.utils import np_utils | ||
| + | |||
| + | from sklearn.model_selection import train_test_split | ||
| + | |||
| import numpy as np | import numpy as np | ||
| - | import chainer | + | from PIL import |
| - | from chainer | + | import |
| - | from chainer import datasets, iterators, optimizers, serializers | + | |
| - | from chainer import Link, Chain, ChainList | + | |
| - | import chainer.functions as F | + | |
| - | import chainer.links as L | + | |
| - | from chainer.training | + | |
| </ | </ | ||
| + | 始めはかなりとっつきにくいimport文ですが、pythonの勉強をしていくにつれて、徐々に、慣れてきます。 | ||
| + | |||
| + | keras特有のもの以外のimport文については、以下のリンクをご覧ください。 | ||
| + | |||
| + | 機械学習で用いるpythonの”import xxx”まとめ\\ | ||
| + | 2017/ | ||
| + | http:// | ||
| + | |||
| + | |||
| + | 上記コードを見ても最初はさっぱりだと思いますが、[[(7)推測|(7)推測(Chainer)]]まで読み終えた後、もう一度このページに戻ってきて、「ああ、ここでこれを使用するためにこれをimportしたんだなあ」くらいに思い出していただければと思います。 | ||
| ===== 開発環境 ===== | ===== 開発環境 ===== | ||
| Windows 8.1\\ | Windows 8.1\\ | ||
| Anaconda \\ | Anaconda \\ | ||
| Python 3.5\\ | Python 3.5\\ | ||
| - | Chainer | + | Tensorflow 1.4\\ |
| + | Keras 2.0.9\\ | ||
| - | Chainerのインストール方法は[[Chainer2.0をWindowsにインストール]]をご覧下さい。 | + | Keras2.0のインストール方法は[[windowsにkeras2.0をインストール]]をご覧下さい。 |
| - | 以下は、上記リンクに記載のように、Anaconda Promptで、chainer2という仮想環境を作成し、chainer | + | 以下は、上記リンクに記載のように、Anaconda Promptで、keras2という仮想環境を作成し、Keras 2.0をインストールしてある前提で、話を進めさせて頂きます。 |
| ===== 手順 ===== | ===== 手順 ===== | ||
| 今回から、 | 今回から、 | ||
| - | Chainer: ビギナー向けチュートリアル Vol.1\\ | ||
| - | https:// | ||
| - | に従って、jupyter notebook上で、Chainer2でMNIST手書き文字認識の機械学習を行い、Chainer2プログラミングを学んでいきます。 | ||
| - | ==== 1. Jupyter Notebookの起動 ==== | + | に従って、jupyter notebook上で、Keras2でMNIST手書き文字認識の機械学習を行い、Keras2プログラミングを学んでいきます。 |
| - | C:/ ドライブ下に、chainer2/ フォルダを作成します。 | + | |
| - | {{: | + | ==== 0. AnacondaのインストールとKeras2仮想環境 ==== |
| - | Windowsのスタートボタンから、Anaconda | + | (前提1)WindowsパソコンにAnacondaをインストール済み。\\ |
| + | インストールしていない場合は、以下をご覧になり、インストールしておいてください。\\ [[http:// | ||
| - | < | + | (前提2)さらに、Anaconda Prompt上でkeras2.0をインストールしておく。\\ |
| - | cd C:/ | + | 具体的な方法については、[[windowsにkeras2.0をインストール]]をご覧ください。 |
| - | activate chainer2 | + | |
| + | ==== 1. Jupyter Notebookの開始 ==== | ||
| + | |||
| + | Windowsのスタートボタンから、Anaconda Promptを起動 | ||
| + | |||
| + | {{: | ||
| + | |||
| + | 今回は、C:/ | ||
| + | |||
| + | Anaconda Prompt上で、以下のコマンドを入力 | ||
| + | < | ||
| + | cd c:/ | ||
| + | </ | ||
| + | keras2仮想環境を起動 | ||
| + | < | ||
| + | activate keras2 | ||
| + | </ | ||
| + | Jupyter Notebookの起動 | ||
| + | < | ||
| jupyter notebook | jupyter notebook | ||
| </ | </ | ||
| - | {{:pasted:20171019-224232.png}} | + | {{:pasted:20171106-041410.png}} |
| - | この後、Webブラウザ(Chromeがオススメです。)が自動的に開き、以下のような画面になります。 | + | すると、ブラウザ(Chromeがお勧めです)が自動的に開いて、以下のような画面になるので、画面右側の方の「New」>「python3」の順にクリックして、新規Jupyter Notebookを作成。 |
| - | {{:pasted:20171019-224809.png}} | + | {{:pasted:20171103-173912.png}} |
| - | ==== 2. ipynbファイルの作成と、Import文の入力(Copy and Paste) ==== | + | 新しいJupyter Notebookが開かれるので、画面上の方の「Untitled」をクリックして、名前を、「train_MNIST_MLP」に変更。 |
| - | 画面右上の、「New」をクリックして、Python 3 (または、chainer2)をクリックして、新しいipynbファイルを作成する。 | + | |
| - | {{:pasted:20171019-224643.png}} | + | {{:pasted:20171103-175203.png}} |
| - | 以下のような画面になるので、一番上の、Untitled をクリックして、MNIST_MLP と入力してEnter。 | + | {{: |
| - | {{: | + | 以下のような状態からスタートします。 |
| - | {{:pasted:20171019-225238.png}} | + | {{:pasted:20171106-042015.png}} |
| - | 以下のような画面になる。 | + | ==== 2. Import文の入力(Copy and Paste) ==== |
| - | {{: | + | 次のような状態になっていると思われます。 |
| - | 上図の部分に、Chainerプログラミングを開始するために必要な、以下のImport文をコピペする。 | + | {{: |
| + | |||
| + | 上図の部分に、Kerasプログラミングを開始するために必要な、以下のImport文をコピペします。 | ||
| < | < | ||
| + | import keras | ||
| + | from keras.models import Sequential | ||
| + | from keras.layers import Dense, Dropout | ||
| + | from keras.optimizers import RMSprop | ||
| + | from keras.utils import np_utils | ||
| + | |||
| + | from sklearn.model_selection import train_test_split | ||
| + | |||
| import numpy as np | import numpy as np | ||
| - | import chainer | + | from PIL import |
| - | from chainer | + | import |
| - | from chainer import datasets, iterators, optimizers, serializers | + | |
| - | from chainer import Link, Chain, ChainList | + | |
| - | import chainer.functions as F | + | |
| - | import chainer.links as L | + | |
| - | from chainer.training | + | |
| </ | </ | ||
| - | さらに、Shift + Enter を押すてから、数秒待つと、以下のようになる。 | + | さらに、Shift + Enter を押してから、数秒待つと、以下のようになります。 |
| - | {{:pasted:20171019-225550.png}} | + | {{:pasted:20171106-042357.png}} |
| これで、Import文の入力は終了です。 | これで、Import文の入力は終了です。 | ||
| - | 次に、[[(2)データの準備・設定]]を行っていきます。 | + | 次に、<wrap hi>[[(2)データ準備(Keras)]]</ |
| ===== 参考文献 ===== | ===== 参考文献 ===== | ||
| - | Chainer: ビギナー向けチュートリアル Vol.1 | + | 初めてKerasプログラミングをやるときの超おすすめ本。\\ |
| - | mitmul | + | |
| - | 2017年05月18日に更新\\ | + | |
| - | https:// | + | |
| < | < | ||
| - | <iframe style=" | + | |
| + | <iframe style=" | ||
| + | |||
| + | & | ||
| + | |||
| + | <iframe style=" | ||
| </ | </ | ||
| + | |||
| + | |||
| + | 機械学習で用いるpythonの”import xxx”まとめ | ||
| + | 2017/10/25 | ||
| + | http:// | ||
| + | |||
| ===== リンク ===== | ===== リンク ===== | ||
| - | <KerasでMNIST目次> | + | 次 [[(2)データ準備(Keras)]] |
| - | [[Kerasプログラミングの全体図]]\\ | + | |
| - | [[(1)Kerasを使用するためのimport文]]\\ | + | 前 [[Kerasプログラミングの全体図]] |
| - | [[(2)データの準備・設定]]\\ | + | |
| - | [[(3)モデルの記述]]\\ | + | |
| - | [[(4)モデルと最適化アルゴリズムの設定]]\\ | + | <wrap hi> |
| - | [[(5)学習(Trainerを利用しない場合)]]\\ | + | [[Kerasプログラミングの全体図]] |
| - | [[(6)結果の出力]]\\ | + | -[[(1)Kerasを使用するためのimport文]] |
| + | -[[(2)データ準備(Keras)]] | ||
| + | -[[(3)モデル設定(Keras)]] | ||
| + | -[[(4)モデル学習(Keras)]] | ||
| + | -[[(5)結果の出力(Keras)]] | ||
| + | -[[(6)学習結果の保存(Keras)]] | ||
| + | -[[(7)推測(Keras)]] | ||
1_kerasを使用するためのimport文.1509101227.txt.gz · 最終更新: 2018/10/07 (外部編集)
