/* 函数名称:CreateBinaryTree(BiTree *T) 说明:创建二叉树,以输入'#'作为空指针标志,按先序遍历的顺序输入二叉树中各个结点 */ void CreateBinaryTree(BiTree *T) { char ch; scanf("%c",&ch); if(ch=='#') *T=NULL; else { *T=(BiTree)malloc(sizeof(BiTNode)); if(!(*T))//容错判断,申请内存空间失败时返回 return; (*T)->data=ch; CreateBinaryTree(&((*T)->lchild)); if((*T)->lchild)//当存在左孩子时将标志位定义为Link //if(((*T)->lchild)!=NULL) (*T)->LTag=Link; CreateBinaryTree(&((*T)->rchild));//当存在右孩子时将标志位定义为Link if((*T)->rchild) (*T)->RTag=Link; } }
void Test4(){ //情况1:最长路径就在根节点所在的树中 int arr1[] = {1,2,3,4,0,0,0,5,0,0,6,7,0,8,0,0,9}; //情况2:最长路径在根节点所在的子树中 int arr2[] = {1,2,3,4,0,0,5,0,6}; Node_p root1 = CreateBinaryTree(arr1,sizeof(arr1)/sizeof(*arr1),0); Node_p root2 = CreateBinaryTree(arr2,sizeof(arr2)/sizeof(*arr2),0); cout<<GetMaxDistance(root1)<<endl; //6 cout<<GetMaxDistance(root2)<<endl; //4 PrevOrder(root1); InOrder(root1); PostOrder(root1); GetTreeMirror(root1); cout<<endl; PrevOrder(root1); InOrder(root1); PostOrder(root1); cout<<endl; PrevOrder(root2); InOrder(root2); PostOrder(root2); GetTreeMirror(root2); cout<<endl; PrevOrder(root2); InOrder(root2); PostOrder(root2); }
void Test3(){ int arr1[] = {1,2,4,'#','#',7,'#','#',3,5,'#','#',6,'#',8}; //非完全二叉树 int arr2[] = {1,2,4,0,0,5,0,0,3,0,0}; //完全二叉树 int arr3[] = {1,2,4,0,0,5,0,0,3,0,6}; //非完全二叉树 Node_p root1 = CreateBinaryTree(arr1,sizeof(arr1)/sizeof(*arr1),'#'); Node_p root2 = CreateBinaryTree(arr2,sizeof(arr2)/sizeof(*arr2),0); Node_p root3 = CreateBinaryTree(arr3,sizeof(arr3)/sizeof(*arr3),0); cout<<IsCompletlyTree(root1)<<endl; cout<<IsCompletlyTree(root2)<<endl; cout<<IsCompletlyTree(root3)<<endl; }
Node* CreateBinaryTree(int *arr, int start, int end) { if (start > end) return NULL; int mid = (start + end) >> 1; Node *n = new Node(arr[mid], NULL, NULL); n->left = CreateBinaryTree(arr, start, mid - 1); n->right = CreateBinaryTree(arr, mid + 1, end); return n; }
void InitNet() { long long a, b; a = posix_memalign((void **)&nnet.syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (nnet.syn0 == NULL) {fprintf(stderr, "Memory allocation failed (syn0)\n"); exit(1);} a = posix_memalign((void **)&nnet.syn1, 128, (long long)layer1_size * vocab_size * sizeof(real)); if (nnet.syn1 == NULL) {fprintf(stderr, "Memory allocation failed (syn1)\n"); exit(1);} a = posix_memalign((void **)&nnet.synRec, 128, (long long)layer1_size * layer1_size * sizeof(real)); if (nnet.synRec == NULL) {fprintf(stderr, "Memory allocation failed (synRec)\n"); exit(1);} if(maxent_hash_size != 0) { a = posix_memalign((void **)&nnet.synMaxent, 128, (long long)maxent_hash_size * sizeof(real)); if (nnet.synMaxent == NULL) {fprintf(stderr, "Memory allocation failed (synMaxent)\n"); exit(1);} memset(nnet.synMaxent, 0, (long long) maxent_hash_size * sizeof(real)); } else { maxent_order = 0; } for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) nnet.syn0[a * layer1_size + b] = (rand() / (real)RAND_MAX - 0.5) / layer1_size; for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) nnet.syn1[a * layer1_size + b] = 0; for (a = 0; a < layer1_size; a++) for (b = 0; b < layer1_size; b++) nnet.synRec[a * layer1_size + b] = (rand() / (real)RAND_MAX - 0.5) / layer1_size; CreateBinaryTree(); }
int main(int argc, char *argv[]) { BiTree T,H; printf("请创建一棵二叉树(如:'ABDH##I##EJ###CF##G##')\n"); CreateBinaryTree(&T); printf("\n二叉树的深度为:%d,结点数目为:%d\n",BinaryDepth(T),NodeCount(T)); printf("\n先序遍历的结果是:\n"); PreOrderTraverse(T); printf("\n中序遍历的结果是:\n"); InOrderTraverse(T); printf("\n后序遍历的结果是:\n"); PostOrderTraverse(T); printf("\n对二叉树进行中序线索化\n"); InOrderThreading(&H,T); printf("\n中序遍历线索二叉树的结果是:\n"); InOrderTraverseThreadTree(H); printf("\n摧毁一棵二叉树\n"); DestoryBinaryTree(&T); printf("\n二叉树的深度为:%d,结点数目为:%d\n",BinaryDepth(T),NodeCount(T)); return 0; }
// 初始化网络结构 void InitNet() { long long a, b; unsigned long long next_random = 1; // 分配词的向量内存,地址是128的倍数 a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn0 == NULL) {printf("Memory allocation failed\n"); exit(1);} if (hs) { // 分配huffman内部节点内存 a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);} // 初始化为0向量 for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1[a * layer1_size + b] = 0; } if (negative>0) { // 分配负样本词的向量空间 a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);} // 初始化为0向量 for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1neg[a * layer1_size + b] = 0; } for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn0[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size; } CreateBinaryTree(); }
NodeTree* CreateBinaryTree() { NodeTree* p; char* data=(char*)malloc(sizeof(char)*100); scanf("%s",data); if (strcmp(data,"*")==0) { return NULL; } else { p=CreateNodeTree(atoi(data)); p->left=CreateBinaryTree(); p->right=CreateBinaryTree(); } return p; }
int main() { INTVEC conf = parseInputFile(); /* for(INTVEC_CITER iter = conf.begin(); iter != conf.end(); ++iter) { std::cout << *iter << " "; } std::cout << std::endl; */ std::cout << "Create BinaryTree!" << std::endl; pNode pRoot = CreateBinaryTree(conf, 0, conf.size()); std::cout << "Print BinaryTree!" << std::endl; printBinaryTree(pRoot, 0); for(std::map<int, INTVEC>::const_iterator mciter = m_tmp.begin(); mciter != m_tmp.end(); ++mciter) { for(INTVEC_CITER iter = mciter->second.begin(); iter != mciter->second.end(); ++iter) { std::cout << *iter << " "; } std::cout << std::endl; } return 0; }
void main() { BinaryTree *tree = CreateBinaryTree(); ElementTree item; item.data = 30; InsertTree(tree, item); item.data = 5; InsertTree(tree, item); item.data = 40; InsertTree(tree, item); item.data = 2; InsertTree(tree, item); item.data = 35; InsertTree(tree, item); item.data = 80; InsertTree(tree, item); Preorder(tree->root); printf("\n"); item.data = 80; DeleteTreeNode(tree->root, item); Preorder(tree->root); }
void InitNet() { long long a, b; unsigned long long next_random = 1; a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn0 == NULL) {printf("Memory allocation failed\n"); exit(1);} if (hs) { a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1[a * layer1_size + b] = 0; } if (negative>0) { a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++){ //next_random = next_random * (unsigned long long)25214903917 + 11; //syn1neg[a * layer1_size + b] = fabs((((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size); //if (syn1neg[a * layer1_size + b] < 1e-6) syn1neg[a * layer1_size + b] = 0; syn1neg[a * layer1_size + b] = 0; } } for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn0[a * layer1_size + b] = fabs((((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size); } a = posix_memalign((void **)&rateTable, 128, (long long)vocab_size * sizeof(real)); for (a = 0; a < vocab_size; a++) rateTable[a] = alpha; CreateBinaryTree(); }
// 网络结构初始化 void InitNet() { // intialize the neural network structure long long a, b; // posix_memalign() 成功时会返回size字节的动态内存,并且这块内存的地址是alignment(这里是128)的倍数 // syn0 存储的是word vectors a = posix_memlign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn0 == NULL) { printf("Memory allocation failed\n"); exit(1); } // Hierarchical Softmax if (hs) { // hs中,用syn1 a = posix_memlign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);} for (b = 0; b < layer1_size; b++) for (a = 0; a < vocab_size; a++) syn1[a * layer1_size + b] = 0; } // Negative Sampling if (negative > 0) { // ns中,用syn1neg a = posix_memlign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1neg == NULL) {printf("Memory allocaiton failed\n"); exit(1);} for (b = 0; b < layer1_size; b++) for (a = 0; a < vocab_size; a++) syn1neg[a * layer1_size + b] = 0; } for (b = 0; b < layer1_size; b++) for (a = 0; a < vocab_size; a++) syn0[a * layer1_size + b] = (rand() / (real)RAND_MAX - 0.5) / layer1_size;//随机初始化word vectors CreateBinaryTree();// 创建霍夫曼树 }
void InitNet() { long long a, b; a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn0 == NULL) { printf("Memory allocation failed\n"); exit(1); } if (hs) { a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1 == NULL) { printf("Memory allocation failed\n"); exit(1); } for (b = 0; b < layer1_size; b++) for (a = 0; a < vocab_size; a++) syn1[a * layer1_size + b] = 0; } if (negative>0) { a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1neg == NULL) { printf("Memory allocation failed\n"); exit(1); } for (b = 0; b < layer1_size; b++) for (a = 0; a < vocab_size; a++) syn1neg[a * layer1_size + b] = 0; } for (b = 0; b < layer1_size; b++) for (a = 0; a < vocab_size; a++) syn0[a * layer1_size + b] = (rand() / (real)RAND_MAX - 0.5) / layer1_size; CreateBinaryTree(); }
int main() { NodeTree* root=CreateBinaryTree(); // NodeList* FirstFromList=GetListFromTree(root); // TraverseList(FirstFromList); // root=GetTreeFromList(FirstFromList); PrettyPrint(root,0); return 0; }
status CreateBinaryTree(Node* T) //二叉树的建立 前序算法 { ElemType d; scanf("%d",&d); T->data = d; if(T->data == 0000) //虚构的孩子 { T = NULL; return TRUE; } T->lChild = (Node*)malloc(sizeof(Node)); //左孩子指针域 T->rChild = (Node*)malloc(sizeof(Node)); //右孩子指针域 CreateBinaryTree(T->lChild); CreateBinaryTree(T->rChild); return TRUE; }
int _tmain(int argc, _TCHAR* argv[]) { int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Node *tree = CreateBinaryTree(arr, 0, 9); while (tree != NULL) { printf("%d \n", tree->value); tree = tree->left; } return 0; }
void InitNet() { long long a, b; unsigned long long next_random = 1; a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn0 == NULL) {printf("Memory allocation failed\n"); exit(1);} if (hs) { if (global_image) { a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * (layer1_size + layer1_image_size) * sizeof(real)); if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size + layer1_image_size; b++) syn1[a * (layer1_size + layer1_image_size) + b] = 0; } else { a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1[a * layer1_size + b] = 0; } } if (negative>0) { if (global_image) { a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * (layer1_size + layer1_image_size) * sizeof(real)); if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < (layer1_size + layer1_image_size); b++) syn1neg[a * (layer1_size + layer1_image_size) + b] = 0; } else { a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1neg[a * layer1_size + b] = 0; } } // Initialize syn0 for words and sentences in vocab for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn0[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size; } // Initialize image vector from trained CNN if(global_image) { a = posix_memalign((void **)&syn0_im, 128, (long long)image_size * layer1_image_size * sizeof(real)); if (syn0_im == NULL) {printf("Memory allocation failed\n"); exit(1);} ReadCNNFeature(); printf("Finished reading CNN feature\n"); } CreateBinaryTree(); }
TEST(beEqualOrGreaterThanX, test3) { std::vector<int> pre = {5,5,0,0,0,5,9,9,9}; std::vector<int> in = {0,0,0,5,5,5,9,9,9}; BinaryTree<int> bt = CreateBinaryTree(pre, in); std::vector<int> result; result = EGthanX(bt, 5); assert_eq_vector({9,9,9,5,5,5}, result); result = EGthanX(bt, 0); assert_eq_vector({9,9,9,5,5,5,0,0,0}, result); result = EGthanX(bt, 101); assert_eq_vector({}, result); result = EGthanX(bt, 9); assert_eq_vector({9,9,9}, result); }
TEST(beEqualOrGreaterThanX, test1) { std::vector<int> pre = {4,2,1,3,5,6}; std::vector<int> in = {1,2,3,4,5,6}; BinaryTree<int> bt = CreateBinaryTree(pre, in); std::vector<int> result; result = EGthanX(bt, 3); assert_eq_vector({6,5,4,3}, result); result = EGthanX(bt, 1); assert_eq_vector({6,5,4,3,2,1}, result); result = EGthanX(bt, 7); assert_eq_vector({}, result); result = EGthanX(bt, -10); assert_eq_vector({6,5,4,3,2,1}, result); }
void InitNet() { long long a, b; unsigned long long next_random = 1; // a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); #ifdef _WIN32 syn0 = (real *)_aligned_malloc((long long)vocab_size * layer1_size * sizeof(real), 128); #else a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); #endif if (syn0 == NULL) {Rprintf("Memory allocation failed\n"); exit(1);} if (hs) { // a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); #ifdef _WIN32 syn1 = (real *)_aligned_malloc((long long)vocab_size * layer1_size * sizeof(real), 128); #else a = posix_memalign((void **)&(syn1), 128, (long long)vocab_size * layer1_size * sizeof(real)); #endif if (syn1 == NULL) {Rprintf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1[a * layer1_size + b] = 0; } if (negative>0) { // a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real)); #ifdef _WIN32 syn1neg = (real *)_aligned_malloc((long long)vocab_size * layer1_size * sizeof(real), 128); #else a = posix_memalign((void **)&(syn1neg), 128, (long long)vocab_size * layer1_size * sizeof(real)); #endif if (syn1neg == NULL) {Rprintf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1neg[a * layer1_size + b] = 0; } for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn0[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size; } CreateBinaryTree(); }
void Test1(){ int arr[] = {1,2,4,'#','#',7,'#','#',3,5,'#','#',6,'#',8}; Node_p root = CreateBinaryTree(arr,sizeof(arr)/sizeof(*arr),'#'); cout<<"前序遍历:"<<endl; PrevOrder(root); PrevOrderNonR(root); cout<<endl; cout<<"中序遍历:"<<endl; InOrder(root); InOrderNonR(root); cout<<endl; cout<<"后序遍历:"<<endl; PostOrder(root); PostOrderNonR(root); cout<<endl; cout<<"层序遍历:"<<endl; LevelOrder(root); cout<<endl; int depth = Depth(root); cout<<"该树的高度为:"<<depth<<endl; cout<<"该树共有"<<LeafNum(root)<<"个叶子节点"<<endl; for(int i=1; i<=depth; ++i){ cout<<"该树第"<<i<<"层有"<<GetKNode(root,i)<<"个节点"<<endl; } Node_p node1 = FindNode(root,4); Node_p node2 = FindNode(root,7); Node_p ancestor = GetCommonAncestor(root,node1,node2,1); cout<<node1->val<<"和"<<node2->val<<"的公共祖先为:"<<ancestor->val<<endl; node1 = FindNode(root,4); node2 = FindNode(root,5); ancestor = GetCommonAncestor(root,node1,node2,1); cout<<node1->val<<"和"<<node2->val<<"的公共祖先为:"<<ancestor->val<<endl; node1 = FindNode(root,5); node2 = FindNode(root,8); ancestor = GetCommonAncestor(root,node1,node2,1); cout<<node1->val<<"和"<<node2->val<<"的公共祖先为:"<<ancestor->val<<endl; }
void InitNet() { long long a, b; unsigned long long next_random = 1; a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * 2 * sizeof(real)); a = posix_memalign((void **)&syn2, 128, (long long)layer1_size * sizeof(real)); a = posix_memalign((void **)&syn3, 128, (long long)layer1_size * sizeof(real)); if (syn0 == NULL) {printf("Memory allocation failed\n"); exit(1);} if (hs) { a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1[a * layer1_size + b] = 0; } if (negative>0) { a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * 2 * sizeof(real)); if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1neg[a * layer1_size + b] = 0; a = posix_memalign((void **)&syn2neg, 128, (long long)neg_size * layer1_size * sizeof(real)); if (syn2neg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < neg_size; a++) for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn2neg[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / (layer1_size * 2); } } for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size * 2; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn0[a * layer1_size * 2 + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / (layer1_size * 2); } for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn2[b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size; } for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn3[b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size; } CreateBinaryTree(); }
TEST(beEqualOrGreaterThanX, test2) { std::vector<int> pre = { 61, 59, 6, 60, 99, 72,101}; std::vector<int> in = { 6, 59, 60, 61, 72, 99,101}; BinaryTree<int> bt = CreateBinaryTree(pre, in); std::vector<int> result; result = EGthanX(bt, 3); assert_eq_vector({101, 99, 72, 61, 60, 59, 6}, result); result = EGthanX(bt, 61); assert_eq_vector({101, 99, 72, 61}, result); result = EGthanX(bt, 101); assert_eq_vector({101}, result); result = EGthanX(bt, 120); assert_eq_vector({}, result); result = EGthanX(bt, 6); assert_eq_vector({101, 99, 72, 61, 60, 59, 6}, result); }
void Test5(){ int arr[]={5,3,2,1,0,0,0,4,0,0,7,6,0,0,8}; Node_p root = CreateBinaryTree(arr,sizeof(arr)/sizeof(*arr),0); PrevOrder(root); InOrder(root); PostOrder(root); Node_p head = BecomeLinkList(root); Node_p cur = head; while(cur->right!=NULL){ cout<<cur->val<<" "; cur = cur->right; } cout<<cur->val<<endl; while(cur->left!=NULL){ cout<<cur->val<<" "; cur = cur->left; } cout<<cur->val<<endl; }
void InitNet() { long long a, b; a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); //先知道这个也是申请动态数组,对齐还有128这个参数以后再了解 if (syn0 == NULL) { printf("Memory allocation failed\n"); exit(1); } if (hs)//采用softmax { a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1 == NULL) { printf("Memory allocation failed\n"); exit(1); } for (b = 0; b < layer1_size; b++) for (a = 0; a < vocab_size; a++) syn1[a * layer1_size + b] = 0; } if (negative>0)//还有负样本 { a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1neg == NULL) { printf("Memory allocation failed\n"); exit(1); } for (b = 0; b < layer1_size; b++) for (a = 0; a < vocab_size; a++) syn1neg[a * layer1_size + b] = 0; } for (b = 0; b < layer1_size; b++) for (a = 0; a < vocab_size; a++) syn0[a * layer1_size + b] = (rand() / (real)RAND_MAX - 0.5) / layer1_size; CreateBinaryTree();//建立huffman树,对每个单词进行编码 }
//为啥syn1的size跟syn0一样的 void InitNet() { long long a, b; unsigned long long next_random = 1; a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); //给syn0分配内存,syn0对应huffman树叶子节点的词向量 if (syn0 == NULL) {printf("Memory allocation failed\n"); exit(1);} if (hs) { //使用层次softmax a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); //给syn1分配内存,syn1对应huffman树内部节点的词向量 if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1[a * layer1_size + b] = 0; // } if (negative>0) { a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real)); //syn1neg存储negative sampling的参数 if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1neg[a * layer1_size + b] = 0; } for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; //这个初始化是为什么 syn0[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size; } CreateBinaryTree(); }
void InitNet() { long long a, b; unsigned long long next_random = 1; a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn0 == NULL) {printf("Memory allocation failed\n"); exit(1);} a = posix_memalign((void **)&syn_id_neg, 128, (long long)sentence_num * layer1_size * sizeof(real)); if (syn_id_neg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < sentence_num ; a++) for (b = 0; b < layer1_size; b++) syn_id_neg[a * layer1_size + b] = 0; a = posix_memalign((void **)&syn_id, 128, (long long)sentence_num * layer1_size * sizeof(real)); if (syn_id == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < sentence_num ; a++) for (b = 0; b < layer1_size; b++) syn_id[a * layer1_size + b] = 0; if (avg_vectors){ a = posix_memalign((void **)&syn_avg, 128, (long long)sentence_num * layer1_size * sizeof(real)); if (syn_avg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < sentence_num ; a++) for (b = 0; b < layer1_size; b++) syn_avg[a * layer1_size + b] = 0; a = posix_memalign((void **)&syn_avg_neg, 128, (long long)sentence_num * layer1_size * sizeof(real)); if (syn_avg_neg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < sentence_num ; a++) for (b = 0; b < layer1_size; b++) syn_avg_neg[a * layer1_size + b] = 0; a = posix_memalign((void **)&syn_avg_haf, 128, (long long)sentence_num * layer1_size * sizeof(real)); if (syn_avg_haf == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < sentence_num ; a++) for (b = 0; b < layer1_size; b++) syn_avg_haf[a * layer1_size + b] = 0; } if (hs) { a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1[a * layer1_size + b] = 0; } if (negative>0) { a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real)); if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);} for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) syn1neg[a * layer1_size + b] = 0; } for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn0[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size; } for (a = 0; a < sentence_num; a++) for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn_id_neg[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size; } for (a = 0; a < sentence_num; a++) for (b = 0; b < layer1_size; b++) { next_random = next_random * (unsigned long long)25214903917 + 11; syn_id[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size; } CreateBinaryTree(); }
static void ChoosePrize( void ) { if( !ffl.prize_shuffle ) ffl.prize_shuffle = CreateBinaryTree(); else ResetBinaryTree( ffl.prize_shuffle ); // reset down count ffl.current_down = 0; ffl.scoreboard.tick_draw = 0; if( ffl.flags.prize_line_mode ) { uint32_t rand = genrand_int32( ffl.rng ); uint32_t value = ( (uint64_t)ffl.prizes.total_lines * (uint64_t)rand ) / 0xFFFFFFFFU; int n; for( n = 0; n < ffl.prizes.number_lines; n++ ) { if( value < ffl.prizes.lines[n].count ) { int down; for( down = 0; down < ffl.prizes.lines[n].picks; down++ ) { rand = genrand_int32( ffl.rng ); AddBinaryNode( ffl.prize_shuffle, (POINTER)ffl.prizes.lines[n].payouts[down], (uintptr_t)rand ); } for( down = 0; down < 4; down++ ) { if( !down ) ffl.prizes.prize_line.value[down] = (uint32_t)GetLeastNode( ffl.prize_shuffle ); else ffl.prizes.prize_line.value[down] = (uint32_t)GetGreaterNode( ffl.prize_shuffle ); } DeductPrizeLine( n ); break; } else value -= ffl.prizes.lines[n].count; } } else { uint32_t rand; int square; int g = 0; int c = 0; for( square = 0; square < 32; square++ ) { rand = genrand_int32( ffl.rng ); AddBinaryNode( ffl.prize_shuffle, (POINTER)ffl.prizes.grid[g].value, (uintptr_t)rand ); c++; if( c == ffl.prizes.grid[g].count ) { c = 0; g++; } } for( square = 0; square < 32; square++ ) { if( !square ) ffl.prizes.grid_prizes[square].value = (uint32_t)GetLeastNode( ffl.prize_shuffle ); else ffl.prizes.grid_prizes[square].value = (uint32_t)GetGreaterNode( ffl.prize_shuffle ); } } }