VDB_CALL int vdb_init() { if(!__vdb.is_initialized) { __vdb.is_initialized = 1; __vdb.sample_enabled = 1; vdb_os_init(); __vdb.fd = socket(AF_INET, SOCK_STREAM, 0); if(__vdb.fd == -1) { vdb_report_error(); __vdb.init_error = 1; } else { struct sockaddr_in serv_name; serv_name.sin_family = AF_INET; serv_name.sin_addr.s_addr = htonl(0x7F000001L); serv_name.sin_port = htons(10000); if(-1 == connect(__vdb.fd, (struct sockaddr*) &serv_name, sizeof(serv_name))) { vdb_report_error(); __vdb.init_error = 1; } atexit(vdb_exit); } } if(__vdb.is_initialized == 2) { //this never runs, but it tricks compilers into including these functions as debug symbols //even though they may never be used //useful if you want to call them from the debugger directly vdb_point_v(NULL); vdb_line_v(NULL); vdb_normal_v(NULL); vdb_triangle_v(NULL); vdb_point(0,0,0); vdb_line(0,0,0,0,0,0); vdb_normal(0,0,0,0,0,0); vdb_triangle(0,0,0,0,0,0,0,0,0); vdb_color_v(NULL); vdb_color(0,0,0); vdb_label(""); vdb_label(0); } return __vdb.init_error; }
int main() { double * w = new double[N_DIMS]; bzero(w, sizeof(double) * (N_DIMS)); double (*data)[N_DIMS] = (double(*)[N_DIMS]) malloc(sizeof(double) * N_DIMS * N_ROWS); double * response = new double[N_ROWS]; FILE * file = fopen("../data/lr_p.txt","r"); assert(file); for(int j = 0; j < N_DIMS; j++) { for(int i = 0; i < N_ROWS; i++) { fscanf(file,"%lf",&data[i][j]); } } file = fopen("../data/lr_r.txt","r"); assert(file); for(int j = 0; j < N_ROWS; j++) { fscanf(file, "%lf", &response[j]); } file = fopen("../data/lr_wi.txt","r"); assert(file); for(int j = 0; j < N_DIMS; j++) { fscanf(file, "%lf", &w[j]); } /*for(int i = 0; i < N_ROWS; i++) { double p = drand(); response[i] = 3; for(int d = 0; d < N_DIMS; d++) { data[i][d] = drand(); response[i] += (d + 1)*data[i][d] + (drand() - .5) * .1; } #ifdef VIEW vdb_color(1,1,1); vdb_point(data[i][0],data[i][1],response[i]); #endif }*/ double begin = current_time(); double * grad = new double[N_DIMS]; for(int round = 0; round < N_ITERATIONS; round++) { bzero(grad,sizeof(double) * (N_DIMS)); for(int r = 0; r < N_ROWS; r++) { double r_1 = 1.0 / (r + 1); double diff[2] = {0.,0.}; for(int d = 0; d < N_DIMS; d += 2) { diff[0] += w[d + 0] * data[r][d + 0]; diff[1] += w[d + 1] * data[r][d + 1]; } double diff_ = (diff[0] + diff[1]); diff_ = 1.0/(1.0+exp(-diff_)); diff_ -= response[r]; for(int d = 0; d < N_DIMS; d++) { grad[d] += (diff_*data[r][d] - grad[d]) * r_1; } } for(int d = 0; d < N_DIMS; d++) { w[d] -= .07 * grad[d]; } } printf("Elapsed: %f\n", current_time()-begin); for(int i = 0; i < N_DIMS; i++) printf("w[%d] = %f\n", i, w[i]); #ifdef VIEW vdb_color(1,0,0); vdb_triangle(0,0,w[0],1,0,w[0]+ w[1],0,1,w[0]+w[2]); #endif return 0; }
VDB_CALL int vdb_triangle_fn(float x0, float y0, float z0, float x1, float y1, float z1,float x2, float y2, float z2) { return vdb_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2); }