In [23]:
%matplotlib inline
In [2]:
import matplotlib.pyplot as plt
In [3]:
import numpy
import numpy as np
import pandas
import pandas as pd
In [4]:
import os, sys
print(os.getcwd() )
print( os.listdir( "../data/") )
In [5]:
ex1data1DF = pd.read_csv("../data/ex1data1.txt",header=None)
ex1data2DF = pd.read_csv("../data/ex1data2.txt", header=None)
In [6]:
Xyex1data1 = ex1data1DF.values
Xex1data1 = Xyex1data1[:,0]
yex1data1 = Xyex1data1[:,1]
Xyex1data2 = ex1data2DF.values
Xex1data2 = Xyex1data2[:,0:-1]
yex1data2 = Xyex1data2[:,-1]
print(Xex1data1.shape)
print(yex1data1.shape)
print(Xex1data2.shape)
print(yex1data2.shape)
Xex1data1 = np.vstack( Xex1data1 )
yex1data1 = np.vstack( yex1data1 )
yex1data2 = np.vstack( yex1data2 )
print(Xex1data1.shape)
print(yex1data1.shape)
print(Xex1data2.shape)
print(yex1data2.shape)
In [30]:
m_1 = Xex1data1.shape[0]
print(m_1)
In [7]:
Theta=np.array([0.])
b = np.array([0.])
In [8]:
a1 = np.matmul( Xex1data1, Theta) + b
print(a1)
In [9]:
res = np.vstack(a1) - yex1data1
print(np.hstack(res))
In [10]:
np.matmul( np.hstack(res) , res)
Out[10]:
In [11]:
0.5 * np.matmul( np.hstack(res) , res) / (float( Xex1data1.size ) )
Out[11]:
In [12]:
Xex1data1.size
Out[12]:
In [13]:
np.matmul( np.hstack(res) , Xex1data1) / (float( Xex1data1.size ))
Out[13]:
In [14]:
def feedfwd(X,Theta,b):
a1 = np.matmul(X,Theta)
a1b = a1 + b
return a1,a1b
In [15]:
def costJ(X,Theta,b,y):
m =X.shape[0]
a1,a1b = feedfwd(X,Theta,b)
res = (np.vstack(a1b) - y)
J = 0.5 * np.matmul( np.hstack(res),res) /float(m)
return res,J
In [16]:
a1_temp,a1b_temp = feedfwd(Xex1data1,Theta,b)
In [17]:
a1b_temp
Out[17]:
In [18]:
feedfwd(Xex1data1,Theta,b)
Out[18]:
In [19]:
costJ(Xex1data1,Theta,b,yex1data1)
Out[19]:
In [25]:
def grad_desc_1( a0, yhat, y , Theta, b, alpha):
m = y.shape[0]
res = np.vstack(yhat) - y
d_Theta = np.matmul( np.hstack(res), a0 )
d_b = np.sum( res, axis=0)
Thetatp1 = Theta - alpha * d_Theta/(float(m)) # gradient descent
btp1 = b - alpha * d_b / (float(m)) # gradient descent
return d_Theta,d_b , Thetatp1, btp1
In [26]:
# expect
#(array([-6336.89842532]),
# array([-566.3961]),
# array([ 0.6532885]),
# array([ 0.05839135]))
grad_desc_1( Xex1data1, a1b_temp , yex1data1, Theta,b,0.01)
Out[26]:
In [33]:
np.matmul( np.ones(1*m_1), res)
Out[33]:
In [34]:
np.sum( res,axis=0)
Out[34]:
In [22]:
res = np.vstack(a1b_temp) - yex1data1
print(res.shape)
In [35]:
def gradDesc( X,y, Theta_0,b_0, alpha, iters):
Theta=Theta_0
b=b_0
params_hist = []
for iter in range(iters):
a1,a1b = feedfwd(X,Theta,b)
res,J = costJ(X,Theta,b,y)
d_Theta,d_b,Thetatp1,btp1 = grad_desc_1(X,np.vstack(a1b),y,Theta,b,alpha)
Theta = Thetatp1
b=btp1 # update
params = list( [a1,a1b,res,J,d_Theta,d_b,Thetatp1,btp1 ])
params_hist.append(params)
return params_hist
In [36]:
import timeit
In [39]:
start_time = timeit.default_timer()
result1500 = gradDesc(Xex1data1,yex1data1, Theta,b,0.01,1500)
elapsedtime = timeit.default_timer() - start_time
In [40]:
print(elapsedtime ) # in seconds
In [23]:
a1,a1b = feedfwd(Xex1data1, Theta,b)
res,J = costJ(Xex1data1,Theta,b,yex1data1)
d_Theta,d_b,Theta1p1, btp1 = grad_desc_1(Xex1data1,a1b,yex1data1,Theta,b,0.001)
In [24]:
result1500[-1][3:]
Out[24]:
In [69]:
result10 = gradDesc(Xex1data1,yex1data1,Theta,b,0.001,10)
In [85]:
feedfwd(np.array([3.5,7]).reshape((2,1)),result1500[-1][-2],result1500[-1][-1])
Out[85]:
In [84]:
result1500[-2]
Out[84]:
In [73]:
a1b.shape
Out[73]:
In [91]:
plt.plot( np.array(result1500)[:,3])
Out[91]:
In [93]:
plt.plot( np.array(result1500)[2:,3])
Out[93]:
In [1]:
# http://scikit-learn.org/stable/modules/preprocessing.html#normalization
import sklearn
from sklearn import preprocessing
In [31]:
Xex1data2 = preprocessing.normalize(ex1data2DF.values[:,:-1].astype(np.float32), axis=0, norm='l2' )
In [32]:
print( Xex1data2.dtype)
Xex1data2
Out[32]:
numpy.ndarray.tofilendarray.tofile(fid,sep="",format="%s")
Write array to a file as text or binary (default).
Data always written in 'C' order, independent of order of a.
fid : file or str
An open file object, or string containing filename.
sep : str
Separator between array items for text output. If "" (empty), a binary file is written, equivalent to file.write(a.tobytes())
format : str
Format string for text file output. Each entry in the array is formatted to text by first converting it to closest Python type, and then using "format" % item.
In [33]:
Xex1data2.tofile("../data/" + "Xex1data2.npy")
Then go to CUDA C++14 file (e.g. cuBlackDreams/examples/linreg.cu in thise case). Load it with std::ifstream, Input stream class to operate on files. This function has been wrapped up in the function you'll find in cuBlackDreams/src/FileIO.h, cuBlackDreams/src/FileIO.cpp,
npy2fvec or std::vector
The most important thing to note is that NumPy reshapes (.reshape) into row-major ordering, i.e.
and so we'll read in this binary file, with std::ifstream and .read(...) in C++ in row-major ordering assumed for the matrix $A$. Or, I've wrapped up the procedure, serially (i.e. for loop), to convert to column-major ordering for a flattened matrix, in npy2fvec in cuBlackDreams/src/FileIO.h.
std::ifstreamtypedef basic_ifstream<char> ifstream;
reinterpret_castreinterpret_cast < new_type > ( expression )
Returns a value of type new_type.
In [34]:
# sanity check
Xex1data2_in = np.fromfile("../data/"+ "Xex1data2.npy", dtype=np.float32)
In [35]:
Xex1data2_in
Out[35]:
In [ ]: