1.起動方法(Pycharmを使っているとして)
PyCharmで[View] - [Tool Windows] - [Terminal]を選択し、以下のコマンドを入力。
tensorboard --logdir=logs
2.次にブラウザを立ち上げて「http://localhost:6006」と入力。
補足:tensorboardは機械学習プログラムの中のlogを読みにいくので、計算プログラム中に以下の記述を入れておく必要がある。
writer = tf.summary.FileWriter('logs', graph=sess.graph)
更にtensorboardを立ち上げても、エラーが出る、もしくは自分が見たい計算モデルが表示されていない場合、logフォルダ内のlogファイルを全部削除して、もう一度計算させる、つまり、新しいログファイルを作ると計算結果がうまく表示される場合がある。
2019年1月1日火曜日
2017年10月26日木曜日
Tensorflowでの警告メッセージの消し方
Tensorflowにおいて、スクリプトを実行すると、「CPU拡張命令セットが使えますよ」的なメッセージがでるので、それを消すためにはスクリプトに下記の命令を書いておく。
import os
os.environ[ 'TF_CPP_MIN_LOG_LEVEL'] = '2'
import os
os.environ[ 'TF_CPP_MIN_LOG_LEVEL'] = '2'
2017年10月17日火曜日
Tensorflow mnistデータの演習
参考ホームページから、下記コードをJupyter notebookにコピペして走らせてみる。
0
31
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
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})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
「得られた結果」
0
31
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
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})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
「得られた結果」
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From D:\Users\shige\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:170: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
0.9169
参考リンク(MNIST for ML beginners)
2017年10月12日木曜日
Anacondaで異なる環境を立ち上げる方法
Anaconda Navigatorで真ん中上部のタブを「Not installed」にして、jupyter関連のプログラムをインストールする。そうすると、Environmentsの好きな環境のところをクリックすると「Open with JupyterNotebook」を選択できるようになる。
AnacondaへのTensorflowインストール(Windows)
Windows版アナコンダには、Tensorflowが入っていないので、JupyterNotebookから
import tensorflow as tf
と打つと、
ModuleNotFoundError: No module named 'tensorflow'と返される。
そこで、Anacondaのプログラムから「Anaconda Navigator」を立ち上げて、真ん中上部のタブ「installed」から「Not installed」を選ぶ。その後は、tensorflowにチェックをつけてapplyボタンを押す。
import tensorflow as tf
と打つと、
ModuleNotFoundError: No module named 'tensorflow'と返される。
そこで、Anacondaのプログラムから「Anaconda Navigator」を立ち上げて、真ん中上部のタブ「installed」から「Not installed」を選ぶ。その後は、tensorflowにチェックをつけてapplyボタンを押す。
2017年4月24日月曜日
wikipediaをダウンロードしてword2vecを試すまでの手順
1.wikipediaのデータを取ってくる。
curl https://dumps.wikimedia.org/jawiki/latest/jawiki-latest-pages-articles.xml.bz2 -o jawiki-latest-pages-articles.xml.bz2
2.テキストデータへの変換を行う。
git clone https://github.com/attardi/wikiextractor
python3 wikiextractor/WikiExtractor.py jawiki-latest-pages-articles.xml.bz2
3.ここまでやるとtextディレクトリが作成されているので、ここの情報を一つにまとめるために以下の処理を行う。
cat text/*/* > jawiki.txt
4.mecabのインストール
5.mecabで分かち書き(mecabエラー)
「mecab -Owakati jawiki.txt -o jawiki_data.txt」
としたところ、
「input-buffer overflow. The line is split. use -b #SIZE option.」
とエラーが出た。
そこで、バッファーを大きくして
「mecab -b 81920 -Owakati jawiki.txt > jawiki_data.txt」
6.word2vecで機械学習
/usr/local/bin/word2vec -train jawiki_data.txt -output jawiki_data.bin -cbow 0 -size 200 -window 5 -negative 0 -hs 1 -sample 1e-3 -binary 1
7.word2vecで計算
/usr/local/bin/word2vec-distance jawiki_data.bin
curl https://dumps.wikimedia.org/jawiki/latest/jawiki-latest-pages-articles.xml.bz2 -o jawiki-latest-pages-articles.xml.bz2
2.テキストデータへの変換を行う。
git clone https://github.com/attardi/wikiextractor
python3 wikiextractor/WikiExtractor.py jawiki-latest-pages-articles.xml.bz2
3.ここまでやるとtextディレクトリが作成されているので、ここの情報を一つにまとめるために以下の処理を行う。
cat text/*/* > jawiki.txt
4.mecabのインストール
5.mecabで分かち書き(mecabエラー)
「mecab -Owakati jawiki.txt -o jawiki_data.txt」
としたところ、
「input-buffer overflow. The line is split. use -b #SIZE option.」
とエラーが出た。
そこで、バッファーを大きくして
「mecab -b 81920 -Owakati jawiki.txt > jawiki_data.txt」
6.word2vecで機械学習
/usr/local/bin/word2vec -train jawiki_data.txt -output jawiki_data.bin -cbow 0 -size 200 -window 5 -negative 0 -hs 1 -sample 1e-3 -binary 1
7.word2vecで計算
/usr/local/bin/word2vec-distance jawiki_data.bin
2017年2月3日金曜日
Tensorflow入門【MNIST編】【情報保存編】
mnist編で学習パラメタを保存する場合はこちら。
tensorflowというフォルダに保存する設定になっています。
##save dataのところが、今回追加した部分。
【mnist.py】
import tensorflow as tf
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()
# create the model
x = tf.placeholder("float", [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y= tf.nn.softmax(tf.matmul(x, W) + b)
##save data
saver = tf.train.Saver()
# define loss and optimizer
y_ = tf.placeholder("float", [None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# train
tf.global_variables_initializer().run()
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x: batch_xs, y_:batch_ys})
# test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuray = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuray.eval({x: mnist.test.images, y_: mnist.test.labels}))
##save data
save_path = saver.save(sess,"/tensorflow/model.ckpt")
print("Model saved in file: %s" % save_path)
tensorflowというフォルダに保存する設定になっています。
##save dataのところが、今回追加した部分。
【mnist.py】
import tensorflow as tf
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()
# create the model
x = tf.placeholder("float", [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y= tf.nn.softmax(tf.matmul(x, W) + b)
##save data
saver = tf.train.Saver()
# define loss and optimizer
y_ = tf.placeholder("float", [None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# train
tf.global_variables_initializer().run()
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x: batch_xs, y_:batch_ys})
# test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuray = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuray.eval({x: mnist.test.images, y_: mnist.test.labels}))
##save data
save_path = saver.save(sess,"/tensorflow/model.ckpt")
print("Model saved in file: %s" % save_path)
2017年1月26日木曜日
Tensorflow入門【MNIST編】
ここを見ながら、「python mnist.py」を実行しようとしたが、
ImportError: No module named 'input_data'
と怒られたので、
ここから、「input_data.py」を作って、tensorflowを動かしているフォルダに置く。
(もしくはPathの通っているところに input_data.pyを置いてもOK)
その後、「python mnist.py」と入力したら動いた。
アウトプットされた結果は「0.9096」だった。
尚、trainの部分はオリジナルは
「tf.initialize_all_variables().run()」であったが、2017/03/02以降使えなくなるそうなので、代わりに「tf.global_variables_initializer().run()」と記述した。
【mnist.py】
import tensorflow as tf
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()
# create the model
x = tf.placeholder("float", [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y= tf.nn.softmax(tf.matmul(x, W) + b)
# define loss and optimizer
y_ = tf.placeholder("float", [None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# train
tf.global_variables_initializer().run()
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x: batch_xs, y_:batch_ys})
# test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuray = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuray.eval({x: mnist.test.images, y_: mnist.test.labels}))
ImportError: No module named 'input_data'
と怒られたので、
ここから、「input_data.py」を作って、tensorflowを動かしているフォルダに置く。
(もしくはPathの通っているところに input_data.pyを置いてもOK)
その後、「python mnist.py」と入力したら動いた。
アウトプットされた結果は「0.9096」だった。
尚、trainの部分はオリジナルは
「tf.initialize_all_variables().run()」であったが、2017/03/02以降使えなくなるそうなので、代わりに「tf.global_variables_initializer().run()」と記述した。
【mnist.py】
import tensorflow as tf
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()
# create the model
x = tf.placeholder("float", [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y= tf.nn.softmax(tf.matmul(x, W) + b)
# define loss and optimizer
y_ = tf.placeholder("float", [None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# train
tf.global_variables_initializer().run()
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x: batch_xs, y_:batch_ys})
# test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuray = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuray.eval({x: mnist.test.images, y_: mnist.test.labels}))
2017年1月17日火曜日
Tensorflow入門【インストール編】
バージョン0.12からwindowsをネイティブサポートするようになりました。(2016.11.29)
1.PCにpython(バージョン3.5.2)をインストールする。
ダウンロード先はここ。
単純に「Python3.5.2」を選ぶと32bit版なので、ちゃんと64bit版(Windows x86-64 executable installer)を選ぶこと。
2.Python 3.5.2をインストールする。
3.Pythonに含まれる「pip」というツールを使って、tensorflowをインストールする。
「Power Shell」を開いて、下記のコマンドを入力する。
python -m pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc1-cp35-cp35m-win_amd64.whl
4.インストールの確認。
下記内容を書いたファイル「hello.py」(pythonの拡張子はpy)
import tensorflow as tf
hello = tf.constant('Hello tensorflow')
sess = tf.Session()
print(sess.run(hello))
を作って、PowerShellから
python hello.py
と入力し、「hello tensorflow」と表示されればOK。
1.PCにpython(バージョン3.5.2)をインストールする。
ダウンロード先はここ。
単純に「Python3.5.2」を選ぶと32bit版なので、ちゃんと64bit版(Windows x86-64 executable installer)を選ぶこと。
2.Python 3.5.2をインストールする。
3.Pythonに含まれる「pip」というツールを使って、tensorflowをインストールする。
「Power Shell」を開いて、下記のコマンドを入力する。
python -m pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc1-cp35-cp35m-win_amd64.whl
4.インストールの確認。
下記内容を書いたファイル「hello.py」(pythonの拡張子はpy)
import tensorflow as tf
hello = tf.constant('Hello tensorflow')
sess = tf.Session()
print(sess.run(hello))
を作って、PowerShellから
python hello.py
と入力し、「hello tensorflow」と表示されればOK。
登録:
投稿 (Atom)
