Last active
April 12, 2017 06:54
-
-
Save Hinaser/6f0413f78aad33cf688729330c777764 to your computer and use it in GitHub Desktop.
TensorBoard Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import tensorflow as tf | |
# セッションを生成 | |
sess = tf.InteractiveSession() | |
# トレーニングするパラメータを定義 | |
A = tf.Variable(tf.zeros(shape=[3, 3]), dtype=tf.float32, name="A") | |
b = tf.Variable(tf.zeros(shape=[3, 1]), dtype=tf.float32, name="b") | |
sess.run(tf.global_variables_initializer()) | |
# Define placeholder for training input | |
# 学習用入力データのプレースホルダの定義 | |
x = tf.placeholder(tf.float32, shape=[3, 1], name="x") | |
# 学習用出力データのプレースホルダの定義 | |
y = tf.placeholder(tf.float32, shape=[3, 1], name="y") | |
# 機械学習のモデルを定義 | |
y_ = tf.matmul(A, x) + b | |
# 誤差を定義 | |
with tf.name_scope('loss'): | |
loss = tf.reduce_mean(tf.square(y_ - y)) | |
# トレーニングアルゴリズムを定義 | |
with tf.name_scope('train'): | |
learning_rate = 1.0e-3 | |
train = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) | |
# TensorBoardで追跡する変数を定義 | |
with tf.name_scope('summary'): | |
tf.summary.scalar('A', tf.reshape(tf.matrix_determinant(A), [])) | |
tf.summary.scalar('b', tf.reshape(tf.norm(b), [])) | |
tf.summary.scalar('loss', loss) | |
merged = tf.summary.merge_all() | |
writer = tf.summary.FileWriter('/tmp/tensorboard-sample', sess.graph) | |
# トレーニングデータを生成 | |
samples_count = 100 | |
x_input = np.ndarray(shape=[samples_count, 3, 1]) | |
y_input = np.ndarray(shape=[samples_count, 3, 1]) | |
for i in range(samples_count): | |
x_input[i][0] = np.random.rand() | |
x_input[i][1] = np.random.rand() | |
x_input[i][2] = np.random.rand() | |
for j in range(3): | |
y_input[i][j] = x_input[i][j] * 2 | |
# バッチ処理開始 | |
summary = None | |
total_epochs = 1000 | |
for _ in range(total_epochs): | |
for i in range(samples_count): | |
__, summary = sess.run([train, merged], feed_dict={x: x_input[i], y: y_input[i]}) | |
# 進捗10%毎に途中経過をコンソール出力、TensorBoardに監視対象変数の値を記録 | |
if _ % (total_epochs/10) == 0: | |
curr_A, curr_b, curr_loss = sess.run([A, b, loss], feed_dict={x: x_input[_ % samples_count], y: y_input[_ % samples_count]}) | |
print('-----------------------------') | |
print('y') | |
print(y_input[_ % samples_count]) | |
print('A') | |
print(curr_A) | |
print('x') | |
print(x_input[_ % samples_count]) | |
print('b') | |
print(curr_b) | |
print('loss') | |
print(curr_loss) | |
writer.add_summary(summary, _) | |
# 最終結果の確認 | |
print('==========================') | |
# x = (1,2,3) を入力すると (2,4,6)に近いベクトルが出力されるはずだが、実際にどうなるか確認。 | |
x_test = np.array([1,2,3]).reshape([3,1]) | |
y_test = np.array([2,4,6]).reshape([3,1]) | |
estimated_y, final_loss = sess.run([y_, loss], feed_dict={x:x_test, y: y_test}) | |
print('Input x') | |
print(x_test) | |
print('Computed y') | |
print(estimated_y) | |
print('loss') | |
print(final_loss) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment