개요

텐서플로우 Estimator에 이어 DNNRegressor를 이용하는 텐서플로우 예제 코드를 통해 딥러닝을 이용한 Regression을 익혀보자.

Jupyter로 돌린 출력결과물도 첨부되어있으니 코드를 참고하자.

패키지 로드

sklearn, numpy, tensorflow 패키지를 로드한다.

import numpy as np
from sklearn import datasets
from sklearn import metrics
from sklearn import model_selection
from sklearn import preprocessing

import tensorflow as tf

데이터셋 로드

sklearn에 포함되어 있는 보스턴 주택가격 예측 데이터셋을 로드해보자.

# Load dataset
boston = datasets.load_boston()
x, y = boston.data, boston.target

train/val 데이터셋 나누기

sklearn을 이용하여 train/val 데이터셋을 나눈다.
아래 코드의 의미는 validation으로 20%의 데이터를 랜덤하게 뽑고 나머지 80%는 트레이닝 데이터셋으로 쓴다는 듯이다.

x_train, x_test, y_train, y_test = model_selection.train_test_split(
      x, y, test_size=0.2, random_state=42)

Feature Normalization

StandardScaler를 이용하여 feature들의 scale을 맞추어 Normalization을 수행한다.

scaler = preprocessing.StandardScaler()
x_train = scaler.fit_transform(x_train)

DNNRegressor 생성

  • 학습에 사용할 feature의 종류를 feature_column으로 명시한다.
  • 이 예제에서는 x_train이 전부 float의 numpy array이니 x하나를 numpy array로 지정해준다.
  • 이 feature_column을 명시하여 DNNRegressor를 생성한다. 히든 레이어는 유닛 10, 10으로 지정하였다.

코드는 아래와 같다.

feature_columns = [
      tf.feature_column.numeric_column('x', shape=np.array(x_train).shape[1:])]
regressor = tf.estimator.DNNRegressor(
      feature_columns=feature_columns, hidden_units=[10, 10])

INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmptd91vipn
INFO:tensorflow:Using config: {'_model_dir': '/tmp/tmptd91vipn', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f598e0ec668>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}

학습

tensorflow estimator로 DNNRegressor를 지정하였고 이 모델이 사용할 input을 train_input_fn으로 지정한다.
tf.estimator.inputs.numpy_input_fn를 이용하면 numpy array를 손쉽게 input_fn형태로 만들 수 있다.

추가로 epoch이나 step을 지정한다.

train_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={'x': x_train}, y=y_train, batch_size=1, num_epochs=None, shuffle=True)
regressor.train(input_fn=train_input_fn, steps=20000)

INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Saving checkpoints for 1 into /tmp/tmptd91vipn/model.ckpt.
INFO:tensorflow:loss = 332.814, step = 0
INFO:tensorflow:global_step/sec: 502.894
INFO:tensorflow:loss = 55.5998, step = 100 (0.202 sec)
INFO:tensorflow:global_step/sec: 802.079
INFO:tensorflow:loss = 8.23713, step = 200 (0.123 sec)
INFO:tensorflow:global_step/sec: 710.081
INFO:tensorflow:loss = 24.208, step = 300 (0.141 sec)
INFO:tensorflow:global_step/sec: 796.775
INFO:tensorflow:loss = 7.13299, step = 400 (0.128 sec)
INFO:tensorflow:global_step/sec: 834.958
INFO:tensorflow:loss = 7.83215, step = 500 (0.118 sec)
INFO:tensorflow:global_step/sec: 864.107
INFO:tensorflow:loss = 0.0191153, step = 600 (0.116 sec)
INFO:tensorflow:global_step/sec: 808.684
INFO:tensorflow:loss = 0.00982953, step = 700 (0.124 sec)
INFO:tensorflow:global_step/sec: 826.407
INFO:tensorflow:loss = 22.1491, step = 800 (0.121 sec)
INFO:tensorflow:global_step/sec: 817.699
INFO:tensorflow:loss = 4.1506, step = 900 (0.122 sec)
INFO:tensorflow:global_step/sec: 839.633
INFO:tensorflow:loss = 1.14635, step = 1000 (0.119 sec)
INFO:tensorflow:global_step/sec: 824.662
INFO:tensorflow:loss = 5.88814, step = 1100 (0.123 sec)
INFO:tensorflow:global_step/sec: 838.475
INFO:tensorflow:loss = 7.44594, step = 1200 (0.118 sec)
INFO:tensorflow:global_step/sec: 817.997
INFO:tensorflow:loss = 4.17761, step = 1300 (0.122 sec)
INFO:tensorflow:global_step/sec: 807.342
INFO:tensorflow:loss = 11.7445, step = 1400 (0.126 sec)
INFO:tensorflow:global_step/sec: 774.619
INFO:tensorflow:loss = 55.357, step = 1500 (0.127 sec)
INFO:tensorflow:global_step/sec: 818.712
INFO:tensorflow:loss = 45.9535, step = 1600 (0.122 sec)
INFO:tensorflow:global_step/sec: 844.851
INFO:tensorflow:loss = 1.93643, step = 1700 (0.118 sec)
INFO:tensorflow:global_step/sec: 818.855
INFO:tensorflow:loss = 1.23202, step = 1800 (0.122 sec)
INFO:tensorflow:global_step/sec: 690.365
INFO:tensorflow:loss = 29.7573, step = 1900 (0.145 sec)
INFO:tensorflow:global_step/sec: 840.065
INFO:tensorflow:loss = 0.36335, step = 2000 (0.119 sec)
INFO:tensorflow:global_step/sec: 776.296
INFO:tensorflow:loss = 47.88, step = 2100 (0.129 sec)
INFO:tensorflow:global_step/sec: 810.108
INFO:tensorflow:loss = 50.0341, step = 2200 (0.123 sec)
INFO:tensorflow:global_step/sec: 733.791
INFO:tensorflow:loss = 3.32858, step = 2300 (0.137 sec)
INFO:tensorflow:global_step/sec: 793.953
INFO:tensorflow:loss = 2.59969, step = 2400 (0.126 sec)
INFO:tensorflow:global_step/sec: 848.584
INFO:tensorflow:loss = 58.1603, step = 2500 (0.118 sec)
INFO:tensorflow:global_step/sec: 708.173
INFO:tensorflow:loss = 0.058202, step = 2600 (0.141 sec)
INFO:tensorflow:global_step/sec: 586.426
INFO:tensorflow:loss = 3.8723, step = 2700 (0.170 sec)
INFO:tensorflow:global_step/sec: 771.21
INFO:tensorflow:loss = 7.8789, step = 2800 (0.130 sec)
INFO:tensorflow:global_step/sec: 802.234
INFO:tensorflow:loss = 53.0881, step = 2900 (0.125 sec)
INFO:tensorflow:global_step/sec: 826.679
INFO:tensorflow:loss = 0.172462, step = 3000 (0.121 sec)
INFO:tensorflow:global_step/sec: 801.149
INFO:tensorflow:loss = 3.57607, step = 3100 (0.125 sec)
INFO:tensorflow:global_step/sec: 721.961
INFO:tensorflow:loss = 4.42883, step = 3200 (0.138 sec)
INFO:tensorflow:global_step/sec: 793.935
INFO:tensorflow:loss = 0.112773, step = 3300 (0.126 sec)
INFO:tensorflow:global_step/sec: 799.86
INFO:tensorflow:loss = 0.339431, step = 3400 (0.125 sec)
INFO:tensorflow:global_step/sec: 763.178
INFO:tensorflow:loss = 4.63551, step = 3500 (0.131 sec)
INFO:tensorflow:global_step/sec: 828.318
INFO:tensorflow:loss = 2.3293, step = 3600 (0.121 sec)
INFO:tensorflow:global_step/sec: 875.4
INFO:tensorflow:loss = 4.41939, step = 3700 (0.114 sec)
INFO:tensorflow:global_step/sec: 795.592
INFO:tensorflow:loss = 2.56564, step = 3800 (0.126 sec)
INFO:tensorflow:global_step/sec: 747.412
INFO:tensorflow:loss = 7.43406, step = 3900 (0.134 sec)
INFO:tensorflow:global_step/sec: 761.282
INFO:tensorflow:loss = 5.68849, step = 4000 (0.132 sec)
INFO:tensorflow:global_step/sec: 828.755
INFO:tensorflow:loss = 1.76523, step = 4100 (0.120 sec)
INFO:tensorflow:global_step/sec: 788.719
INFO:tensorflow:loss = 13.1284, step = 4200 (0.127 sec)
INFO:tensorflow:global_step/sec: 809.696
INFO:tensorflow:loss = 0.261134, step = 4300 (0.124 sec)
INFO:tensorflow:global_step/sec: 774.859
INFO:tensorflow:loss = 28.1798, step = 4400 (0.129 sec)
INFO:tensorflow:global_step/sec: 861.13
INFO:tensorflow:loss = 67.3457, step = 4500 (0.116 sec)
INFO:tensorflow:global_step/sec: 369.546
INFO:tensorflow:loss = 4.64224, step = 4600 (0.270 sec)
INFO:tensorflow:global_step/sec: 684.591
INFO:tensorflow:loss = 68.8507, step = 4700 (0.146 sec)
INFO:tensorflow:global_step/sec: 816.969
INFO:tensorflow:loss = 0.00500155, step = 4800 (0.123 sec)
INFO:tensorflow:global_step/sec: 799.285
INFO:tensorflow:loss = 0.2406, step = 4900 (0.125 sec)
INFO:tensorflow:global_step/sec: 747.523
INFO:tensorflow:loss = 35.5466, step = 5000 (0.134 sec)
INFO:tensorflow:global_step/sec: 859.829
INFO:tensorflow:loss = 34.5862, step = 5100 (0.116 sec)
INFO:tensorflow:global_step/sec: 773.733
INFO:tensorflow:loss = 4.35585, step = 5200 (0.129 sec)
INFO:tensorflow:global_step/sec: 804.715
INFO:tensorflow:loss = 1.33864, step = 5300 (0.124 sec)
INFO:tensorflow:global_step/sec: 757.41
INFO:tensorflow:loss = 15.6384, step = 5400 (0.132 sec)
INFO:tensorflow:global_step/sec: 859.659
INFO:tensorflow:loss = 0.571912, step = 5500 (0.116 sec)
INFO:tensorflow:global_step/sec: 780.161
INFO:tensorflow:loss = 1.37559, step = 5600 (0.128 sec)
INFO:tensorflow:global_step/sec: 841.515
INFO:tensorflow:loss = 1.78944, step = 5700 (0.119 sec)
INFO:tensorflow:global_step/sec: 810.437
INFO:tensorflow:loss = 0.508848, step = 5800 (0.123 sec)
INFO:tensorflow:global_step/sec: 809.419
INFO:tensorflow:loss = 5.45089, step = 5900 (0.124 sec)
INFO:tensorflow:global_step/sec: 800.667
INFO:tensorflow:loss = 2.93178, step = 6000 (0.125 sec)
INFO:tensorflow:global_step/sec: 770.226
INFO:tensorflow:loss = 0.141419, step = 6100 (0.131 sec)
INFO:tensorflow:global_step/sec: 797.175
INFO:tensorflow:loss = 8.12921, step = 6200 (0.124 sec)
INFO:tensorflow:global_step/sec: 780.596
INFO:tensorflow:loss = 10.4931, step = 6300 (0.128 sec)
INFO:tensorflow:global_step/sec: 725.238
INFO:tensorflow:loss = 12.3125, step = 6400 (0.138 sec)
INFO:tensorflow:global_step/sec: 719.696
INFO:tensorflow:loss = 1.47908, step = 6500 (0.139 sec)
INFO:tensorflow:global_step/sec: 778.626
INFO:tensorflow:loss = 22.8053, step = 6600 (0.128 sec)
INFO:tensorflow:global_step/sec: 827.511
INFO:tensorflow:loss = 16.2612, step = 6700 (0.121 sec)
INFO:tensorflow:global_step/sec: 867.737
INFO:tensorflow:loss = 4.40943, step = 6800 (0.115 sec)
INFO:tensorflow:global_step/sec: 686.213
INFO:tensorflow:loss = 27.4671, step = 6900 (0.146 sec)
INFO:tensorflow:global_step/sec: 695.349
INFO:tensorflow:loss = 0.00441625, step = 7000 (0.144 sec)
INFO:tensorflow:global_step/sec: 679.906
INFO:tensorflow:loss = 0.015301, step = 7100 (0.147 sec)
INFO:tensorflow:global_step/sec: 695.924
INFO:tensorflow:loss = 10.3052, step = 7200 (0.143 sec)
INFO:tensorflow:global_step/sec: 735.771
INFO:tensorflow:loss = 1.38084, step = 7300 (0.136 sec)
INFO:tensorflow:global_step/sec: 805.544
INFO:tensorflow:loss = 20.9993, step = 7400 (0.124 sec)
INFO:tensorflow:global_step/sec: 830.409
INFO:tensorflow:loss = 6.02663, step = 7500 (0.120 sec)
INFO:tensorflow:global_step/sec: 761.08
INFO:tensorflow:loss = 0.0713421, step = 7600 (0.132 sec)
INFO:tensorflow:global_step/sec: 837.39
INFO:tensorflow:loss = 15.1367, step = 7700 (0.119 sec)
INFO:tensorflow:global_step/sec: 790.854
INFO:tensorflow:loss = 1.11562, step = 7800 (0.126 sec)
INFO:tensorflow:global_step/sec: 778.274
INFO:tensorflow:loss = 0.0815536, step = 7900 (0.129 sec)
INFO:tensorflow:global_step/sec: 648.352
INFO:tensorflow:loss = 4.2392, step = 8000 (0.154 sec)
INFO:tensorflow:global_step/sec: 737.228
INFO:tensorflow:loss = 1.72332, step = 8100 (0.135 sec)
INFO:tensorflow:global_step/sec: 780.044
INFO:tensorflow:loss = 0.55593, step = 8200 (0.130 sec)
INFO:tensorflow:global_step/sec: 772.805
INFO:tensorflow:loss = 46.4397, step = 8300 (0.127 sec)
INFO:tensorflow:global_step/sec: 795.904
INFO:tensorflow:loss = 0.783323, step = 8400 (0.126 sec)
INFO:tensorflow:global_step/sec: 796.843
INFO:tensorflow:loss = 4.01275, step = 8500 (0.127 sec)
INFO:tensorflow:global_step/sec: 790.637
INFO:tensorflow:loss = 25.9779, step = 8600 (0.124 sec)
INFO:tensorflow:global_step/sec: 749.952
INFO:tensorflow:loss = 2.50688, step = 8700 (0.133 sec)
INFO:tensorflow:global_step/sec: 762.916
INFO:tensorflow:loss = 3.2233, step = 8800 (0.132 sec)
INFO:tensorflow:global_step/sec: 790.565
INFO:tensorflow:loss = 7.64458, step = 8900 (0.126 sec)
INFO:tensorflow:global_step/sec: 696.178
INFO:tensorflow:loss = 0.378032, step = 9000 (0.144 sec)
INFO:tensorflow:global_step/sec: 747.12
INFO:tensorflow:loss = 0.204102, step = 9100 (0.134 sec)
INFO:tensorflow:global_step/sec: 818.596
INFO:tensorflow:loss = 0.0725628, step = 9200 (0.122 sec)
INFO:tensorflow:global_step/sec: 797.289
INFO:tensorflow:loss = 0.0957936, step = 9300 (0.126 sec)
INFO:tensorflow:global_step/sec: 857.559
INFO:tensorflow:loss = 31.0386, step = 9400 (0.117 sec)
INFO:tensorflow:global_step/sec: 652.509
INFO:tensorflow:loss = 7.3679, step = 9500 (0.153 sec)
INFO:tensorflow:global_step/sec: 791.53
INFO:tensorflow:loss = 11.2406, step = 9600 (0.126 sec)
INFO:tensorflow:global_step/sec: 768.259
INFO:tensorflow:loss = 6.89346, step = 9700 (0.130 sec)
INFO:tensorflow:global_step/sec: 747.888
INFO:tensorflow:loss = 6.06454, step = 9800 (0.134 sec)
INFO:tensorflow:global_step/sec: 798.032
INFO:tensorflow:loss = 0.702841, step = 9900 (0.125 sec)
INFO:tensorflow:global_step/sec: 836.093
INFO:tensorflow:loss = 0.638201, step = 10000 (0.120 sec)
INFO:tensorflow:global_step/sec: 757.668
INFO:tensorflow:loss = 2.3522, step = 10100 (0.132 sec)
INFO:tensorflow:global_step/sec: 818.278
INFO:tensorflow:loss = 0.484747, step = 10200 (0.122 sec)
INFO:tensorflow:global_step/sec: 708.045
INFO:tensorflow:loss = 24.7199, step = 10300 (0.141 sec)
INFO:tensorflow:global_step/sec: 803.916
INFO:tensorflow:loss = 1.93817, step = 10400 (0.125 sec)
INFO:tensorflow:global_step/sec: 801.472
INFO:tensorflow:loss = 5.75445, step = 10500 (0.125 sec)
INFO:tensorflow:global_step/sec: 828.414
INFO:tensorflow:loss = 0.0485668, step = 10600 (0.121 sec)
INFO:tensorflow:global_step/sec: 840.737
INFO:tensorflow:loss = 0.450988, step = 10700 (0.119 sec)
INFO:tensorflow:global_step/sec: 774.496
INFO:tensorflow:loss = 0.438409, step = 10800 (0.129 sec)
INFO:tensorflow:global_step/sec: 823.333
INFO:tensorflow:loss = 3.46141, step = 10900 (0.121 sec)
INFO:tensorflow:global_step/sec: 711.671
INFO:tensorflow:loss = 0.858961, step = 11000 (0.141 sec)
INFO:tensorflow:global_step/sec: 621.824
INFO:tensorflow:loss = 0.0948232, step = 11100 (0.161 sec)
INFO:tensorflow:global_step/sec: 791.232
INFO:tensorflow:loss = 0.0213672, step = 11200 (0.127 sec)
INFO:tensorflow:global_step/sec: 821.152
INFO:tensorflow:loss = 57.9661, step = 11300 (0.122 sec)
INFO:tensorflow:global_step/sec: 829.224
INFO:tensorflow:loss = 17.0319, step = 11400 (0.121 sec)
INFO:tensorflow:global_step/sec: 763.603
INFO:tensorflow:loss = 2.78993, step = 11500 (0.131 sec)
INFO:tensorflow:global_step/sec: 794.662
INFO:tensorflow:loss = 0.682431, step = 11600 (0.126 sec)
INFO:tensorflow:global_step/sec: 752.431
INFO:tensorflow:loss = 2.03266, step = 11700 (0.133 sec)
INFO:tensorflow:global_step/sec: 828.109
INFO:tensorflow:loss = 1.07927, step = 11800 (0.121 sec)
INFO:tensorflow:global_step/sec: 746.28
INFO:tensorflow:loss = 0.170363, step = 11900 (0.134 sec)
INFO:tensorflow:global_step/sec: 788.35
INFO:tensorflow:loss = 9.90286, step = 12000 (0.127 sec)
INFO:tensorflow:global_step/sec: 837.325
INFO:tensorflow:loss = 5.32528, step = 12100 (0.120 sec)
INFO:tensorflow:global_step/sec: 821.668
INFO:tensorflow:loss = 26.7867, step = 12200 (0.121 sec)
INFO:tensorflow:global_step/sec: 825.62
INFO:tensorflow:loss = 0.0219138, step = 12300 (0.121 sec)
INFO:tensorflow:global_step/sec: 788.224
INFO:tensorflow:loss = 12.5151, step = 12400 (0.127 sec)
INFO:tensorflow:global_step/sec: 772.506
INFO:tensorflow:loss = 4.69863, step = 12500 (0.129 sec)
INFO:tensorflow:global_step/sec: 709.826
INFO:tensorflow:loss = 0.409087, step = 12600 (0.141 sec)
INFO:tensorflow:global_step/sec: 815.391
INFO:tensorflow:loss = 6.77616, step = 12700 (0.122 sec)
INFO:tensorflow:global_step/sec: 845.29
INFO:tensorflow:loss = 30.3747, step = 12800 (0.118 sec)
INFO:tensorflow:global_step/sec: 788.995
INFO:tensorflow:loss = 30.032, step = 12900 (0.127 sec)
INFO:tensorflow:global_step/sec: 787.107
INFO:tensorflow:loss = 18.0729, step = 13000 (0.127 sec)
INFO:tensorflow:global_step/sec: 794.194
INFO:tensorflow:loss = 9.24199, step = 13100 (0.126 sec)
INFO:tensorflow:global_step/sec: 849.438
INFO:tensorflow:loss = 5.45386, step = 13200 (0.118 sec)
INFO:tensorflow:global_step/sec: 820.588
INFO:tensorflow:loss = 0.227121, step = 13300 (0.122 sec)
INFO:tensorflow:global_step/sec: 756.858
INFO:tensorflow:loss = 0.646706, step = 13400 (0.132 sec)
INFO:tensorflow:global_step/sec: 783.925
INFO:tensorflow:loss = 0.00928143, step = 13500 (0.128 sec)
INFO:tensorflow:global_step/sec: 779.241
INFO:tensorflow:loss = 1.74816, step = 13600 (0.128 sec)
INFO:tensorflow:global_step/sec: 751.246
INFO:tensorflow:loss = 6.27708, step = 13700 (0.133 sec)
INFO:tensorflow:global_step/sec: 821.725
INFO:tensorflow:loss = 0.978401, step = 13800 (0.122 sec)
INFO:tensorflow:global_step/sec: 761.752
INFO:tensorflow:loss = 0.138794, step = 13900 (0.131 sec)
INFO:tensorflow:global_step/sec: 897.012
INFO:tensorflow:loss = 4.48457, step = 14000 (0.111 sec)
INFO:tensorflow:global_step/sec: 897.998
INFO:tensorflow:loss = 11.5302, step = 14100 (0.111 sec)
INFO:tensorflow:global_step/sec: 803.967
INFO:tensorflow:loss = 0.810817, step = 14200 (0.125 sec)
INFO:tensorflow:global_step/sec: 829.607
INFO:tensorflow:loss = 3.63237, step = 14300 (0.120 sec)
INFO:tensorflow:global_step/sec: 843.624
INFO:tensorflow:loss = 0.349592, step = 14400 (0.118 sec)
INFO:tensorflow:global_step/sec: 844.82
INFO:tensorflow:loss = 5.02592, step = 14500 (0.118 sec)
INFO:tensorflow:global_step/sec: 770.342
INFO:tensorflow:loss = 0.0611814, step = 14600 (0.130 sec)
INFO:tensorflow:global_step/sec: 637.313
INFO:tensorflow:loss = 10.1526, step = 14700 (0.157 sec)
INFO:tensorflow:global_step/sec: 791.421
INFO:tensorflow:loss = 41.8588, step = 14800 (0.127 sec)
INFO:tensorflow:global_step/sec: 740.885
INFO:tensorflow:loss = 3.76941, step = 14900 (0.135 sec)
INFO:tensorflow:global_step/sec: 730.786
INFO:tensorflow:loss = 0.0171801, step = 15000 (0.136 sec)
INFO:tensorflow:global_step/sec: 738.073
INFO:tensorflow:loss = 9.65106, step = 15100 (0.136 sec)
INFO:tensorflow:global_step/sec: 727.46
INFO:tensorflow:loss = 3.80764, step = 15200 (0.138 sec)
INFO:tensorflow:global_step/sec: 734.808
INFO:tensorflow:loss = 0.0332825, step = 15300 (0.136 sec)
INFO:tensorflow:global_step/sec: 842.289
INFO:tensorflow:loss = 0.154623, step = 15400 (0.119 sec)
INFO:tensorflow:global_step/sec: 861.938
INFO:tensorflow:loss = 0.00658053, step = 15500 (0.116 sec)
INFO:tensorflow:global_step/sec: 743.825
INFO:tensorflow:loss = 6.25578, step = 15600 (0.135 sec)
INFO:tensorflow:global_step/sec: 749.212
INFO:tensorflow:loss = 0.0491047, step = 15700 (0.133 sec)
INFO:tensorflow:global_step/sec: 783.493
INFO:tensorflow:loss = 1.1981, step = 15800 (0.128 sec)
INFO:tensorflow:global_step/sec: 842.402
INFO:tensorflow:loss = 0.0415625, step = 15900 (0.119 sec)
INFO:tensorflow:global_step/sec: 829.834
INFO:tensorflow:loss = 11.0979, step = 16000 (0.121 sec)
INFO:tensorflow:global_step/sec: 824.202
INFO:tensorflow:loss = 1.17323, step = 16100 (0.121 sec)
INFO:tensorflow:global_step/sec: 819.019
INFO:tensorflow:loss = 3.62272, step = 16200 (0.122 sec)
INFO:tensorflow:global_step/sec: 824.806
INFO:tensorflow:loss = 0.146472, step = 16300 (0.121 sec)
INFO:tensorflow:global_step/sec: 773.905
INFO:tensorflow:loss = 5.42578, step = 16400 (0.129 sec)
INFO:tensorflow:global_step/sec: 772.778
INFO:tensorflow:loss = 0.0553051, step = 16500 (0.129 sec)
INFO:tensorflow:global_step/sec: 744.159
INFO:tensorflow:loss = 4.62679, step = 16600 (0.134 sec)
INFO:tensorflow:global_step/sec: 774.365
INFO:tensorflow:loss = 0.0862167, step = 16700 (0.129 sec)
INFO:tensorflow:global_step/sec: 851.973
INFO:tensorflow:loss = 0.988773, step = 16800 (0.117 sec)
INFO:tensorflow:global_step/sec: 771.017
INFO:tensorflow:loss = 27.7746, step = 16900 (0.130 sec)
INFO:tensorflow:global_step/sec: 779.499
INFO:tensorflow:loss = 0.0469364, step = 17000 (0.128 sec)
INFO:tensorflow:global_step/sec: 840.212
INFO:tensorflow:loss = 0.109288, step = 17100 (0.119 sec)
INFO:tensorflow:global_step/sec: 789.952
INFO:tensorflow:loss = 29.0845, step = 17200 (0.127 sec)
INFO:tensorflow:global_step/sec: 702.119
INFO:tensorflow:loss = 8.42992, step = 17300 (0.143 sec)
INFO:tensorflow:global_step/sec: 831.708
INFO:tensorflow:loss = 2.78621, step = 17400 (0.120 sec)
INFO:tensorflow:global_step/sec: 881.651
INFO:tensorflow:loss = 4.03428, step = 17500 (0.114 sec)
INFO:tensorflow:global_step/sec: 792.257
INFO:tensorflow:loss = 3.42032, step = 17600 (0.126 sec)
INFO:tensorflow:global_step/sec: 754.778
INFO:tensorflow:loss = 0.00777177, step = 17700 (0.133 sec)
INFO:tensorflow:global_step/sec: 782.158
INFO:tensorflow:loss = 6.11569, step = 17800 (0.128 sec)
INFO:tensorflow:global_step/sec: 766.615
INFO:tensorflow:loss = 26.1524, step = 17900 (0.130 sec)
INFO:tensorflow:global_step/sec: 837.499
INFO:tensorflow:loss = 2.18022, step = 18000 (0.119 sec)
INFO:tensorflow:global_step/sec: 776.433
INFO:tensorflow:loss = 148.146, step = 18100 (0.129 sec)
INFO:tensorflow:global_step/sec: 796.568
INFO:tensorflow:loss = 7.4129, step = 18200 (0.125 sec)
INFO:tensorflow:global_step/sec: 795.947
INFO:tensorflow:loss = 3.18213, step = 18300 (0.126 sec)
INFO:tensorflow:global_step/sec: 760.142
INFO:tensorflow:loss = 0.873878, step = 18400 (0.132 sec)
INFO:tensorflow:global_step/sec: 731.154
INFO:tensorflow:loss = 18.1953, step = 18500 (0.137 sec)
INFO:tensorflow:global_step/sec: 811.127
INFO:tensorflow:loss = 36.6224, step = 18600 (0.123 sec)
INFO:tensorflow:global_step/sec: 821.314
INFO:tensorflow:loss = 5.39793, step = 18700 (0.122 sec)
INFO:tensorflow:global_step/sec: 874.597
INFO:tensorflow:loss = 0.0223742, step = 18800 (0.114 sec)
INFO:tensorflow:global_step/sec: 800.299
INFO:tensorflow:loss = 2.69393, step = 18900 (0.125 sec)
INFO:tensorflow:global_step/sec: 809.838
INFO:tensorflow:loss = 41.8301, step = 19000 (0.124 sec)
INFO:tensorflow:global_step/sec: 780.617
INFO:tensorflow:loss = 0.812759, step = 19100 (0.128 sec)
INFO:tensorflow:global_step/sec: 792.094
INFO:tensorflow:loss = 1.17215, step = 19200 (0.126 sec)
INFO:tensorflow:global_step/sec: 768.074
INFO:tensorflow:loss = 0.195214, step = 19300 (0.130 sec)
INFO:tensorflow:global_step/sec: 809.919
INFO:tensorflow:loss = 0.675196, step = 19400 (0.123 sec)
INFO:tensorflow:global_step/sec: 833.79
INFO:tensorflow:loss = 5.21501, step = 19500 (0.120 sec)
INFO:tensorflow:global_step/sec: 814.548
INFO:tensorflow:loss = 6.95674, step = 19600 (0.123 sec)
INFO:tensorflow:global_step/sec: 731.125
INFO:tensorflow:loss = 0.23439, step = 19700 (0.137 sec)
INFO:tensorflow:global_step/sec: 774.692
INFO:tensorflow:loss = 0.271831, step = 19800 (0.129 sec)
INFO:tensorflow:global_step/sec: 823.975
INFO:tensorflow:loss = 2.1597, step = 19900 (0.121 sec)
INFO:tensorflow:Saving checkpoints for 20000 into /tmp/tmptd91vipn/model.ckpt.
INFO:tensorflow:Loss for final step: 8.36867.

