code: def graphcuts_sosvm(
      num_train_samples=10, len_label=5, len_feat=20, num_test_samples=5
  ):
      """Graph cuts as approximate inference in structured output SVM framework.
  
      Args:
          num_train_samples: number of training samples
          len_label: number of classes, i.e., size of label space
          len_feat: the dimension of the feature vector
          num_test_samples: number of testing samples
      """
      import time
  
      # generate synthetic dataset
      (labels_train, feats_train) = generate_data(
          num_train_samples, len_label, len_feat
      )
  
      # compute full-connected edge table
      full = np.vstack(
          [x for x in itertools.combinations(range(len_label), 2)]
      )
  
      # define factor types
      factor_types = define_factor_types(len_label, len_feat, full)
  
      # create features and labels for factor graph mode
      (labels_fg, feats_fg) = build_factor_graph_model(
          labels_train, feats_train, factor_types, full, sg.GRAPH_CUT
      )
  
      # create model and register factor types
      model = sg.create_structured_model(
          "FactorGraphModel",
          features=feats_fg,
          labels=labels_fg,
          inf_type="GRAPH_CUT",
      )
  
      for i in range(len(factor_types)):
          model.add("factor_types", factor_types[i])
  
      # Training
      # the 3rd parameter is do_weighted_averaging, by turning this on,
      # a possibly faster convergence rate may be achieved.
      # the 4th parameter controls outputs of verbose training information
      sgd = sg.create_machine(
          "StochasticSOSVM",
          model=model,
          labels=labels_fg,
          do_weighted_averaging=True,
          num_iter=150,
          m_lambda=0.0001,
      )
  
      # train
      t0 = time.time()
      sgd.train()
      t1 = time.time()
      w_sgd = sgd.get("w")
      # print "SGD took", t1 - t0, "seconds."
  
      # training error
      labels_pr = sgd.apply()
      # ave_loss = evaluation(labels_pr, labels_fg, model)
      # print('SGD: Average training error is %.4f' % ave_loss)
  
      # testing error
      # generate synthetic testing dataset
      (labels_test, feats_test) = generate_data(
          num_test_samples, len_label, len_feat
      )
      # create features and labels for factor graph mode
      (labels_fg_test, feats_fg_test) = build_factor_graph_model(
          labels_test, feats_test, factor_types, full, sg.GRAPH_CUT
      )
      # set features and labels to sgd
      sgd.get("model").put("features", feats_fg_test)
      sgd.get("model").put("labels", labels_fg_test)
      # test
      labels_pr = sgd.apply()
  
file_path: shogun/examples/undocumented/python/structure_graphcuts.py
imports: import numpy
  import itertools
  import shogun
label_desc: Use image node_id 5 for calling a global function with example usage: graphcuts_sosvm(num_train_samples, len_label, len_feat, num_test_samples) without return types