Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tensorflow LinearRegressor
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