Ngoc Minh Tran
Ngoc Minh Tran

Reputation: 81

Differences between Numpy and TensorFlow

I am trying to write two scripts that demonstrate locally weighted linear regression. I have used the Numpy to solve matrix problems in the first script as follows:

trX = np.linspace(0, 1, 100) 
trY= trX + np.random.normal(0,1,100)
xArr = []
yArr = []
for i in range(len(trX)):
    xArr.append([1.0,float(trX[i])])
    yArr.append(float(trY[i]))
xMat = mat(xArr); 
yMat = mat(yArr).T
m = shape(xMat)[0]
weights = mat(eye((m)))
k = 0.01
yHat = zeros(m)

for i in range(m):
    for j in range(m):
        diffMat = xArr[i] - xMat[j,:]
        weights[j,j] = exp(diffMat*diffMat.T/(-2.0*k**2))
    xTx = xMat.T * (weights * xMat)
    if linalg.det(xTx) == 0.0:
        print("This matrix is singular, cannot do inverse")
    ws = xTx.I * (xMat.T * (weights * yMat))
    yHat[i] = xArr[i]*ws


plt.scatter(trX, trY) 

plt.plot(trX, yHat, 'r')
plt.show() 

if running the script above, the result: enter image description here

In the second script, I have used the TensorFlow to solve matrix problems. This script looks like this:

trX = np.linspace(0, 1, 100) 
trY= trX + np.random.normal(0,1,100)

sess = tf.Session()
xArr = []
yArr = []
for i in range(len(trX)):
    xArr.append([1.0,float(trX[i])])
    yArr.append(float(trY[i]))

xMat = mat(xArr); 
yMat = mat(yArr).T

A_tensor = tf.constant(xMat)
b_tensor = tf.constant(yMat)

m = shape(xMat)[0]
weights = mat(eye((m)))
k = 0.01
yHat = zeros(m)
for i in range(m):
    for j in range(m):
        diffMat = xMat[i]- xMat[j,:]
        weights[j,j] = exp(diffMat*diffMat.T/(-2.0*k**2))
    weights_tensor = tf.constant(weights)
    # Matrix inverse solution
    wA = tf.matmul(weights_tensor, A_tensor)
    tA_A = tf.matmul(tf.transpose(A_tensor), wA)
    tA_A_inv = tf.matrix_inverse(tA_A)
    wb = tf.matmul(weights_tensor, b_tensor)
    tA_wb = tf.matmul(tf.transpose(A_tensor), wb)
    solution = tf.matmul(tA_A_inv, tA_wb)
    sol_val = sess.run(solution)
    yHat[i] =sol_val[0][0]*xArr[i][1] + sol_val[1][0] 

plt.scatter(trX, trY) 

plt.plot(trX, yHat, 'r')
plt.show() 

If running it:

enter image description here

What do things make difference between two results? Or maybe I have wrong things in my scripts? Please help me.

Upvotes: 3

Views: 364

Answers (1)

user11530462
user11530462

Reputation:

Problem is with the line of the code,

yHat[i] =sol_val[0][0]*xArr[i][1] + sol_val[1][0] 

Numpy Array Multiplication was happening incorrectly.

It will work properly if you replace the above line of code with

yHat[i] =sol_val[0][0]*xArr[i][0] + sol_val[1][0]*xArr[i][1]

Complete working code is given below:

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

import tensorflow as tf

trX = np.linspace(0, 1, 100) 
trY= trX + np.random.normal(0,1,100)
#print('trY = ', trY)

sess = tf.Session()
xArr = []
yArr = []
for i in range(len(trX)):
    xArr.append([1.0,float(trX[i])])
    yArr.append(float(trY[i]))

xMat = mat(xArr); 
yMat = mat(yArr).T

A_tensor = tf.constant(xMat)
b_tensor = tf.constant(yMat)

#print("A_Tensor = xMat = ", sess.run(A_tensor))
#print("B_Tensor = yMat = ", sess.run(b_tensor))

m = shape(xMat)[0]
weights = mat(eye((m)))
k = 0.01
yHat = zeros(m)
for i in range(m):
    for j in range(m):
        diffMat = xMat[i]- xMat[j,:]
        weights[j,j] = exp(diffMat*diffMat.T/(-2.0*k**2))
    weights_tensor = tf.constant(weights)    
    # Matrix inverse solution
    wA = tf.matmul(weights_tensor, A_tensor)
    tA_A = tf.matmul(tf.transpose(A_tensor), wA)
    tA_A_inv = tf.matrix_inverse(tA_A)
    wb = tf.matmul(weights_tensor, b_tensor)
    tA_wb = tf.matmul(tf.transpose(A_tensor), wb)
    solution = tf.matmul(tA_A_inv, tA_wb)
    sol_val = sess.run(solution)
    #plt.plot(sol_val, 'b')
    #plt.show()
    #print("Sol_Val = ", sol_val)
    #print("Sol_Val[0][0] = ", sol_val[0][0])
    #print("Sol_Val[1][0] = ", sol_val[1][0])
    #print('xArr[i] = ', np.array(xArr[i]))
    #print('xArr[i][0] = ', np.array(xArr[i][0]))
    #print('xArr[i][1] = ', np.array(xArr[i][1]))
    #yHat[i] =sol_val[0][0]*xArr[i][1] + sol_val[1][0]
    yHat[i] =sol_val[0][0]*xArr[i][0] + sol_val[1][0]*xArr[i][1]
    #print("Weights = ", sess.run(weights_tensor))
    #yHat[i] = np.array(xArr[i])*sol_val
    #print(sol_val)

plt.scatter(trX, trY) 

plt.plot(trX, yHat, 'r')
plt.show()

The plot is shown below:

enter image description here

Upvotes: 1

Related Questions