LoginSignup
0
0

More than 5 years have passed since last update.

多層ニューラルネットワークの作成②

Last updated at Posted at 2018-06-27

前回できなかったからリベンジ

必要なものをインポート

6-6多層ニューラルネットワークの作成.py

import tensorflow as tf
import matplotlib.pyplot as plt
import csv
import os
import numpy as np
import requests
import time

データをダウンロードしデータファイルを作成

6-6多層ニューラルネットワークの作成.py
birth_weight_file = 'birth_weight.csv'
if not os.path.exists(birth_weight_file):
    birthdata_url = 'https://github.com/nfmcclure/tensorflow_cookbook/raw/master/01_Introduction/07_Working_with_Data_Sources/birthweight_data/birthweight.dat'
    birth_file = requests.get(birthdata_url)
    birth_data = birth_file.text.split('\r\n')
    birth_header = birth_data[0].split('\t')
    birth_data = [[float(x) for x in y.split('\t') if len(x) >= 1] 
                  for y in birth_data[1:] if len(y) >= 1]
    with open(birth_weight_file, "w") as f:
        writer = csv.writer(f)
        writer.writerows([birth_header])
        writer.writerows(birth_data)
        f.close()

birth weight dataをメモリに読み込む

6-6多層ニューラルネットワークの作成.py
birth_data = []
with open(birth_weight_file, newline='') as csvfile:
    csv_reader = csv.reader(csvfile)
    birth_header = next(csv_reader)
    for row in csv_reader:
        birth_data.append(row)

birth_data = [[float(x) for x in row] for row in birth_data]

目的変数を抽出

6-6多層ニューラルネットワークの作成.py
y_vals = np.array([x[8] for x in birth_data])

7つの特徴量を入力

6-6多層ニューラルネットワークの作成.py
cols_of_interest = ['AGE', 'LWT', 'RACE', 'SMOKE', 'PTL', 'HT', 'UI']

予測変数を抽出

6-6多層ニューラルネットワークの作成.py
x_vals = np.array([[x[ix] for ix, feature in enumerate(birth_header) 
    if feature in cols_of_interest] for x in birth_data])

グラフセッションを作成

6-6多層ニューラルネットワークの作成.py
sess = tf.Session()
seed = 3
tf.set_random_seed(seed)
np.random.seed(seed)
batch_size = 100

データセットをtrainとtestで80:20で分割

6-6多層ニューラルネットワークの作成.py
train_indices = np.random.choice(len(x_vals), round(len(x_vals)*0.8), replace=False)
test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
x_vals_train = x_vals[train_indices]
x_vals_test = x_vals[test_indices]
y_vals_train = y_vals[train_indices]
y_vals_test = y_vals[test_indices]

Normalize by column (min-max norm to be between 0 and 1)/min-maxスケーリングを使って入力特徴量を0~1の値で正規化

6-6多層ニューラルネットワークの作成.py
def normalize_cols(m):
    col_max = m.max(axis=0)
    col_min = m.min(axis=0)
    return (m - col_min) / (col_max - col_min)
x_vals_train = np.nan_to_num(normalize_cols(x_vals_train))
x_vals_test = np.nan_to_num(normalize_cols(x_vals_test))

重みとバイアスを初期化する変数の作成,stddev=標準偏差

6-6多層ニューラルネットワークの作成.py
def init_weight(shape, st_dev):
    weight = tf.Variable(tf.random_normal(shape, stddev=st_dev))
    return(weight)

def init_bias(shape, st_dev):
    bias = tf.Variable(tf.random_normal(shape, stddev=st_dev))
    return(bias)

プレースホルダーの作成、shape=7:7つの入力特徴量 shape=1:出生体重

