Last active
September 21, 2017 10:05
-
-
Save RottenFruits/3dc9aa52cf10618ee49ba6aa58d3e01a to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#ライブラリ読み込み\n", | |
"import tensorflow as tf\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"from sklearn.datasets import load_boston\n", | |
"from sklearn.preprocessing import StandardScaler" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#ボストンデータ読み込み\n", | |
"boston = load_boston()\n", | |
"df = pd.DataFrame(boston.data, columns = boston.feature_names)\n", | |
"df['target'] = boston.target\n", | |
"\n", | |
"#学習データ準備\n", | |
"f_num = df.shape[1] - 1\n", | |
"train_X = np.array(df.iloc[:, :f_num])\n", | |
"train_Y = np.array(df.iloc[:, f_num: f_num + 1])\n", | |
"n_samples = train_X.shape[0]\n", | |
"\n", | |
"#正規化\n", | |
"ss = StandardScaler()\n", | |
"ss.fit(train_X)\n", | |
"train_X = ss.transform(train_X)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#データ\n", | |
"with tf.name_scope('data'):\n", | |
" X = tf.placeholder(tf.float32, shape = [None, f_num ], name = \"X\")\n", | |
" Y = tf.placeholder(tf.float32, name = \"Y\")\n", | |
"\n", | |
"#パラメータ\n", | |
"with tf.name_scope('parameter'):\n", | |
" W = tf.Variable(tf.zeros([f_num, 1]), name = \"weight\")\n", | |
" b = tf.Variable(tf.zeros([1]), name = \"bias\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#モデル\n", | |
"with tf.name_scope('model'):\n", | |
" pred = tf.add(tf.matmul(X, W), b) #matmulは行列の積" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#損失関数\n", | |
"with tf.name_scope('loss'):\n", | |
" # Mean squared error\n", | |
" loss = tf.reduce_mean(tf.square(pred - Y))\n", | |
" tf.summary.scalar('loss', loss)\n", | |
"\n", | |
"optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.01)\n", | |
"train_step = optimizer.minimize(loss)\n", | |
"\n", | |
"#R2\n", | |
"with tf.name_scope('r2'):\n", | |
" r2 = 1 - (tf.reduce_sum(tf.square(Y - pred)) / tf.reduce_sum(tf.square(Y - tf.reduce_mean(Y))))\n", | |
" tf.summary.scalar('r2', r2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#weight = 0\n", | |
"#学習開始\n", | |
"with tf.Session() as sess:\n", | |
" # ログの設定\n", | |
" summary = tf.summary.merge_all()\n", | |
" writer = tf.summary.FileWriter(\"boston_log\", sess.graph)\n", | |
" \n", | |
" sess.run(tf.global_variables_initializer())#変数初期化\n", | |
" \n", | |
" for i in range(1000):\n", | |
" sess.run(train_step, feed_dict={X: train_X, Y: train_Y})\n", | |
" if i != 0 and i % 200 == 0: # 200ステップごとに精度を出力\n", | |
" train_summary, train_loss, train_r2 = sess.run([summary, loss, r2], feed_dict={X: train_X, Y:train_Y})# コストと精度を出力\n", | |
" writer.add_summary(train_summary, i) #summaryの更新\n", | |
" \n", | |
" print(\"Step:\", '%04d' % (i), \"loss=\", \"{:.9f}\".format(train_loss), \"r2=\", \"{:.9f}\".format(train_r2), \"W=\", sess.run(W), \"b=\", sess.run(b)) \n", | |
" \n", | |
" training_cost, training_r2 = sess.run([loss,r2], feed_dict={X: train_X, Y: train_Y})\n", | |
" print(\"Training cost=\", training_cost, \"Training r2=\", training_r2, \"W=\", sess.run(W), \"b=\", sess.run(b), '\\n')\n", | |
" #weight = sess.run(W)\n", | |
" #pred_y = sess.run(pred, feed_dict={X: train_X})\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%%bash\n", | |
"tensorboard --logdir /Users/ogawashouhei/Google\\ ドライブ/Study/tf/boston_log --port 6017" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment