1
作爲一個練習,我嘗試使用tf.contrib.learn.LinearRegressor來爲方程建模y = 3 * x1^2 + 4 * x2^2。代碼運行,但我對結果的準確性有點失望。這個結果對於線性方程如y = 3 * x1 + 4 * x2是很好的。我認爲tf.contrib.learn可以很好地處理平方條款。是否有可用於獲得更好結果的設置,如不同的優化器?線性迴歸,Tensorflow,非線性方程,tf.contrib.learn
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)
改變了我的代碼,這樣的:'高清input_fn(input_batch,output_batch): 輸入= { 「X1」:tf.constant(input_batch [:,0] ** 2),「x2」:tf.constant(input_batch [:,1] ** 2)}'還有'def input_fn_predict(x1,x2): inputs = {「x1」 tf.constant([[x1 ** 2]]),「x2」:tf.constant([[x2 ** 2]])}',效果很好。我認爲tf.contrib會自動爲每個功能輸入創建平方和甚至一些立方項,以便解決更多真實世界的應用程序。 –