pub fn predict_multilabel(
        &self,
        inputs: &[&str],
        labels: &[&str],
        template: Option<Box<dyn Fn(&str) -> String>>,
        max_length: usize,
    ) -> Vec<Vec<Label>> {
        let num_inputs = inputs.len();
        let (input_tensor, mask) = self.prepare_for_model(inputs, labels, template, max_length);
        let output = no_grad(|| {
            let output = self.zero_shot_classifier.forward_t(
                Some(input_tensor),
                Some(mask),
                None,
                None,
                None,
                false,
            );
            output.view((num_inputs as i64, labels.len() as i64, -1i64))
        });
        let scores = output.slice(-1, 0, 3, 2).softmax(-1, Float).select(-1, -1);

        let mut output_labels = vec![];
        for sentence_idx in 0..num_inputs {
            let mut sentence_labels = vec![];
            let sentence_scores = scores
                .select(0, sentence_idx as i64)
                .iter::<f64>()
                .unwrap()
                .collect::<Vec<f64>>();

            for (label_index, score) in sentence_scores.into_iter().enumerate() {
                let label_string = labels[label_index].to_string();
                let label = Label {
                    text: label_string,
                    score,
                    id: label_index as i64,
                    sentence: sentence_idx,
                };
                sentence_labels.push(label);
            }
            output_labels.push(sentence_labels);
        }
        output_labels
    }
/// Zero shot multi-label classification with 0, 1 or no true label.
///
/// # Arguments
///
/// * `input` - `&[&str]` Array of texts to classify.
/// * `labels` - `&[&str]` Possible labels for the inputs.
/// * `template` - `Option<Box<dyn Fn(&str) -> String>>` closure to build label propositions. If None, will default to `"This example is about {}."`.
/// * `max_length` -`usize` Maximum sequence length for the inputs. If needed, the input sequence will be truncated before the label template.
///
/// # Returns
///
/// * `Vec<Vec<Label>>` containing a vector of labels and their probability for each input text
///
/// # Example
///
/// ```no_run
/// # fn main() -> anyhow::Result<()> {
/// use rust_bert::pipelines::zero_shot_classification::ZeroShotClassificationModel;
///
/// let sequence_classification_model = ZeroShotClassificationModel::new(Default::default())?;
///
/// let input_sentence = "Who are you voting for in 2020?";
/// let input_sequence_2 = "The central bank is meeting today to discuss monetary policy.";
/// let candidate_labels = &["politics", "public health", "economics", "sports"];
///
/// let output = sequence_classification_model.predict_multilabel(
///     &[input_sentence, input_sequence_2],
///     candidate_labels,
///     None,
///     128,
/// );
/// # Ok(())
/// # }
/// ```
/// outputs:
/// ```no_run
/// # use rust_bert::pipelines::sequence_classification::Label;
/// let output = [
///     [
///         Label {
///             text: "politics".to_string(),
///             score: 0.972,
///             id: 0,
///             sentence: 0,
///         },
///         Label {
///             text: "public health".to_string(),
///             score: 0.032,
///             id: 1,
///             sentence: 0,
///         },
///         Label {
///             text: "economy".to_string(),
///             score: 0.006,
///             id: 2,
///             sentence: 0,
///         },
///         Label {
///             text: "sports".to_string(),
///             score: 0.004,
///             id: 3,
///             sentence: 0,
///         },
///     ],
///     [
///         Label {
///             text: "politics".to_string(),
///             score: 0.975,
///             id: 0,
///             sentence: 1,
///         },
///         Label {
///             text: "economy".to_string(),
///             score: 0.852,
///             id: 2,
///             sentence: 1,
///         },
///         Label {
///             text: "public health".to_string(),
///             score: 0.0818,
///             id: 1,
///             sentence: 1,
///         },
///         Label {
///             text: "sports".to_string(),
///             score: 0.001,
///             id: 3,
///             sentence: 1,
///         },
///     ],
/// ]
/// .to_vec();
/// ```
Zero shot multi-label classification with 0, 1 or no true label.
Zero shot multi - label classification with 0 1 or no true label .
pub fn predict_multilabel(
        &self,
        inputs: &[&str],
        labels: &[&str],
        template: Option<Box<dyn Fn(&str) -> String>>,
        max_length: usize,
    ) -> Vec<Vec<Label>> {
        let num_inputs = inputs.len();
        let (input_tensor, mask) = self.prepare_for_model(inputs, labels, template, max_length);
        let output = no_grad(|| {
            let output = self.zero_shot_classifier.forward_t(
                Some(input_tensor),
                Some(mask),
                None,
                None,
                None,
                false,
            );
            output.view((num_inputs as i64, labels.len() as i64, -1i64))
        });
        let scores = output.slice(-1, 0, 3, 2).softmax(-1, Float).select(-1, -1);
        let mut output_labels = vec![];
        for sentence_idx in 0..num_inputs {
            let mut sentence_labels = vec![];
            let sentence_scores = scores
                .select(0, sentence_idx as i64)
                .iter::<f64>()
                .unwrap()
                .collect::<Vec<f64>>();
            for (label_index, score) in sentence_scores.into_iter().enumerate() {
                let label_string = labels[label_index].to_string();
                let label = Label {
                    text: label_string,
                    score,
                    id: label_index as i64,
                    sentence: sentence_idx,
                };
                sentence_labels.push(label);
            }
            output_labels.push(sentence_labels);
        }
        output_labels
    }
pub fn predict_multilabel ( & self , inputs : & [ & str ] , labels : & [ & str ] , template : Option < Box < dyn Fn ( & str ) -> String > > , max_length : usize , ) -> Vec < Vec < Label > > { let num_inputs = inputs . len ( ) ; let ( input_tensor , mask ) = self . prepare_for_model ( inputs , labels , template , max_length ) ; let output = no_grad ( | | { let output = self . zero_shot_classifier . forward_t ( Some ( input_tensor ) , Some ( mask ) , None , None , None , false , ) ; output . view ( ( num_inputs as i64 , labels . len ( ) as i64 , - 1i64 ) ) } ) ; let scores = output . slice ( - 1 , 0 , 3 , 2 ) . softmax ( - 1 , Float ) . select ( - 1 , - 1 ) ; let mut output_labels = vec ! [ ] ; for sentence_idx in 0 .. num_inputs { let mut sentence_labels = vec ! [ ] ; let sentence_scores = scores . select ( 0 , sentence_idx as i64 ) . iter :: < f64 > ( ) . unwrap ( ) . collect :: < Vec < f64 > > ( ) ; for ( label_index , score ) in sentence_scores . into_iter ( ) . enumerate ( ) { let label_string = labels [ label_index ] . to_string ( ) ; let label = Label { text : label_string , score , id : label_index as i64 , sentence : sentence_idx , } ; sentence_labels . push ( label ) ; } output_labels . push ( sentence_labels ) ; } output_labels }
Zero shot multi-label classification with 0, 1 or no true label.
Zero shot multi - label classification with 0 1 or no true label .
&self
inputs
&[&str]
labels
&[&str]
template
Option<Box<dyn Fn(&str) -> String>>
max_length
usize
`Vec>` containing a vector of labels and their probability for each input text
` Vec > ` containing a vector of labels and their probability for each input text
&self
inputs
&[&str]
labels
&[&str]
template
Option<Box<dyn Fn(&str) -> String>>
max_length
usize
example
None
fn main() -> anyhow::result<()> {
use rust_bert::pipelines::zero_shot_classification::ZeroShotClassificationModel.

let sequence_classification_model = ZeroShotClassificationModel::new(Default::default())?.

let input_sentence = "Who are you voting for in 2020?";
let input_sequence_2 = "The central bank is meeting today to discuss monetary policy.";
let candidate_labels = &["politics", "public health", "economics", "sports"].

let output = sequence_classification_model.predict_multilabel(
&[input_sentence, input_sequence_2],
candidate_labels,
None,
128,
).
use rust_bert :: pipelines :: zero_shot_classification :: ZeroShotClassificationModel . let sequence_classification_model = ZeroShotClassificationModel :: new ( Default :: default () ) ? . let input_sentence = " Who are you voting for in 2020? " ; let input_sequence_2 = " The central bank is meeting today to discuss monetary policy . " ; let candidate_labels = & [ " politics " " public health " " economics " " sports " ] . let output = sequence_classification_model . predict_multilabel ( & [ input_sentence input_sequence_2 ] candidate_labels None 128 ) .
ok(())
None
}
None
use rust_bert::pipelines::sequence_classification::label;
None