⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svm_struct_api.c

📁 一款不错的支持向量机程序
💻 C
📖 第 1 页 / 共 5 页
字号:
  PyObject_SetAttrString(pSm, "w", pValue);  Py_DECREF(pValue);  pArray = PyObject_GetAttrString(pSm, "alpha");  if (pArray) {    pValue = PySequence_Tuple(pArray);    Py_DECREF(pArray);    PyObject_SetAttrString(pSm, "alpha", pValue);    Py_DECREF(pValue);  }  pArray = PyObject_GetAttrString(pSm, "index");  if (pArray && pArray != Py_None) {    pValue = PySequence_Tuple(pArray);    Py_DECREF(pArray);    PyObject_SetAttrString(pSm, "index", pValue);    Py_DECREF(pValue);  }    if (PyObject_HasAttrString(pSm, "cobj"))    PyObject_DelAttrString(pSm, "cobj");  /* Set up the call to the Python method. */  pDict = PyModule_GetDict(pModule);  pFunc = PyDict_GetItemString(pDict, PYTHON_WRITE_MODEL);  if (pFunc == NULL) {    /*fprintf(stderr, "Warning: could not find function %s!\n",      PYTHON_WRITE_MODEL);*/    /* Do the default behavior of using pickle to dump the structmodel       to a file. */    module = PyImport_ImportModule("pickle");    if (module == NULL) {      fprintf(stderr, "The cPickle package is unavailable!!\n");      Py_Exit(1);    }    pFunc = PyDict_GetItemString(PyModule_GetDict(module), "dump");    /* Open the file for writing. */    pFile = PyFile_FromString(file, "w");    pArgs = Py_BuildValue("(OOi)", pSm, pFile, -1);    /* Dump it! */    PYTHON_CALL(pValue, pFunc, pArgs);    if (pValue == NULL) {      fprintf(stderr, "cPickle.dump failed to write structmodel!\n");      Py_Exit(1);    }    Py_DECREF(pValue);    Py_DECREF(pArgs);    Py_DECREF(pFile);    Py_DECREF(module);    return;  }  /* Build the argument list. */  pArgs = PyTuple_New(3);  PyTuple_SetItem(pArgs, 0, PyString_FromString(file));  PyTuple_SetItem(pArgs, 1, pSm);  PyTuple_SetItem(pArgs, 2, sparmToPythonObject(sparm));  /* Call the embedded python function!! */  PYTHON_CALL(pValue, pFunc, pArgs);  Py_DECREF(pArgs);  Py_DECREF(pValue);}STRUCTMODEL read_struct_model(char *file, STRUCT_LEARN_PARM *sparm){  /* Reads structural model sm from file file. This function is used     only in the prediction module, not in the learning module. */  /* Writes structural model sm to file file. */  PyObject *pDict, *pFunc, *pArgs, *pValue, *pFile, *module, *pSm;  STRUCTMODEL sm;  int i, n;  /* Do some minimal initialization of sparm. */  sparm->py_sparm = NULL;  sparm->custom_argc = 0;  /* Set up the call to the Python method. */  pDict = PyModule_GetDict(pModule);  pFunc = PyDict_GetItemString(pDict, PYTHON_READ_MODEL);  if (pFunc == NULL) {    /*fprintf(stderr, "Warning: could not find function %s!\n",      PYTHON_READ_MODEL);*/    /* Do the default behavior of using pickle to load the structmodel       from a file. */    module = PyImport_ImportModule("pickle");    if (module == NULL) {      fprintf(stderr, "The pickle package is unavailable!!\n");      Py_Exit(1);    }    pFunc = PyDict_GetItemString(PyModule_GetDict(module), "load");    /* Open the file for reading. */    pFile = PyFile_FromString(file, "r");    pArgs = Py_BuildValue("(O)", pFile);    /* Load it! */    PYTHON_CALL(pSm, pFunc, pArgs);    if (pSm == NULL) {      fprintf(stderr, "pickle.load failed to read structmodel!\n");      Py_Exit(1);    }    Py_DECREF(pFile);    Py_DECREF(module);  } else {    /* Build the argument list. */    pArgs = PyTuple_New(2);    PyTuple_SetItem(pArgs, 0, PyString_FromString(file));    PyTuple_SetItem(pArgs, 1, sparmToPythonObject(sparm));    /* Call the embedded python function!! */    PYTHON_CALL(pSm, pFunc, pArgs);    if (pSm == Py_None) {      fprintf(stderr, "%s returned None, indicating a read error!\n",	      PYTHON_READ_MODEL);      Py_Exit(1);    }  }  Py_DECREF(pArgs);  /* By whatever method, either the user function or default behavior,     we now apparently have a Python struct model object.  Put the     relevant fields from the returned value in the C structmodel. */  sm.py_sm = pSm;  sm.dirty = 1;  sm.svm_model = (MODEL*)my_malloc(sizeof(MODEL));  /* Set scalar parameters. */  assert(pythonGetI(pSm, "size_psi", &sm.sizePsi));  /* Set scalar parameters for the model. */  assert(pythonGetI(pSm, "sv_num", &sm.svm_model->sv_num));  assert(pythonGetF(pSm, "b", &sm.svm_model->b));  assert(pythonGetI(pSm, "totwords", &sm.svm_model->totwords));  assert(pythonGetI(pSm, "totdoc", &sm.svm_model->totdoc));  assert(pythonGetF(pSm, "loo_error", &sm.svm_model->loo_error));  assert(pythonGetF(pSm, "loo_recall", &sm.svm_model->loo_recall));  assert(pythonGetF(pSm, "loo_precision", &sm.svm_model->loo_precision));  assert(pythonGetF(pSm, "xa_error", &sm.svm_model->xa_error));  assert(pythonGetF(pSm, "xa_recall", &sm.svm_model->xa_recall));  assert(pythonGetF(pSm, "xa_precision", &sm.svm_model->xa_precision));  assert(pythonGetF(pSm, "maxdiff", &sm.svm_model->maxdiff));  assert(pythonGetI(pSm, "at_upper_bound", &sm.svm_model->at_upper_bound));  /* Set scalar parameters from the kernel parameter. */  assert(pythonGetI(pSm,"kernel_type",&sm.svm_model->kernel_parm.kernel_type));  assert(pythonGetI(pSm,"poly_degree",&sm.svm_model->kernel_parm.poly_degree));  assert(pythonGetF(pSm, "rbf_gamma", &sm.svm_model->kernel_parm.rbf_gamma));  assert(pythonGetF(pSm, "coef_lin", &sm.svm_model->kernel_parm.coef_lin));  assert(pythonGetF(pSm, "coef_const", &sm.svm_model->kernel_parm.coef_const));  assert(pythonGetS(pSm, "custom", sm.svm_model->kernel_parm.custom,		    sizeof(sm.svm_model->kernel_parm.custom)));  /* Copy over the friggin' w array! */  pValue = PyObject_GetAttrString(pSm, "w");  if (pValue == NULL) {    fprintf(stderr, "No w attribute for sm read from %s!", file);    Py_Exit(1);  }  sm.svm_model->lin_weights = sm.w =    (double*)my_malloc((sm.sizePsi+1)*sizeof(double));  n = PySequence_Size(pValue);  if (n > sm.sizePsi) n=sm.sizePsi;  for (i=0+pStartingIndex; i<n+pStartingIndex; ++i) {    PyObject *pPsiValue = PySequence_GetItem(pValue, i);    sm.w[i+1-pStartingIndex] = PyFloat_AsDouble(pPsiValue);    Py_DECREF(pPsiValue);  }  Py_DECREF(pValue);  /* Copy over the support vectors. */  assert(pValue = PyObject_GetAttrString(pSm, "supvec"));  sm.svm_model->supvec = (DOC**)my_malloc(sm.svm_model->sv_num*sizeof(DOC*));  n = PySequence_Size(pValue)+1;  if (n > sm.svm_model->sv_num) n=sm.svm_model->sv_num;  if (n > 0)    sm.svm_model->supvec[0] = NULL;  for (i=1; i<n; ++i) {    PyObject *pSv = PySequence_GetItem(pValue, i-1);    DOC *sv = pythonObjectToDoc(pSv);    Py_DECREF(pSv);    sm.svm_model->supvec[i] = sv;  }  Py_DECREF(pValue);  /* Copy over the alpha array. */  assert(pValue = PyObject_GetAttrString(pSm, "alpha"));  n = PySequence_Size(pValue);  sm.svm_model->alpha = (double*)my_malloc(n*sizeof(double));  for (i=0; i<n; ++i) {    PyObject *pItem = PySequence_GetItem(pValue, i);    sm.svm_model->alpha[i] = PyFloat_AsDouble(pItem);    Py_DECREF(pItem);  }  Py_DECREF(pValue);  /* Copy over the index array. */  assert(pValue = PyObject_GetAttrString(pSm, "index"));  if (pValue == Py_None) {    sm.svm_model->index = NULL;  } else {    n = PySequence_Size(pValue);    sm.svm_model->index = (long*)my_malloc(n*sizeof(long));    for (i=0; i<n; ++i) {      PyObject *pItem = PySequence_GetItem(pValue, i);      sm.svm_model->index[i] = PyInt_AsLong(pItem);      Py_DECREF(pItem);    }    Py_DECREF(pValue);  }  /* That'll do, pig.  That'll do. */  return sm;}int pythonDummyClose(FILE*f) {  /* This function does nothing, since SVM^struct handles the opening     and closing of the file by itself very well, thank you.  It is     only used because we need to pass an already open FILE into     Python, and the function requires a function. */  return 0;}void        write_label(FILE *fp, LABEL y){  /* Writes label y to file handle fp. */  PyObject *pDict, *pFunc, *pArgs, *pValue;  /* Set up the call to the Python method. */  pDict = PyModule_GetDict(pModule);  pFunc = PyDict_GetItemString(pDict, PYTHON_WRITE_LABEL);  if (pFunc == NULL) {    /*fprintf(stderr, "Warning: could not find function %s!\n",      PYTHON_WRITE_LABEL);*/    /* Perform default behavior of simply outputting the label to the file. */    PyObject_Print((PyObject*)y.py_label, fp, 0);    fprintf(fp, "\n");    return;  }  /* Build the argument list. */  Py_INCREF((PyObject*)y.py_label);  pArgs = PyTuple_New(2);  PyTuple_SetItem(pArgs, 0, PyFile_FromFile(fp,"foobar","w",pythonDummyClose));  PyTuple_SetItem(pArgs, 1, (PyObject*)y.py_label);  /* Call the embedded python function!! */  PYTHON_CALL(pValue, pFunc, pArgs);  Py_DECREF(pArgs);  Py_DECREF(pValue);}void        free_pattern(PATTERN x) {  /* Frees the memory of x. */  Py_DECREF((PyObject*)x.py_pattern);}void        free_label(LABEL y) {  /* Frees the memory of y. */  Py_DECREF((PyObject*)y.py_label);}void        free_struct_model(STRUCTMODEL sm) {  /* Frees the memory of model. */  /* if(sm.w) free(sm.w); */ /* this is free'd in free_model */  if(sm.svm_model) free_model(sm.svm_model,1);  /* add free calls for user defined data here */  Py_DECREF((PyObject*)sm.py_sm);}void        free_struct_sample(SAMPLE s){  /* Frees the memory of sample s. */  int i;  for(i=0;i<s.n;i++) {     free_pattern(s.examples[i].x);    free_label(s.examples[i].y);  }  free(s.examples);}void        print_struct_help(){  /* Prints a help text that is appended to the common help text of     svm_struct_learn. */  /* Writes label y to file handle fp. */  PyObject *pDict, *pFunc, *pArgs, *pValue;  /* Set up the call to the Python method. */  pDict = PyModule_GetDict(pModule);  pFunc = PyDict_GetItemString(pDict, PYTHON_PRINT_HELP);  if (pFunc == NULL) {    /*fprintf(stderr, "Warning: could not find function %s!\n",      PYTHON_PRINT_HELP);*/    /* Perform default behavior of simply outputting SVM^struct's       regular help string. */    printf      ("         --* string  -> custom parameters that can be adapted for\n"       "                        struct learning. The * can be replaced by\n"       "                        any string and there can be multiple\n"       "                        options starting with --.\n"       "\n"       "         --m module  -> for the Python code, use the module named\n"       "                        'module'.  In most settings, the module\n"       "                        is stored in the Python file module.py.\n"       "                        If omitted, the default is to load the\n"       "                        module %s.", PYTHON_MODULE_NAME       );    return;  }  /* Build the argument list. */  pArgs = PyTuple_New(0);  /* Call the embedded python function!! */  PYTHON_CALL(pValue, pFunc, pArgs);  Py_DECREF(pArgs);  Py_DECREF(pValue);  }void         parse_struct_parameters(STRUCT_LEARN_PARM *sparm) {  PyObject *pDict, *pFunc, *pArgs, *pValue;  char *moduleName = PYTHON_MODULE_NAME;  int i;  /* Detect whether we should use an alternate module name. */  for(i=0;(i<sparm->custom_argc)&&((sparm->custom_argv[i])[0] == '-');i++){    if (!strcmp("--m", sparm->custom_argv[i])) {      moduleName = sparm->custom_argv[++i];      break;    }  }  /* Load the module. */  api_load_module(moduleName);  /* Set up the call the Python function parse_parameters in the module. */  sparm->py_sparm = NULL;  pDict = PyModule_GetDict(pModule);  pFunc = PyDict_GetItemString(pDict, PYTHON_PARSE_ARGUMENTS);  if (pFunc == NULL) {    fprintf(stderr, "Warning: could not find function %s!\n",	    PYTHON_PARSE_ARGUMENTS);    return;  }  pArgs = PyTuple_New(1); /* This is a new instance! */  PyTuple_SetItem(pArgs, 0, sparmToPythonObject(sparm));  /* Call the embedded python function!! */  PYTHON_CALL(pValue, pFunc, pArgs);  Py_DECREF(pArgs);  Py_DECREF(pValue);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -