线性回归,Tensorflow,非线性方程,tf.contrib.learn
作为练习,我尝试使用tf.contrib.learn.LinearRegressor对方程y=3*x1^2+4*x2^2进行建模。代码运行了,但是我对结果的准确性有点失望。对于y=3*x1+4*x2这样的线性方程,结果是好的。我认为tf.contrib.learn能够很好地处理平方项。是否有可以用来获得更好结果的设置,例如不同的优化器线性回归,Tensorflow,非线性方程,tf.contrib.learn,tensorflow,linear-regression,Tensorflow,Linear Regression,作为练习,我尝试使用tf.contrib.learn.LinearRegressor对方程y=3*x1^2+4*x2^2进行建模。代码运行了,但是我对结果的准确性有点失望。对于y=3*x1+4*x2这样的线性方程,结果是好的。我认为tf.contrib.learn能够很好地处理平方项。是否有可以用来获得更好结果的设置,例如不同的优化器 from __future__ import absolute_import from __future__ import division
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import tempfile
from six.moves import urllib
import pandas as pd
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import math
FLAGS = None
myImportedDatax1_np = np.empty((100, 1))
myImportedDatax2_np = np.empty((100, 1))
myImportedDatay_np = np.empty((100, 1))
def trueOutput(x1, x2):
return [3 * math.pow(x1, 2) + 4 * math.pow(x2, 2)]
count = 0
# Create data, using true equation, in range x1= 0 to 9, and x2=0 to 9
for a in range(0, 10):
for b in range(0, 10):
myImportedDatax1_np[count] = a
myImportedDatax2_np[count] = b
myImportedDatay_np[count] = trueOutput(myImportedDatax1_np[count], myImportedDatax2_np[count])
print(myImportedDatay_np[count])
count = count + 1
combined_Imported_Data_x = np.append(myImportedDatax1_np, myImportedDatax2_np, axis=1)
def build_estimator(model_dir, model_type):
x1 = tf.contrib.layers.real_valued_column("x1")
x2 = tf.contrib.layers.real_valued_column("x2")
wide_columns = [x1, x2]
m = tf.contrib.learn.LinearRegressor(model_dir=model_dir, feature_columns=wide_columns)
return m
def input_fn(input_batch, output_batch):
inputs = {"x1": tf.constant(input_batch[:,0]), "x2": tf.constant(input_batch[:,1])}
output = tf.constant(output_batch)
return inputs, output
def input_fn_predict(x1, x2):
inputs = {"x1": tf.constant([[x1]]), "x2": tf.constant([[x2]])}
return inputs
def train_and_eval(model_dir, model_type, train_steps, train_data, test_data):
model_dir = tempfile.mkdtemp() if not model_dir else model_dir
print("model directory = %s" % model_dir)
m = build_estimator(model_dir, model_type)
m.fit(input_fn=lambda: input_fn(combined_Imported_Data_x, myImportedDatay_np), steps=train_steps)
my_x1 = 2
my_x2 = 6
prediction = list(m.predict(input_fn=lambda: input_fn_predict(my_x1, my_x2)))
print("Prediction value is: ")
print(prediction)
print("Actual value is: ")
true_y = trueOutput(my_x1, my_x2)
print(true_y)
def main(_):
train_and_eval(FLAGS.model_dir, FLAGS.model_type, FLAGS.train_steps,
FLAGS.train_data, FLAGS.test_data)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.register("type", "bool", lambda v: v.lower() == "true")
parser.add_argument(
"--model_dir",
type=str,
default="",
help="Base directory for output models."
)
parser.add_argument(
"--model_type",
type=str,
default="wide_n_deep",
help="Valid model types: {'wide', 'deep', 'wide_n_deep'}."
)
parser.add_argument(
"--train_steps",
type=int,
default=10000,
help="Number of training steps."
)
parser.add_argument(
"--train_data",
type=str,
default="",
help="Path to the training data."
)
parser.add_argument(
"--test_data",
type=str,
default="",
help="Path to the test data."
)
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
用于建立线性回归模型。方程y=3*x1^2+4*x2^2
在x1
和x2
中不是线性回归,因此tf.contrib.learn.LinearRegressor
将无法对其建模。您可以创建新功能x1^2
和x2^2
,然后使用tf.contrib.learn.LinearRegressor
来训练模型。此外,请参见此处关于使用神经网络预测平方函数的讨论:
用于建立线性回归模型。方程y=3*x1^2+4*x2^2
在x1
和x2
中不是线性回归,因此tf.contrib.learn.LinearRegressor
将无法对其建模。您可以创建新功能x1^2
和x2^2
,然后使用tf.contrib.learn.LinearRegressor
来训练模型。此外,请参见此处关于使用神经网络预测平方函数的讨论:
将我的代码更改为:
def input\u fn(input\u batch,output\u batch):inputs={“x1”:tf.常量(input\u batch[:,0]**2),“x2”:tf.常量(input\u batch[:,1]**2)}
,以及def input\u fn\u predict(x1,x2):inputs={“x1”:tf.常量([[x1****2]]),“x2”:tf.常量([[x2**]}
,效果非常好。我认为tf.contrib会自动为每个功能输入创建平方项,甚至一些立方项,以便能够解决更多的实际应用程序。将我的代码更改为:def input\u fn(input\u batch,output\u batch):inputs={“x1”:tf.constant(input\u batch[:,0]**2),“x2”:tf.constant(input\u batch[:,1]**2)
,,还有def input\u fn\u predict(x1,x2):inputs={“x1”:tf.常量([[x1**2]]),“x2”:tf.常量([[x2**2]])}
,它工作得很好。我认为tf.contrib会自动为每个特征输入创建平方项,甚至一些立方项,以便解决更多的实际应用程序。