6-6多層ニューラルネットワークの作成.py
x_data = tf.placeholder(shape=[None, 7], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

# Create a fully connected layer:relu関数はxが負のとき0,正の時xの値を取る
def fully_connected(input_layer, weights, biases):
    layer = tf.add(tf.matmul(input_layer, weights), biases)
    return tf.nn.relu(layer)

1つ目の層(25個のノードを持つ隠れ層)を作成

6-6多層ニューラルネットワークの作成.py
weight_1 = init_weight(shape=[7, 25], st_dev=10.0)
bias_1 = init_bias(shape=[25], st_dev=10.0)
layer_1 = fully_connected(x_data, weight_1, bias_1)

2つ目の層(10個のノードを持つ隠れ層)を作成

6-6多層ニューラルネットワークの作成.py
weight_2 = init_weight(shape=[25, 10], st_dev=10.0)
bias_2 = init_bias(shape=[10], st_dev=10.0)
layer_2 = fully_connected(layer_1, weight_2, bias_2)

3つ目の層(3個のノードを持つ隠れ層)を作成

6-6多層ニューラルネットワークの作成.py
weight_3 = init_weight(shape=[10, 3], st_dev=10.0)
bias_3 = init_bias(shape=[3], st_dev=10.0)
layer_3 = fully_connected(layer_2, weight_3, bias_3)

出力層(1個の出力)を作成

6-6多層ニューラルネットワークの作成.py
weight_4 = init_weight(shape=[3, 1], st_dev=10.0)
bias_4 = init_bias(shape=[1], st_dev=10.0)
final_output = fully_connected(layer_3, weight_4, bias_4)

損失関数を作成

6-6多層ニューラルネットワークの作成.py
loss = tf.reduce_mean(tf.abs(y_target - final_output))

tensorboardで見る為に加えたもの

6-6多層ニューラルネットワークの作成.py
with tf.name_scope('summary'):
    tf.summary.scalar('loss', loss)
    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter('./logs', sess.graph)

最適化を作成

6-6多層ニューラルネットワークの作成.py
my_opt = tf.train.AdamOptimizer(0.05)
train_step = my_opt.minimize(loss)

変数を初期化

6-6多層ニューラルネットワークの作成.py
init = tf.global_variables_initializer()
sess.run(init)

計算開始時間

6-6多層ニューラルネットワークの作成.py
t1 = time.time()

損失ベクトルを初期化

6-6多層ニューラルネットワークの作成.py
loss_vec = []
test_loss = []
for i in range(200):
    # バッチを選択するためのインデックスをランダムに選択    
    rand_index = np.random.choice(len(x_vals_train), size=batch_size)
    # ランダムな値でバッチを取得
    rand_x = x_vals_train[rand_index]
    rand_y = np.transpose([y_vals_train[rand_index]])
    #トレーニングステップを実行
    sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})

    #トレーニングセットの損失値を保存
    temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
    loss_vec.append(temp_loss)

    # テストセットの損失値を保存
    test_temp_loss = sess.run(loss, feed_dict={x_data: x_vals_test, y_target: np.transpose([y_vals_test])})
    test_loss.append(test_temp_loss)
    if (i+1) % 25 == 0:
        print('Generation: ' + str(i+1) + '. Loss = ' + str(temp_loss))

計算終了時間

6-6多層ニューラルネットワークの作成.py
t2 = time.time()

損失値をplot

6-6多層ニューラルネットワークの作成.py
plt.plot(loss_vec, 'k-', label='Train Loss')
plt.plot(test_loss, 'r--', label='Test Loss')
plt.title('Loss (MSE) per Generation')
plt.legend(loc='upper right')
plt.xlabel('Generation')
plt.ylabel('Loss')
plt.show()

正解率のモデル

6-6多層ニューラルネットワークの作成.py
actuals = np.array([x[0] for x in birth_data])
test_actuals = actuals[test_indices]
train_actuals = actuals[train_indices]
test_preds = [x[0] for x in sess.run(final_output, feed_dict={x_data: x_vals_test})]
train_preds = [x[0] for x in sess.run(final_output, feed_dict={x_data: x_vals_train})]
test_preds = np.array([1.0 if x < 2500.0 else 0.0 for x in test_preds])
train_preds = np.array([1.0 if x < 2500.0 else 0.0 for x in train_preds])

正解率を出力

6-6多層ニューラルネットワークの作成.py
test_acc = np.mean([x == y for x, y in zip(test_preds, test_actuals)])
train_acc = np.mean([x == y for x, y in zip(train_preds, train_actuals)])
print('On predicting the category of low birthweight from regression output (<2500g):')
print('Test Accuracy: {}'.format(test_acc))
print('Train Accuracy: {}'.format(train_acc))

計算処理時間

6-6多層ニューラルネットワークの作成.py
elapsed_time = t2-t1
print(f"経過時間:{elapsed_time}")

出力結果

6-6多層ニューラルネットワークの作成.py
Generation: 25. Loss = 5622.841
Generation: 50. Loss = 2633.468
Generation: 75. Loss = 2492.912
Generation: 100. Loss = 2275.6975
Generation: 125. Loss = 2089.1477
Generation: 150. Loss = 2178.2175
Generation: 175. Loss = 2027.2734
Generation: 200. Loss = 2271.147
On predicting the category of low birthweight from regression output (<2500g):
Test Accuracy: 0.39473684210526316
Train Accuracy: 0.5364238410596026
経過時間:0.39993739128112793

Figure_1-1.png

やっとできた!何でできなかったとか精度の工夫した結果とはまた次回のせます
ねむい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0