pub fn predict(
        &self,
        inputs: &[&str],
        labels: &[&str],
        template: Option<Box<dyn Fn(&str) -> String>>,
        max_length: usize,
    ) -> 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.softmax(1, Float).select(-1, -1);
        let label_indices = scores.as_ref().argmax(-1, true).squeeze1(1);
        let scores = scores
            .gather(1, &label_indices.unsqueeze(-1), false)
            .squeeze1(1);
        let label_indices = label_indices.iter::<i64>().unwrap().collect::<Vec<i64>>();
        let scores = scores.iter::<f64>().unwrap().collect::<Vec<f64>>();

        let mut output_labels: Vec<Label> = vec![];
        for sentence_idx in 0..label_indices.len() {
            let label_string = labels[label_indices[sentence_idx] as usize].to_string();
            let label = Label {
                text: label_string,
                score: scores[sentence_idx],
                id: label_indices[sentence_idx],
                sentence: sentence_idx,
            };
            output_labels.push(label)
        }
        output_labels
    }
/// Zero shot classification with 1 (and exactly 1) 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 {}."`.
/// * `max_length` -`usize` Maximum sequence length for the inputs. If needed, the input sequence will be truncated before the label template.
///
/// # Returns
///
/// * `Vec<Label>` containing with the most likely label for each input sentence.
///
/// # 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 prime minister has announced a stimulus package which was widely criticized by the opposition.";
/// let candidate_labels = &["politics", "public health", "economics", "sports"];
///
/// let output = sequence_classification_model.predict(
///     &[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.959,
///         id: 0,
///         sentence: 0,
///     },
///     Label {
///         text: "economy".to_string(),
///         score: 0.642,
///         id: 2,
///         sentence: 1,
///     },
/// ]
/// .to_vec();
/// ```
Zero shot classification with 1 (and exactly 1) true label.
Zero shot classification with 1 ( and exactly 1 ) true label .
pub fn predict(
        &self,
        inputs: &[&str],
        labels: &[&str],
        template: Option<Box<dyn Fn(&str) -> String>>,
        max_length: usize,
    ) -> 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.softmax(1, Float).select(-1, -1);
        let label_indices = scores.as_ref().argmax(-1, true).squeeze1(1);
        let scores = scores
            .gather(1, &label_indices.unsqueeze(-1), false)
            .squeeze1(1);
        let label_indices = label_indices.iter::<i64>().unwrap().collect::<Vec<i64>>();
        let scores = scores.iter::<f64>().unwrap().collect::<Vec<f64>>();
        let mut output_labels: Vec<Label> = vec![];
        for sentence_idx in 0..label_indices.len() {
            let label_string = labels[label_indices[sentence_idx] as usize].to_string();
            let label = Label {
                text: label_string,
                score: scores[sentence_idx],
                id: label_indices[sentence_idx],
                sentence: sentence_idx,
            };
            output_labels.push(label)
        }
        output_labels
    }
pub fn predict ( & self , inputs : & [ & str ] , labels : & [ & str ] , template : Option < Box < dyn Fn ( & str ) -> String > > , max_length : usize , ) -> 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 . softmax ( 1 , Float ) . select ( - 1 , - 1 ) ; let label_indices = scores . as_ref ( ) . argmax ( - 1 , true ) . squeeze1 ( 1 ) ; let scores = scores . gather ( 1 , & label_indices . unsqueeze ( - 1 ) , false ) . squeeze1 ( 1 ) ; let label_indices = label_indices . iter :: < i64 > ( ) . unwrap ( ) . collect :: < Vec < i64 > > ( ) ; let scores = scores . iter :: < f64 > ( ) . unwrap ( ) . collect :: < Vec < f64 > > ( ) ; let mut output_labels : Vec < Label > = vec ! [ ] ; for sentence_idx in 0 .. label_indices . len ( ) { let label_string = labels [ label_indices [ sentence_idx ] as usize ] . to_string ( ) ; let label = Label { text : label_string , score : scores [ sentence_idx ] , id : label_indices [ sentence_idx ] , sentence : sentence_idx , } ; output_labels . push ( label ) } output_labels }
Zero shot classification with 1 (and exactly 1) true label.
Zero shot classification with 1 ( and exactly 1 ) true label .
&self
inputs
&[&str]
labels
&[&str]
template
Option<Box<dyn Fn(&str) -> String>>
max_length
usize
`Vec` containing with the most likely label for each input sentence.
` Vec ` containing with the most likely label for each input sentence .
&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 prime minister has announced a stimulus package which was widely criticized by the opposition.";
let candidate_labels = &["politics", "public health", "economics", "sports"].

let output = sequence_classification_model.predict(
&[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 prime minister has announced a stimulus package which was widely criticized by the opposition . " ; let candidate_labels = & [ " politics " " public health " " economics " " sports " ] . let output = sequence_classification_model . predict ( & [ input_sentence input_sequence_2 ] candidate_labels None 128 ) .
ok(())
None
}
None
use rust_bert::pipelines::sequence_classification::label;
None