-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
4,136 additions
and
24 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "Threadpool/Threadpool.hpp" | ||
|
||
#include <iostream> | ||
namespace ThreadLib { | ||
|
||
void Worker::operator()() { | ||
while(true) { | ||
std::unique_lock<std::mutex> lock(pool.queue_mutex); | ||
|
||
while(!pool.stop && pool.tasks.empty()) | ||
pool.condition.wait(lock); | ||
|
||
if(pool.stop && pool.tasks.empty()) | ||
return; | ||
|
||
std::function<void()> task(pool.tasks.top().second); | ||
pool.tasks.pop(); | ||
|
||
lock.unlock(); | ||
|
||
task(); | ||
} | ||
} | ||
|
||
// the constructor just launches some amount of workers | ||
Threadpool::Threadpool(Threadpool::size_type threads) : stop(false) { | ||
workers.reserve(threads); | ||
|
||
for(Threadpool::size_type i = 0; i < threads; ++i) | ||
workers.emplace_back(Worker(*this)); | ||
} | ||
|
||
// the destructor joins all threads | ||
Threadpool::~Threadpool() { | ||
{ | ||
std::unique_lock<std::mutex> lock(queue_mutex); | ||
stop = true; | ||
condition.notify_all(); | ||
} | ||
|
||
for(Threadpool::size_type i = 0; i < workers.size(); ++i) { | ||
workers[i].join(); | ||
} | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* $File: common.hh | ||
* $Date: Sun Sep 08 08:35:24 2013 +0800 | ||
* $Author: Xinyu Zhou <zxytim[at]gmail[dot]com> | ||
*/ | ||
|
||
#include "type.hh" | ||
#include "dataset.hh" | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cassert> | ||
#include <cstring> | ||
|
||
#include <memory> | ||
#include <algorithm> | ||
#include <vector> | ||
#include <set> | ||
#include <map> | ||
#include <queue> | ||
#include <limits> | ||
|
||
#define For(i, n) for (__decltype(n) i = 0; i < (n); i ++) | ||
#define Forr(i, l, r) for (__decltype(r) i = (l); i <= (r); i ++) | ||
|
||
/** | ||
* vim: syntax=cpp11 foldmethod=marker | ||
*/ | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/* | ||
* $File: datamanip.cc | ||
* $Date: Thu Dec 05 00:08:18 2013 +0000 | ||
* $Author: Xinyu Zhou <zxytim[at]gmail[dot]com> | ||
*/ | ||
|
||
#include "datamanip.hh" | ||
|
||
#include "common.hh" | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
|
||
using namespace std; | ||
|
||
static const int BUF_SIZE = 65536; | ||
|
||
void read_svm_data(const char *fpath, Dataset &dataset, Labels &labels) { | ||
FILE *fin = fopen(fpath, "r"); | ||
dataset.resize(0); | ||
labels.resize(0); | ||
|
||
char *buf = new char[BUF_SIZE]; | ||
while (fgets(buf, BUF_SIZE, fin) == buf) { | ||
Instance x; | ||
char *ptr; | ||
for (ptr = buf; *ptr; ptr ++) { | ||
if (*ptr == ' ' || *ptr == '\n' || *ptr == '\r') { | ||
*ptr = 0; | ||
int label = atoi(buf); | ||
/* | ||
if (label != 0 && label != 1) { | ||
printf("!!!!%d\n", label); | ||
} | ||
*/ | ||
labels.push_back(label); | ||
*ptr = ' '; | ||
break; | ||
} | ||
} | ||
char *last = ptr; | ||
int ind = -1; | ||
double val; | ||
for (; ; ptr ++) { | ||
if (*ptr == ' ' || *ptr == '\n' || *ptr == '\r' || *ptr == 0) { | ||
if (ind != -1) { | ||
char orig = *ptr; | ||
*ptr = 0; | ||
val = atof(last); | ||
x.push_back(make_pair(ind, val)); | ||
*ptr = orig; | ||
ind = -1; | ||
} | ||
last = ptr + 1; | ||
} else if (*ptr == ':') { | ||
*ptr = 0; | ||
ind = atoi(last); | ||
last = ptr + 1; | ||
*ptr = ':'; | ||
} | ||
if (*ptr == 0) | ||
break; | ||
} | ||
dataset.push_back(x); | ||
} | ||
delete [] buf; | ||
fclose(fin); | ||
} | ||
|
||
void read_svm_data(const char *fpath, Dataset &dataset, RealLabels &labels) { | ||
FILE *fin = fopen(fpath, "r"); | ||
dataset.resize(0); | ||
labels.resize(0); | ||
|
||
char *buf = new char[BUF_SIZE]; | ||
while (fgets(buf, BUF_SIZE, fin) == buf) { | ||
Instance x; | ||
char *ptr; | ||
for (ptr = buf; *ptr; ptr ++) { | ||
if (*ptr == ' ' || *ptr == '\n' || *ptr == '\r') { | ||
*ptr = 0; | ||
real_t label = atof(buf); | ||
/* | ||
if (label != 0 && label != 1) { | ||
printf("!!!!%d\n", label); | ||
} | ||
*/ | ||
labels.push_back(label); | ||
*ptr = ' '; | ||
break; | ||
} | ||
} | ||
char *last = ptr; | ||
int ind = -1; | ||
double val; | ||
for (; ; ptr ++) { | ||
if (*ptr == ' ' || *ptr == '\n' || *ptr == '\r' || *ptr == 0) { | ||
if (ind != -1) { | ||
char orig = *ptr; | ||
*ptr = 0; | ||
val = atof(last); | ||
x.push_back(make_pair(ind, val)); | ||
*ptr = orig; | ||
ind = -1; | ||
} | ||
last = ptr + 1; | ||
} else if (*ptr == ':') { | ||
*ptr = 0; | ||
ind = atoi(last); | ||
last = ptr + 1; | ||
*ptr = ':'; | ||
} | ||
if (*ptr == 0) | ||
break; | ||
} | ||
dataset.push_back(x); | ||
} | ||
delete [] buf; | ||
fclose(fin); | ||
} | ||
|
||
void print_instance(FILE *fout, const Instance &instance) { | ||
for (auto item: instance) | ||
fprintf(fout, "%d:%lf ", item.first, item.second); | ||
} | ||
|
||
void print_data(FILE *fout, const Dataset &dataset) { | ||
for (auto instance: dataset) { | ||
print_instance(fout, instance); | ||
fprintf(fout, "\n"); | ||
} | ||
} | ||
|
||
void print_data(FILE *fout, const Dataset &dataset, const Labels &labels) { | ||
assert(dataset.size() == labels.size()); | ||
For(i, dataset.size()) { | ||
fprintf(fout, "%d ", labels[i]); | ||
print_instance(fout, dataset[i]); | ||
fprintf(fout, "\n"); | ||
} | ||
} | ||
|
||
void print_data(FILE *fout, const Dataset &dataset, const RealLabels &labels) { | ||
assert(dataset.size() == labels.size()); | ||
For(i, dataset.size()) { | ||
fprintf(fout, "%f ", labels[i]); | ||
print_instance(fout, dataset[i]); | ||
fprintf(fout, "\n"); | ||
} | ||
} | ||
|
||
void print_labels(FILE *fout, const Labels &labels) { | ||
for (auto i: labels) | ||
fprintf(fout, "%d\n", i); | ||
} | ||
|
||
void print_labels(FILE *fout, const RealLabels &labels) { | ||
for (auto i: labels) | ||
fprintf(fout, "%f\n", i); | ||
} | ||
|
||
void get_data_metric(const Dataset &x, int &n, int &m) { | ||
n = x.size(); | ||
m = 0; | ||
for (size_t i = 0; i < x.size(); i ++) { | ||
const Instance &xi = x[i]; | ||
for (size_t j = 0; j < xi.size(); j ++) | ||
if (xi[j].first + 1 > m) | ||
m = xi[j].first + 1; | ||
} | ||
} | ||
|
||
void get_data_metric(const std::vector<std::vector<real_t> >&x, int &n, int &m) { | ||
if (x.size() == 0) { | ||
n = m = -1; | ||
return; | ||
} | ||
n = x.size(); | ||
m = x[0].size(); | ||
for (size_t i = 0; i < x.size(); i ++) | ||
if ((int)x[i].size() != m) { | ||
n = m = -1; | ||
break; | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
/** | ||
* vim: syntax=cpp11 foldmethod=marker | ||
*/ | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* $File: datamanip.hh | ||
* $Date: Thu Dec 05 00:08:15 2013 +0000 | ||
* $Author: Xinyu Zhou <zxytim[at]gmail[dot]com> | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "dataset.hh" | ||
|
||
#include <cstdio> | ||
#include <cassert> | ||
#include "random.hh" | ||
|
||
// integer label | ||
void read_svm_data(const char *fpath, Dataset &dataset, Labels &labels); | ||
void read_svm_data(const char *fpath, Dataset &dataset, RealLabels &labels); | ||
void print_data(FILE *fout, const Dataset &dataset); | ||
void print_data(FILE *fout, const Dataset &dataset, const Labels &labels); | ||
void print_data(FILE *fout, const Dataset &dataset, const RealLabels &labels); | ||
void print_labels(FILE *fout, const Labels &labels); | ||
void print_labels(FILE *fout, const RealLabels &labels); | ||
void print_instance(FILE *fout, const Instance &instance); | ||
|
||
void get_data_metric(const Dataset &x, int &n, int &m); | ||
void get_data_metric(const std::vector<std::vector<real_t> >&x, int &n, int &m); | ||
|
||
template<typename Dataset> | ||
void get_refdata_metric(const Dataset &x, int &n, int &m) { | ||
n = x.size(); | ||
m = 0; | ||
for (size_t i = 0; i < x.size(); i ++) { | ||
auto &xi = *x[i]; | ||
for (size_t j = 0; j < xi.size(); j ++) | ||
if (xi[j].first + 1 > m) | ||
m = xi[j].first + 1; | ||
} | ||
} | ||
|
||
// n_choose == -1 means the same number as dataset.size() | ||
template<typename Dataset_t, typename Labels_t> | ||
void bootstrap_samples(const Dataset_t &dataset, const Labels_t &labels, | ||
Dataset_t &subdataset, Labels_t &sublabels, int n_choose = -1) { | ||
Random random; | ||
assert(dataset.size() == labels.size()); | ||
subdataset.resize(0); sublabels.resize(0); | ||
if (n_choose == -1) | ||
n_choose = dataset.size(); | ||
for (int i = 0; i < n_choose; i ++) { | ||
int index = random.rand_int(dataset.size()); | ||
subdataset.push_back(dataset[index]); | ||
sublabels.push_back(labels[index]); | ||
} | ||
} | ||
|
||
/** | ||
* vim: syntax=cpp11 foldmethod=marker | ||
*/ | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.