Skip to content

Commit af4039a

Browse files
committed
Fixed memory allocation bugs: no space for biases
Memory was allocated without space for biases. As the result of this some gradients were overwritten by weights, and some costs were overwritten by gradients. You can see below that word vector positions inside W are calculated with additional space for bias, for example "l1 = (cr.word1 - 1LL) * (vector_size + 1)". Thus the memory allocation must also use additional space. With this fix applied the algorithm tends to perform slightly better on provided analogy tests.
1 parent db5a4d1 commit af4039a

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/glove.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ void initialize_parameters() {
6464
vector_size++; // Temporarily increment to allocate space for bias
6565

6666
/* Allocate space for word vectors and context word vectors, and correspodning gradsq */
67-
a = posix_memalign((void **)&W, 128, 2 * vocab_size * vector_size * sizeof(real)); // Might perform better than malloc
67+
a = posix_memalign((void **)&W, 128, 2 * vocab_size * (vector_size + 1) * sizeof(real)); // Might perform better than malloc
6868
if (W == NULL) {
6969
fprintf(stderr, "Error allocating memory for W\n");
7070
exit(1);
7171
}
72-
a = posix_memalign((void **)&gradsq, 128, 2 * vocab_size * vector_size * sizeof(real)); // Might perform better than malloc
72+
a = posix_memalign((void **)&gradsq, 128, 2 * vocab_size * (vector_size + 1) * sizeof(real)); // Might perform better than malloc
7373
if (gradsq == NULL) {
7474
fprintf(stderr, "Error allocating memory for gradsq\n");
7575
exit(1);

0 commit comments

Comments
 (0)