Exemplo n.º 1
0
    def init_args(self, training_dataset, test_dataset, datapoints,
                  feature_map, multiclass_extension=None):

        if training_dataset is None:
            raise AlgorithmError('Training dataset must be provided')

        is_multiclass = get_num_classes(training_dataset) > 2
        if is_multiclass:
            if multiclass_extension is None:
                raise AlgorithmError('Dataset has more than two classes. A multiclass extension must be provided.')
        else:
            if multiclass_extension is not None:
                logger.warning("Dataset has just two classes. Supplied multiclass extension will be ignored")

        if multiclass_extension is None:
            qsvm_instance = QSVM_Kernel_Binary()
        else:
            qsvm_instance = QSVM_Kernel_Multiclass(multiclass_extension)

        if datapoints is not None:
            if not isinstance(datapoints, np.ndarray):
                datapoints = np.asarray(datapoints)

        qsvm_instance.init_args(training_dataset, test_dataset, datapoints, feature_map, self)
        self.instance = qsvm_instance
Exemplo n.º 2
0
    def init_params(self, params, algo_input):

        if algo_input.training_dataset is None:
            raise AlgorithmError("Training dataset is required.")
        fea_map_params = params.get(QuantumAlgorithm.SECTION_KEY_FEATURE_MAP)
        feature_map = get_feature_map_instance(fea_map_params['name'])
        num_qubits = get_feature_dimension(algo_input.training_dataset)
        fea_map_params['num_qubits'] = num_qubits
        feature_map.init_params(fea_map_params)

        is_multiclass = get_num_classes(algo_input.training_dataset) > 2

        if is_multiclass:
            multicls_ext_params = params.get(
                QuantumAlgorithm.SECTION_KEY_MULTICLASS_EXTENSION)
            multiclass_extension = get_multiclass_extension_instance(
                multicls_ext_params['name'])
            # we need to set this explicitly for quantum version
            multicls_ext_params['params'] = [feature_map, self]
            multicls_ext_params['estimator_cls'] = QKernalSVM_Estimator
            multiclass_extension.init_params(multicls_ext_params)
            logger.info("Multiclass classifcation algo:" +
                        multicls_ext_params['name'])
        else:
            logger.warning(
                "Only two classes in the dataset, use binary classifer"
                " and ignore all options of multiclass_extension")
            multiclass_extension = None

        self.init_args(algo_input.training_dataset, algo_input.test_dataset,
                       algo_input.datapoints, feature_map,
                       multiclass_extension)
Exemplo n.º 3
0
    def __init__(self,
                 feature_map,
                 training_dataset,
                 test_dataset=None,
                 datapoints=None,
                 multiclass_extension=None):
        """Constructor.

        Args:
            feature_map (FeatureMap): feature map module, used to transform data
            training_dataset (dict): training dataset.
            test_dataset (dict): testing dataset.
            datapoints (numpy.ndarray): prediction dataset.
            multiclass_extension (MultiExtension): if number of classes > 2, a multiclass scheme is
                                                    is needed.

        Raises:
            ValueError: if training_dataset is None
            AquaError: use binary classifer for classes > 3
        """
        super().__init__()
        if training_dataset is None:
            raise ValueError('Training dataset must be provided')

        is_multiclass = get_num_classes(training_dataset) > 2
        if is_multiclass:
            if multiclass_extension is None:
                raise AquaError('Dataset has more than two classes. '
                                'A multiclass extension must be provided.')
        else:
            if multiclass_extension is not None:
                logger.warning("Dataset has just two classes. "
                               "Supplied multiclass extension will be ignored")

        self.training_dataset, self.class_to_label = split_dataset_to_data_and_labels(
            training_dataset)
        if test_dataset is not None:
            self.test_dataset = split_dataset_to_data_and_labels(
                test_dataset, self.class_to_label)
        else:
            self.test_dataset = None

        self.label_to_class = {
            label: class_name
            for class_name, label in self.class_to_label.items()
        }
        self.num_classes = len(list(self.class_to_label.keys()))
        self.datapoints = datapoints

        self.feature_map = feature_map
        self.num_qubits = self.feature_map.num_qubits

        if multiclass_extension is None:
            qsvm_instance = _QSVM_Kernel_Binary(self)
        else:
            qsvm_instance = _QSVM_Kernel_Multiclass(self, multiclass_extension)

        self.instance = qsvm_instance
Exemplo n.º 4
0
    def __init__(self,
                 feature_map,
                 training_dataset,
                 test_dataset=None,
                 datapoints=None,
                 multiclass_extension=None):
        """Constructor.

        Args:
            feature_map (FeatureMap): feature map module, used to transform data
            training_dataset (dict): training dataset.
            test_dataset (dict): testing dataset.
            datapoints (numpy.ndarray): prediction dataset.
            multiclass_extension (MultiExtension): if number of classes > 2, a multiclass scheme is
                                                    is needed.

        Raises:
            ValueError: if training_dataset is None
            AquaError: use binary classifer for classes > 3
        """
        super().__init__()
        if training_dataset is None:
            raise ValueError('Training dataset must be provided')

        is_multiclass = get_num_classes(training_dataset) > 2
        if is_multiclass:
            if multiclass_extension is None:
                raise AquaError(
                    'Dataset has more than two classes. A multiclass extension must be provided.'
                )
        else:
            if multiclass_extension is not None:
                logger.warning(
                    "Dataset has just two classes. Supplied multiclass extension will be ignored"
                )

        if multiclass_extension is None:
            qsvm_instance = _QSVM_Kernel_Binary(feature_map, self,
                                                training_dataset, test_dataset,
                                                datapoints)
        else:
            qsvm_instance = _QSVM_Kernel_Multiclass(feature_map, self,
                                                    training_dataset,
                                                    test_dataset, datapoints,
                                                    multiclass_extension)

        self.instance = qsvm_instance