在没有张量流的相同逻辑正常工作的情况下,张量流无法训练模型

时间:2020-04-01 04:26:45

标签: python tensorflow regression

我是Tensorflow的新手。我写了两个线性回归代码。一种使用tensorflow,另一种不使用tensorflow(仅numpy)。使用numpy进行回归可以很好地工作,但是当我将该逻辑转换为TensorFlow时,似乎做错了什么。我已经研究了张量流的一些基础知识(即图,张量和会话)。当我运行代码时,没有出现任何错误,权重甚至都没有变化。这是我的线性回归的张量流版本。

p.s。我知道可以实现此回归的其他内置函数,但是这些函数非常抽象,可以在处理实际问题时使用。在学习机器学习期间,并不是使用内置函数的好方法。

import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt

#graph setting
tf.reset_default_graph()

#data
x = tf.placeholder(shape=(20,2), dtype=tf.float32, name="x")
y = tf.placeholder(shape=(20,1), dtype=tf.float32, name="y")
theta = tf.placeholder(shape=(2,1), dtype=tf.float32, name="theta")

#prediction
def predict(x, theta):
    '''
        x: (20, 2)
        theta: (2, 1)
        return: (20, 1)
    '''
    return tf.matmul(x, theta, name="predict")

#cost function
def mse_cost(y_predict, y_actual, m):
    '''
        y_predict: (20, 1)
        y_actual: (20, 1)
        return: 0 dimention tensor
    '''
    return tf.math.devide(tf.math.reduce_sum(tf.math.squared_difference(y_predict, y_actual, name="squared difference"), name="reduced_sum"), m, name="devide")

#gradient descent function
def gradient(x, theta, predicted, actual, alpha):
    '''
        ##dimention reduction
        theta = theta - alpha * (x.T * (predicted - actual))
              = (2, 1) - (1) * ((2, 20) * (20, 1))
              = (2, 1) - (2, 1)
              = (2, 1)
    '''
    step = tf.math.multiply(alpha, tf.matmul(tf.transpose(x), tf.math.subtract(predicted, actual)), name="step")
    return tf.subtract(theta, step, name="subtraction")

def train(x, y, theta, epoc, alpha):
    '''
        x: (20, 2)
        y: (20, 1)
        theta: (2, 1)
        epoc: int
        alpha: float
    '''
    for _ in range(0, epoch):
        predicted = predict(x, theta)
        theta = gradient(x, theta, predicted, y, alpha)
    return theta

theta = train(x, y, theta, 10000, 0.1)
with tf.Session() as sess:
    x_numpy_data = []
    y_numpy_data = []
    x_numpy_data = np.ones((20,2))
    x_numpy_data[:, 1] = np.arange(0,20)
    y_numpy_data = np.array([(0.2 * i + 2 + np.log(i + 1)) for i in range(0, 20)]).reshape(20,1)
    writer = tf.summary.FileWriter('./graph', sess.graph)
    theta = sess.run(theta, feed_dict={x: x_numpy_data, y: y_numpy_data, theta: np.array([np.random.normal(), np.random.normal()]).reshape(2, 1)})

x = []
y = []
x = np.ones((20,2))
x[:, 1] = np.arange(0,20)
y = np.array([(0.2 * i + 2 + np.log(i + 1)) for i in range(0, 20)]).reshape(20,1) 
print(theta)

#creating figure instance
fig = plt.figure(figsize=(10, 5), dpi=80, facecolor='w', edgecolor='#3EF644', linewidth=3)
fig.suptitle("After training")
axes = fig.subplots()

#data
'''
    theta: (2, 1)
    x: (20, 2)
'''
axes.plot(x[ : , 1], np.dot(x, theta), marker = "x", linestyle="-", label="predicted", markersize="20")
axes.plot(y,marker = "o", linestyle="-", label="actual")
axes.legend()
axes.set_xlabel("x")
axes.set_ylabel("y")

plt.show()

0 个答案:

没有答案