<tensorflow.python.estimator.canned.dnn.DNNRegressor at 0x7f5a102a8f60>

예측

학습이 완료되면 마찬가지로 validation 데이터셋도 동일한 feature engineering 후에 예측 값을 구한다.

x_transformed = scaler.transform(x_test)
test_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={'x': x_transformed}, y=y_test, num_epochs=1, shuffle=False)
predictions = regressor.predict(input_fn=test_input_fn)
y_predicted = np.array(list(p['predictions'] for p in predictions))
y_predicted = y_predicted.reshape(np.array(y_test).shape)

INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmptd91vipn/model.ckpt-20000
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.

모델 평가

MSE로 예측값과 정답값의 오차 비용을 구한다.

score_sklearn = metrics.mean_squared_error(y_predicted, y_test)
print('MSE (sklearn): {0:f}'.format(score_sklearn))

MSE (sklearn): 11.951740

또한 average loss를 구해보자.

scores = regressor.evaluate(input_fn=test_input_fn)
print('MSE (tensorflow): {0:f}'.format(scores['average_loss']))

INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2018-05-19-11:03:15
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmptd91vipn/model.ckpt-20000
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Finished evaluation at 2018-05-19-11:03:15
INFO:tensorflow:Saving dict for global step 20000: average_loss = 11.9517, global_step = 20000, loss = 1219.08
MSE (tensorflow): 11.951739