Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop/two stage #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 75 additions & 3 deletions ICE/ICE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ void ICE::LoadDict(unordered_map<string, unordered_map<string, double>>& graph)
vvnet.LoadDict(graph);
}

void ICE::SaveWeights(string model_name){
void ICE::LoadWeights(string filename){
vvnet.LoadWeights(filename, w_context);
}

void ICE::SaveVertexWeights(string model_name){

cout << "Save Model:" << endl;
ofstream model(model_name);
Expand All @@ -43,6 +47,66 @@ void ICE::SaveWeights(string model_name){
}
}

void ICE::SaveContextWeights(string model_name){

cout << "Save Model:" << endl;
ofstream model(model_name);
if (model)
{
//model << vvnet.MAX_vid << " " << dim << endl;
for (auto k: vvnet.keys)
{
if (vvnet.vertex[vvnet.kmap[k]].branch > 0)
{
model << k;
for (int d=0; d<dim; ++d)
model << " " << w_context[vvnet.kmap[k]][d];
model << endl;
}
}
cout << "\tSave to <" << model_name << ">" << endl;
}
else
{
cout << "\tfail to open file" << endl;
}
}

void ICE::SaveEntityWeights(string model_name){

cout << "Save Model:" << endl;
ofstream model(model_name);
int i = 0;
if (model)
{
//model << vvnet.MAX_vid << " " << dim << endl;
for (auto k: vvnet.keys)
{
// cout << k << endl;
if (i < vocab_count){
i++;
continue;
}
if (vvnet.vertex[vvnet.kmap[k]].branch > 0)
{
model << k;
for (int d=0; d<dim; ++d)
model << " " << w_vertex[vvnet.kmap[k]][d];
model << endl;
}
}
cout << "\tSave to <" << model_name << ">" << endl;
}
else
{
cout << "\tfail to open file" << endl;
}
}

int ICE::getVocabCount(){
vocab_count = vvnet.keys.size();
}

void ICE::Init(int dimension) {

cout << "Model Setting:" << endl;
Expand Down Expand Up @@ -70,7 +134,7 @@ void ICE::Init(int dimension) {
}


void ICE::Train(int sample_times, int negative_samples, double alpha, double alpha_min, int workers){
void ICE::Train(int sample_times, int negative_samples, double alpha, double alpha_min, int workers, int vocab_count, string mode){

omp_set_num_threads(workers);

Expand Down Expand Up @@ -103,7 +167,15 @@ void ICE::Train(int sample_times, int negative_samples, double alpha, double alp
{
v1 = vvnet.SourceSample();
v2 = vvnet.TargetSample(v1);
vvnet.UpdatePair(w_vertex, w_context, v1, v2, dim, negative_samples, _alpha);

if (mode == "stage1"){
vvnet.UpdatePair(w_vertex, w_context, v1, v2, dim, negative_samples, _alpha);
}
else{
if (v1 >= vocab_count){
Copy link
Collaborator

@chihming chihming Nov 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (v1 >= vocab_count)

what's the function of this condition? (you wanna sample a node with an offset vocab_count? if yes, I think we can use "v1+vocab_count" instead)

vvnet.UpdateVertex(w_vertex, w_context, v1, v2, dim, negative_samples, _alpha);
}
}

count++;
if (count % MONITOR == 0)
Expand Down
9 changes: 7 additions & 2 deletions ICE/ICE.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ class ICE {

// model parameters
int dim;
int vocab_count;
vector< vector<double> > w_vertex;
vector< vector<double> > w_context;

// data function
void LoadEdgeList(string);
void LoadWeights(string);
void LoadItemConceptList(string);
void LoadDict(unordered_map<string, unordered_map<string, double>>&);
void SaveWeights(string);
void SaveVertexWeights(string);
void SaveContextWeights(string);
void SaveEntityWeights(string);
int getVocabCount();

// model function
void Init(int);
void Train(int, int, double, double, int);
void Train(int, int, double, double, int, int, string);

};

Expand Down
68 changes: 56 additions & 12 deletions ICE/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ int main(int argc, char **argv){
printf("[ICE-CLI]\n");
printf("\tcommand line interface for ICE\n\n");
printf("Options:\n");
printf("\t-train <string>\n");
printf("\t\tTrain the Network data\n");
// printf("\t-train <string>\n");
// printf("\t\tTrain the Network data\n");
printf("\t-text <string>\n");
printf("\t\ttext-text network data\n");
printf("\t-entity <string>\n");
printf("\t\tentity-text network data\n");
printf("\t-textrep <string>\n");
printf("\t\tsave the word representation data\n");
printf("\t-textcontext <string>\n");
printf("\t\tsave the word context data\n");
printf("\t-save <string>\n");
printf("\t\tSave the representation data\n");
printf("\t\tSave the entity representation data\n");
printf("\t-save_times <int>\n");
printf("\t\tsave a model <int> times; default is 1\n");
printf("\t-dim <int>\n");
Expand All @@ -38,16 +46,21 @@ int main(int argc, char **argv){
printf("\t-alpha <float>\n");
printf("\t\tInit learning rate; default is 0.025\n");
printf("\nExample Usage:\n");
printf("\t./ice -train network.txt -save rep.txt -dim 64 -sample 10 -neg 5 -alpha 0.025 -thread 4\n\n");
printf("\t./ice -text ../data/word.txt -entity ../data/entity.txt -textrep ../data/text.embd -save ../data/result.txt -textcontext ../data/context.txt -dim 4 -sample 10 -neg 5 -alpha 0.025 -thread 4\n\n");
return 0;
}

char network_file[100], rep_file[100];
char text_file[100], text_rep_file[100], text_context_file[100], entity_file[100], result_file[100];
int dimensions=64, negative_samples=5, sample_times=10, save_times=1, threads=1;
double init_alpha=0.025;

if ((i = ArgPos((char *)"-train", argc, argv)) > 0) strcpy(network_file, argv[i + 1]);
if ((i = ArgPos((char *)"-save", argc, argv)) > 0) strcpy(rep_file, argv[i + 1]);
// if ((i = ArgPos((char *)"-train", argc, argv)) > 0) strcpy(network_file, argv[i + 1]);
// if ((i = ArgPos((char *)"-save", argc, argv)) > 0) strcpy(rep_file, argv[i + 1]);
if ((i = ArgPos((char *)"-text", argc, argv)) > 0) strcpy(text_file, argv[i + 1]);
if ((i = ArgPos((char *)"-entity", argc, argv)) > 0) strcpy(entity_file, argv[i + 1]);
if ((i = ArgPos((char *)"-textrep", argc, argv)) > 0) strcpy(text_rep_file, argv[i + 1]);
if ((i = ArgPos((char *)"-textcontext", argc, argv)) > 0) strcpy(text_context_file, argv[i + 1]);
if ((i = ArgPos((char *)"-save", argc, argv)) > 0) strcpy(result_file, argv[i + 1]);
if ((i = ArgPos((char *)"-save_times", argc, argv)) > 0) save_times = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-dim", argc, argv)) > 0) dimensions = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-sample", argc, argv)) > 0) sample_times = atoi(argv[i + 1]);
Expand All @@ -57,8 +70,9 @@ int main(int argc, char **argv){

ICE *ice;
ice = new ICE();
ice->LoadEdgeList(network_file);
ice->LoadEdgeList(text_file);
ice->Init(dimensions);
ice->getVocabCount();

int sub_sample_times = sample_times/save_times, current_sample_times=0;
double alpha_max=init_alpha, alpha_min=init_alpha;
Expand All @@ -68,16 +82,46 @@ int main(int argc, char **argv){
alpha_max = alpha_min;
alpha_min = init_alpha*((double)(save_times-i-1)/save_times);
if (alpha_min < init_alpha*0.0001) alpha_min = init_alpha*0.0001;
ice->Train(sub_sample_times, negative_samples, alpha_max, alpha_min, threads);
ice->Train(sub_sample_times, negative_samples, alpha_max, alpha_min, threads, ice->vocab_count, "stage1");

if (i==(save_times-1))
{
ice->SaveWeights(rep_file);
ice->SaveVertexWeights(text_rep_file);
ice->SaveContextWeights(text_context_file);
}
else
{
string sub_rep_file = rep_file + string(".") + to_string(current_sample_times);
ice->SaveWeights(sub_rep_file);
string sub_text_rep_file = text_rep_file + string(".") + to_string(current_sample_times);
ice->SaveVertexWeights(sub_text_rep_file);
string sub_text_context_file = text_context_file + string(".") + to_string(current_sample_times);
ice->SaveContextWeights(sub_text_context_file);

}
}

ice->LoadEdgeList(entity_file);
ice->Init(dimensions);
ice->LoadWeights(text_context_file);

sub_sample_times = sample_times/save_times, current_sample_times=0;
alpha_max=init_alpha, alpha_min=init_alpha;
for (int i=0; i<save_times; ++i)
{
current_sample_times += sub_sample_times;
alpha_max = alpha_min;
alpha_min = init_alpha*((double)(save_times-i-1)/save_times);
if (alpha_min < init_alpha*0.0001) alpha_min = init_alpha*0.0001;
ice->Train(sub_sample_times, negative_samples, alpha_max, alpha_min, threads, ice->vocab_count, "stage2");

if (i==(save_times-1))
{
ice->SaveEntityWeights(result_file);
}
else
{
string sub_result_file = result_file + string(".") + to_string(current_sample_times);
ice->SaveEntityWeights(sub_result_file);

}
}

Expand Down
Loading