Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tensorflow as tf
- from sklearn import metrics, model_selection
- from sklearn.datasets import load_boston
- import numpy as np
- dataset = load_boston()
- print(dataset.keys())
- test_size = 0.2
- seed = 7
- x, y = dataset['data'], dataset['target']
- print("Info: ")
- print("Dim(x): ", x.shape, "\n", "Dim(y): ", y.shape)
- print("Average data: ", np.average(x, axis=0))
- print("Average target: ", np.average(y))
- x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=test_size, random_state=seed)
- y_train = np.array([y_train]).T
- y_test = np.array([y_test]).T
- # build model
- input_x = tf.placeholder(shape=[None, 13], dtype=tf.float64, name="Input_X")
- input_y = tf.placeholder(dtype=tf.float64, name="Input_Y")
- weights = tf.Variable(tf.random_normal([13,1], dtype=tf.float64), name="Weight")
- bias = tf.Variable(0, dtype=tf.float64, name="Bias")
- predict = tf.add(tf.matmul(input_x, weights), bias, name="Model")
- cost = tf.reduce_sum(tf.square(input_y-predict))
- optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01, name="GradientDescent").minimize(cost, name="Train_min")
- sess = tf.Session()
- init = tf.global_variables_initializer()
- # Train model
- sess.run(init)
- for i in range(10):
- c, _ = sess.run([cost, optimizer], feed_dict={input_x: x_train, input_y: y_train})
- print("Cost of ", i, " is ", c)
- writter = tf.summary.FileWriter(logdir="/tmp/linear/1", graph=sess.graph)
- w, b, pred = sess.run([weights, bias, predict], feed_dict={input_x: x_test})
- print("Weight: ", w, "\n Bias: ", b, "\nAverage pred: ", np.mean(pred, axis=0))
Advertisement
Add Comment
Please, Sign In to add comment