69 #define EXP_A (1048576 / M_LN2) 71 #define FAST_EXP(y) (d2i.n.i = EXP_A * (y) + (1072693248 - EXP_C), d2i.d) 200 return rand() / (
real)RAND_MAX * (max - min) + min;
218 if (ch == 13)
continue;
220 if ((ch ==
' ') || (ch ==
'\t') || (ch ==
'\n')) {
222 if (ch ==
'\n') ungetc(ch, fin);
227 strcpy(word, const_cast<char *>(
"</s>"));
246 unsigned int hash, a;
249 for (a = 0; a < strlen(word); a++) {
250 hash = hash * 237 + word[a];
267 if (!strcmp(word,
vocab[a].word)) {
321 if (layerc_size > 0) {
368 printf(
"Memory allocation failed\n");
374 printf(
"Memory allocation failed\n");
383 printf(
"Memory allocation for direct" 384 " connections failed (requested %lld bytes)\n",
410 printf(
"Memory allocation failed\n");
441 if (layerc_size > 0) {
476 (
bptt + bptt_block + 1) * layer1_size,
sizeof(
neuron)));
484 printf(
"Memory allocation failed\n");
504 df +=
vocab[
i].
cn /
static_cast<double>(b);
506 if (df > (a + 1) /
static_cast<double>(
class_size)) {
508 if (a < class_size - 1) a++;
518 dd += sqrt(
vocab[i].cn / static_cast<double>(b));
521 df += sqrt(
vocab[i].cn / static_cast<double>(b)) / dd;
523 if (df > (a + 1) / static_cast<double>(class_size)) {
525 if (a < class_size - 1) a++;
535 class_words =
reinterpret_cast<int **
>(calloc(class_size,
sizeof(
int *)));
536 class_cn =
reinterpret_cast<int *
>(calloc(class_size,
sizeof(
int)));
537 class_max_cn =
reinterpret_cast<int *
>(calloc(class_size,
sizeof(
int)));
560 while (ch != delim) {
563 printf(
"Unexpected end of file\n");
571 int a, b, ver, unused_size;
578 printf(
"ERROR: model file '%s' not found!\n",
rnnlm_file);
583 unused_size = fscanf(fi,
"%d", &ver);
584 if ((ver == 4) && (
version == 5)) {
588 printf(
"Unknown version of file %s\n",
rnnlm_file);
593 unused_size = fscanf(fi,
"%d", &
filetype);
598 unused_size = fscanf(fi,
"%s", str);
603 unused_size = fscanf(fi,
"%lf", &
llogp);
605 unused_size = fscanf(fi,
"%d", &
iter);
609 unused_size = fscanf(fi,
"%lf", &
logp);
611 unused_size = fscanf(fi,
"%d", &
anti_k);
631 unused_size = fscanf(fi,
"%d", &
bptt);
647 unused_size = fscanf(fi,
"%lf", &d);
651 unused_size = fscanf(fi,
"%lf", &d);
654 unused_size = fscanf(fi,
"%lf", &d);
671 unused_size = fscanf(fi,
"%d%d", &b, &
vocab[a].cn);
673 unused_size = fscanf(fi,
"%d", &
vocab[a].class_index);
682 unused_size = fscanf(fi,
"%lf", &d);
689 unused_size = fread(&fl, 4, 1, fi);
697 unused_size = fscanf(fi,
"%lf", &d);
705 unused_size = fread(&fl, 4, 1, fi);
715 unused_size = fscanf(fi,
"%lf", &d);
722 unused_size = fscanf(fi,
"%lf", &d);
731 unused_size = fscanf(fi,
"%lf", &d);
741 unused_size = fread(&fl, 4, 1, fi);
748 unused_size = fread(&fl, 4, 1, fi);
755 unused_size = fread(&fl, 4, 1, fi);
765 unused_size = fscanf(fi,
"%lf", &d);
772 unused_size = fread(&fl, 4, 1, fi);
802 for (a =
bptt + bptt_block - 1; a > 1; a--) {
816 struct synapse *srcmatrix,
int matrix_width,
817 int from,
int to,
int from2,
int to2,
int type) {
819 real val1, val2, val3, val4;
820 real val5, val6, val7, val8;
823 for (b = 0; b < (to - from) / 8; b++) {
834 for (a = from2; a < to2; a++) {
835 val1 += srcvec[a].
ac * srcmatrix[a + (b * 8 + from + 0) * matrix_width].weight;
836 val2 += srcvec[a].
ac * srcmatrix[a + (b * 8 + from + 1) * matrix_width].weight;
837 val3 += srcvec[a].
ac * srcmatrix[a + (b * 8 + from + 2) * matrix_width].weight;
838 val4 += srcvec[a].
ac * srcmatrix[a + (b * 8 + from + 3) * matrix_width].weight;
840 val5 += srcvec[a].
ac * srcmatrix[a + (b * 8 + from + 4) * matrix_width].weight;
841 val6 += srcvec[a].
ac * srcmatrix[a + (b * 8 + from + 5) * matrix_width].weight;
842 val7 += srcvec[a].
ac * srcmatrix[a + (b * 8 + from + 6) * matrix_width].weight;
843 val8 += srcvec[a].
ac * srcmatrix[a + (b * 8 + from + 7) * matrix_width].weight;
845 dest[b * 8 + from + 0].
ac += val1;
846 dest[b * 8 + from + 1].
ac += val2;
847 dest[b * 8 + from + 2].
ac += val3;
848 dest[b * 8 + from + 3].
ac += val4;
850 dest[b * 8 + from + 4].
ac += val5;
851 dest[b * 8 + from + 5].
ac += val6;
852 dest[b * 8 + from + 6].
ac += val7;
853 dest[b * 8 + from + 7].
ac += val8;
856 for (b = b * 8; b < to - from; b++) {
857 for (a = from2; a < to2; a++) {
859 srcvec[a].
ac * srcmatrix[a + (b + from) * matrix_width].weight;
863 for (a = 0; a < (to2 - from2) / 8; a++) {
874 for (b = from; b < to; b++) {
875 val1 += srcvec[b].
er * srcmatrix[a * 8 + from2 + 0 + b * matrix_width].
weight;
876 val2 += srcvec[b].
er * srcmatrix[a * 8 + from2 + 1 + b * matrix_width].
weight;
877 val3 += srcvec[b].
er * srcmatrix[a * 8 + from2 + 2 + b * matrix_width].
weight;
878 val4 += srcvec[b].
er * srcmatrix[a * 8 + from2 + 3 + b * matrix_width].
weight;
880 val5 += srcvec[b].
er * srcmatrix[a * 8 + from2 + 4 + b * matrix_width].
weight;
881 val6 += srcvec[b].
er * srcmatrix[a * 8 + from2 + 5 + b * matrix_width].
weight;
882 val7 += srcvec[b].
er * srcmatrix[a * 8 + from2 + 6 + b * matrix_width].
weight;
883 val8 += srcvec[b].
er * srcmatrix[a * 8 + from2 + 7 + b * matrix_width].
weight;
885 dest[a * 8 + from2 + 0].
er += val1;
886 dest[a * 8 + from2 + 1].
er += val2;
887 dest[a * 8 + from2 + 2].
er += val3;
888 dest[a * 8 + from2 + 3].
er += val4;
890 dest[a * 8 + from2 + 4].
er += val5;
891 dest[a * 8 + from2 + 5].
er += val6;
892 dest[a * 8 + from2 + 6].
er += val7;
893 dest[a * 8 + from2 + 7].
er += val8;
896 for (a = a * 8; a < to2 - from2; a++) {
897 for (b = from; b < to; b++) {
899 += srcvec[b].
er * srcmatrix[a + from2 + b * matrix_width].
weight;
904 for (a = from2; a < to2; a++) {
935 if (last_word != -1)
neu0[last_word].
ac = 1;
961 if (layerc_size > 0) {
963 0, layerc_size, 0, layer1_size, 0);
978 if (layerc_size > 0) {
997 if (a > 0)
if (
history[a - 1] == -1)
break;
1001 for (b = 1; b <= a; b++) {
1002 hash[a] += PRIMES[(a * PRIMES[b] + b) %
PRIMES_SIZE]
1003 * static_cast<unsigned long long>(
history[b - 1] + 1);
1040 if (
gen > 0)
return;
1050 if (layerc_size > 0) {
1054 +
class_cn[vocab[word].class_index],
1060 +
class_cn[vocab[word].class_index],
1075 if (a > 0)
if (
history[a - 1] == -1)
break;
1080 for (b = 1; b <= a; b++) {
1082 * static_cast<unsigned long long>(
history[b - 1] + 1);
1111 for (c = 0; c <
class_cn[vocab[word].class_index]; c++) {
1133 assert(context_out != NULL);
1141 std::string current_word,
1142 const std::vector < std::string > &history_words,
1143 const std::vector < float > &context_in,
1144 std::vector < float > *context_out) {
1151 std::vector <std::string> history_words_nounk(history_words);
1152 std::string current_word_nounk = current_word;
1153 if (
isUnk(current_word_nounk)) {
1156 for (
int i = 0;
i < history_words_nounk.size(); ++
i) {
1157 if (
isUnk(history_words_nounk[
i])) {
1168 history_words_nounk[history_words_nounk.size() - 1 -
i].c_str());
1171 int word = 0, last_word = 0;
1173 if (current_word_nounk ==
unk_sym) {
1177 if (history_words_nounk.size() > 0) {
1179 history_words_nounk[history_words_nounk.size() - 1].c_str());
1190 if (context_out != NULL) {
1194 if (last_word != -1) {
1213 unordered_map <std::string, float>::const_iterator
iter =
1216 return iter->second;
1221 if (filename.empty())
1224 for (; !unk_reader.
Done(); unk_reader.
Next()) {
1225 std::string key = unk_reader.
Key();
1226 float prob = unk_reader.
Value();
int history[MAX_NGRAM_ORDER]
int getWordHash(const char *word)
void setUnkSym(const std::string &unk)
void copyHiddenLayerToInput()
unordered_map< std::string, float > unk_penalty
void restoreContextFromVector(const std::vector< float > &context_in)
const int MAX_NGRAM_ORDER
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
void goToDelimiter(int delim, FILE *fi)
float computeConditionalLogprob(std::string current_word, const std::vector< std::string > &history_words, const std::vector< float > &context_in, std::vector< float > *context_out)
float getUnkPenalty(const std::string &word)
struct synapse * bptt_syn0
char test_file[MAX_FILENAME_STRING]
char valid_file[MAX_FILENAME_STRING]
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
bool isUnk(const std::string &word)
static union rnnlm::@11 d2i
struct vocab_word * vocab
real random(real min, real max)
void setRnnLMFile(const std::string &str)
void setRandSeed(int newSeed)
void setUnkPenalty(const std::string &filename)
int searchVocab(const char *word)
void matrixXvector(struct neuron *dest, struct neuron *srcvec, struct synapse *srcmatrix, int matrix_width, int from, int to, int from2, int to2, int type)
void saveContextToVector(std::vector< float > *context_out)
const unsigned int PRIMES_SIZE
char train_file[MAX_FILENAME_STRING]
const unsigned int PRIMES[]
void computeNet(int last_word, int word)
void readWord(char *word, FILE *fin)
char rnnlm_file[MAX_FILENAME_STRING]