Created
November 10, 2017 23:33
-
-
Save oiehot/838d36b011a45412e14310b24b36f28b to your computer and use it in GitHub Desktop.
Tensorflow LinearRegressor
This file contains hidden or 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 | |
x = tf.feature_column.numeric_column('x', shape=[1]) # 랭크 1 텐서, 1차원 배열 | |
feature_columns = [x] | |
# LinearRegressor < tf.estimator.Estimator | |
estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns) | |
# 데이터 세트 | |
x_train = np.array([1.0, 2.0, 3.0, 4.0]) | |
y_train = np.array([0.0, -1.0, -2.0, -3.0]) | |
x_eval = np.array([2.0, 5.0, 8.0, 1.0]) | |
y_eval = np.array([-1.01, -4.1, -7.0, 0.0]) | |
input_fn = tf.estimator.inputs.numpy_input_fn( | |
x={'x': x_train}, y=y_train, batch_size=4, num_epochs=None, shuffle=True) | |
train_input_fn = tf.estimator.inputs.numpy_input_fn( | |
x={'x': x_train}, y=y_train, batch_size=4, num_epochs=1000, shuffle=False) | |
eval_input_fn = tf.estimator.inputs.numpy_input_fn( | |
x={'x': x_eval}, y=y_eval, batch_size=4, num_epochs=1000, shuffle=False) | |
# 훈련 | |
estimator.train(input_fn=input_fn, steps=1000) | |
# 결과 | |
train_metrics = estimator.evaluate(input_fn=train_input_fn) | |
eval_metrics = estimator.evaluate(input_fn=eval_input_fn) | |
print('train metrics: %r' % train_metrics) | |
print('eval metrics: %r' % eval_metrics) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment