Skip to content
Browse files

Python seeing empty arrays

  • Loading branch information...
1 parent 0299612 commit a993319e029371f66647942aa9f3db0ef88e96aa Claudio A Andreoni committed Dec 2, 2011
Showing with 94 additions and 0 deletions.
  1. +94 −0 asgd/pasgd.pyx
View
94 asgd/pasgd.pyx
@@ -3,6 +3,7 @@ from libcpp cimport bool
from libc cimport stdlib
cimport numpy as np
+import numpy as np
np.import_array()
cdef extern from "asgd.h":
@@ -13,7 +14,11 @@ cdef extern from "asgd.h":
float* data
ctypedef struct nb_asgd_t:
+ matrix_t* sgd_weights
+ matrix_t* sgd_bias
matrix_t* asgd_weights
+ matrix_t* asgd_bias
+ size_t n_iters
nb_asgd_t* nb_asgd_init(uint64_t n_feats, float sgd_step_size0, float l2_reg, uint64_t n_iters, bool feedback)
@@ -41,7 +46,96 @@ cdef class ASGD:
def __dealloc__(self):
nb_asgd_destr(self.data)
+ def sgd_weights(self):
+ cdef np.npy_intp r_dims[2]
+ r_dims[0] = self.data[0].sgd_weights[0].rows
+ r_dims[1] = self.data[0].sgd_weights[0].cols
+ return np.PyArray_SimpleNewFromData(2, r_dims, np.NPY_FLOAT32, <void*>self.data[0].sgd_weights[0].data)
+
+ def partial_fit(self, np.ndarray X, np.ndarray y):
+ # ensure that the matrices are stored in contiguous C format
+ if np.PyArray_FLAGS(X) & np.NPY_C_CONTIGUOUS == False:
+ msg = 'decision_function: X should be in C contiguous format'
+ raise Exception(msg)
+
+ if np.PyArray_FLAGS(y) & np.NPY_C_CONTIGUOUS == False:
+ msg = 'decision_function: y should be in C contiguous format'
+ raise Exception(msg)
+
+ cdef matrix_t Xc
+ cdef np.npy_intp* X_dims = np.PyArray_DIMS(X)
+ Xc.rows = X_dims[0]
+ Xc.cols = X_dims[1]
+ Xc.data = <float*>np.PyArray_DATA(X)
+ print "np. ",(<float*>np.PyArray_DATA(X))[1]
+ print "ar. ",X[1]
+
+ cdef matrix_t yc
+ cdef np.npy_intp* y_dims = np.PyArray_DIMS(y)
+ yc.rows = y_dims[0]
+ yc.cols = y_dims[1]
+ yc.data = <float*>np.PyArray_DATA(y)
+
+ partial_fit(self.data, &Xc, &yc)
+
+ def fit(self, np.ndarray X, np.ndarray y):
+ # ensure that the matrices are stored in contiguous C format
+ if np.PyArray_FLAGS(X) & np.NPY_C_CONTIGUOUS == False:
+ msg = 'decision_function: X should be in C contiguous format'
+ raise Exception(msg)
+
+ if np.PyArray_FLAGS(y) & np.NPY_C_CONTIGUOUS == False:
+ msg = 'decision_function: y should be in C contiguous format'
+ raise Exception(msg)
+
+ cdef matrix_t Xc
+ cdef np.npy_intp* X_dims = np.PyArray_DIMS(X)
+ Xc.rows = X_dims[0]
+ Xc.cols = X_dims[1]
+ Xc.data = <float*>np.PyArray_DATA(X)
+
+ cdef matrix_t yc
+ cdef np.npy_intp* y_dims = np.PyArray_DIMS(y)
+ yc.rows = y_dims[0]
+ yc.cols = y_dims[1]
+ yc.data = <float*>np.PyArray_DATA(y)
+
+ cdef int* rc
+ cdef np.ndarray r = np.random.random(self.data[0].n_iters*(X_dims[0]-1))
+ rc = <int*>np.PyArray_DATA(r)
+
+ fit(self.data, &Xc, &yc, rc)
+
def decision_function(self, np.ndarray X):
+ # ensure that the matrix is stored in contiguous C format
+ if np.PyArray_FLAGS(X) & np.NPY_C_CONTIGUOUS == False:
+ msg = 'decision_function: X should be in C contiguous format'
+ raise Exception(msg)
+
+ cdef matrix_t Xc
+ cdef np.npy_intp* X_dims = np.PyArray_DIMS(X)
+ Xc.rows = X_dims[0]
+ Xc.cols = X_dims[1]
+ Xc.data = <float*>np.PyArray_DATA(X)
+
+ cdef np.npy_intp r_dims[2]
+ r_dims[0] = X_dims[0]
+ r_dims[1] = self.data[0].asgd_weights.cols
+ cdef r = np.PyArray_SimpleNew(2, r_dims, np.NPY_FLOAT32)
+
+ cdef matrix_t rc
+ rc.rows = r_dims[0]
+ rc.cols = r_dims[1]
+ rc.data = <float*>np.PyArray_DATA(r)
+
+ decision_function(self.data, &Xc, &rc);
+ return r
+
+ def predict(self, np.ndarray X):
+ # ensure that the matrix is stored in contiguous C format
+ if np.PyArray_FLAGS(X) & np.NPY_C_CONTIGUOUS == False:
+ msg = 'predict: X should be in C contiguous format'
+ raise Exception(msg)
cdef matrix_t Xc
cdef np.npy_intp* X_dims = np.PyArray_DIMS(X)

0 comments on commit a993319

Please sign in to comment.
Something went wrong with that request. Please try again.