text:
#!/usr/bin/python
# -*- coding: utf-8 -*-

# The MIT License (MIT)
# Copyright (c) 2018 Daniel Koguciuk <daniel.koguciuk@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

'''
@author: Daniel Koguciuk <daniel.koguciuk@gmail.com>
@note: Created on 23.03.2018
'''

import os
import sys
import cv2
import time
import argparse
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn import decomposition
from sklearn.svm import LinearSVC
from sklearn.manifold import TSNE
from utils import data_generator as gen
from utils.models import MLPClassifier

def _HOG(images):
    """
    Calc HOG features for grayscale images.

    Args:
        images (ndarray of size [images, 32, 32]): Grayscale images.

    Returns:
        (ndarray of size [images, features_no]): HOG features for each image.
    """

    WIN_SIZE = (32, 32)
    BLOCK_SIZE = (8, 8)
    BLOCK_STRIDE = (4, 4)
    CELL_SIZE = (4, 4)
    NBINS = 9

    hog_desriptor = cv2.HOGDescriptor(WIN_SIZE, BLOCK_SIZE, BLOCK_STRIDE, CELL_SIZE, NBINS)
    hog_features = [np.squeeze(hog_desriptor.compute(images[idx])) for idx in range(images.shape[0])]
    return np.stack(hog_features, axis=0)

def images_visualization(instances=10, scale=2.0):
    """
    Visualize some random images from the dataset.

    Args:
        instances (int): How many random images per class should I show?
        scale (float): How many times should I multiply canvas image?
    """

    BORDER = 1
    IMAGE_SIZE = 32
    CHANNELS = 3

    generator = gen.CifarImages('BGR')
    images = generator.get_random_images(train=True, instance_number=instances)
    canvas = np.ones(shape=(instances * 32 + (instances + 1) * BORDER,
                            instances * 32 + (instances + 1) * BORDER, CHANNELS), dtype=np.uint8) * 255
    for class_idx in range(len(images)):
        row = class_idx * IMAGE_SIZE + (class_idx + 1) * BORDER
        for instance_idx in range(instances):
            col = instance_idx * IMAGE_SIZE + (instance_idx + 1) * BORDER
            canvas[row:row + IMAGE_SIZE, col:col + IMAGE_SIZE, :] = images[class_idx][instance_idx]

    canvas = cv2.resize(src=canvas, dsize=(0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_LANCZOS4)
    cv2.imshow('examples', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()

def extract_HOG(generator, train, verbose=True):
    """
    Extract HOG features for specified dataset (train/test).

    Args:
        generator (Generator class object): Generator class object.
        train (bool): Am I working with train or test data?
        verbose (bool): Should I print some additional info?

    Returns:
        (ndarray of size [images, features_no], ndarray of size [images]) Features and labels.
    """
    samples = generator.size(train=train)
    for images, labels in generator.generate_batch(train=train, batch_size=samples):
        start_time = time.time()
        features = _HOG(images)
        hog_time = time.time()
        if verbose:
            print "Features calculated in ", hog_time - start_time, " seconds"
        return features, labels

def decompose(features, labels):
    """
    Decompose features with some dimensionality reduction techniques.

    Args:
        features (ndarray of size [images, features_no]): Features of the dataset.
        labels (ndarray of size [images]): Labels of the dataset.
    """

    # PCA decomposition
    start_time = time.time()
    pca = decomposition.PCA(n_components=2)
    pca.fit(features)
    features_pca = pca.transform(features)
    pca_time = time.time()
    print "PCA features calculated in ", pca_time - start_time, " seconds with variance ", pca.explained_variance_ratio_

    # t-SNE decomposition
    elems = 5000
    tsne = TSNE(n_components=2, verbose=True, perplexity=40, n_iter=300)
    features_tsne = tsne.fit_transform(features[:elems], labels[:elems])
    tsne_time = time.time()
    print "t-SNE features calculated in ", tsne_time - pca_time, " seconds "

    # plots
    plt.figure(figsize=(15, 15))
    plt.scatter(features_pca[:, 0], features_pca[:, 1], c=labels)
    plt.figure(figsize=(15, 15))
    plt.scatter(features_tsne[:, 0], features_tsne[:, 1], c=labels[:elems])
    plt.show()

def hparam_search(features_train, labels_train, features_test, labels_test):
    """
    Search best C param for SVM classifier but first reduce dimension of the features.

    Args:
        features_train (ndarray of size [images, features_no]): Features of the train dataset.
        labels_train (ndarray of size [images]): Labels of the train dataset.
        features_test (ndarray of size [images, features_no]): Features of the test dataset.
        labels_test (ndarray of size [images]): Labels of the test dataset.
    """

    VARIANCE = 0.60
    pca_hparam = decomposition.PCA(VARIANCE)
    pca_hparam.fit(features_train)
    features_hparam_train = pca_hparam.transform(features_train)

    print "Componenst with ", VARIANCE * 100, "% of variance: ", pca_hparam.n_components_
    for C in [0.001, 0.01, 0.1, 1.0, 1.2, 1.5, 2.0, 10.0]:
        classifier_svm = LinearSVC(C=C, verbose=False)
        classifier_svm.fit(features_hparam_train, labels_train)
        print "======= C:", C, "======="
        print "TRAIN SCORE = ", classifier_svm.score(features_hparam_train, labels_train)
        features_hparam_test = pca_hparam.transform(features_test)
        print "TEST  SCORE = ", classifier_svm.score(features_hparam_test, labels_test)

def classify_svm(features_train, labels_train, features_test, labels_test, C, verbose):
    """
    Train SVM classifier and eval train and test scores.

    Args:
        features_train (ndarray of size [images, features_no]): Features of the train dataset.
        labels_train (ndarray of size [images]): Labels of the train dataset.
        features_test (ndarray of size [images, features_no]): Features of the test dataset.
        labels_test (ndarray of size [images]): Labels of the test dataset.
        C (float): C parameter of SVM classifier.
        verbose (bool): Should I print some additional info?
    """
    svm_time_start = time.time()
#    classifier_svm = LinearSVC(C=C, verbose=verbose, dual=False, max_iter=5000)
    classifier_svm = LinearSVC(C=C, verbose=verbose, dual=True, max_iter=1000)
    classifier_svm.fit(features_train, labels_train)
    svm_time_fit = time.time()
    print "SVM fit in ", svm_time_fit - svm_time_start, " seconds\n\n"
    print "TRAIN SCORE = ", classifier_svm.score(features_train, labels_train)
    print "TEST  SCORE = ", classifier_svm.score(features_test, labels_test)

def classify_mlp(features_train, labels_train, features_test, labels_test):

    EPOCHS = 1
    BATCH_SIZE = 200
    CLASSES_COUNT = 10
    LEARNING_RATE = 0.0005

    # Reset
    tf.reset_default_graph()

    # Define model
    with tf.device("/device:GPU:0"):
        model_classifier = MLPClassifier([features_train.shape[-1], 1024, 512, 256, 128, 10], BATCH_SIZE, LEARNING_RATE)

    config = tf.ConfigProto(allow_soft_placement=True)  # , log_device_placement=True)
    with tf.Session(config=config) as sess:

        # Run the initialization
        sess.run(tf.global_variables_initializer())

        # Logs
        log_model_dir = os.path.join("logs", model_classifier.get_model_name())
        writer = tf.summary.FileWriter(os.path.join(log_model_dir, time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())))
#         writer.add_graph(sess.graph)

        # Do the training loop
        global_batch_idx = 1
        for epoch in range(EPOCHS):

            indices = np.arange(features_train.shape[0])
            features_shuffled = features_train.copy()[indices]
            labels_shuffled = labels_train.copy()[indices]
            for index in range(len(features_train)/BATCH_SIZE):
                features = features_shuffled[index*BATCH_SIZE:(index+1)*BATCH_SIZE]
                labels = labels_shuffled[index*BATCH_SIZE:(index+1)*BATCH_SIZE]

                # zero mean
                features = np.clip((features - 0.38) / 10, -1.0, 1.0)

                # run optimizer
                labels_one_hot = sess.run(tf.one_hot(labels, CLASSES_COUNT))
                _, loss, pred, summary = sess.run([model_classifier.optimizer, model_classifier.loss,
                                                   model_classifier.get_classification_prediction(), model_classifier.summary],
                                                   feed_dict={model_classifier.placeholder_embed: features,
                                                              model_classifier.placeholder_label: labels_one_hot})

                # train acc
                acc = float(sum(np.argmax(pred, axis=-1) == labels)) / labels.shape[0]

                # summ
                writer.add_summary(summary, global_batch_idx)
                global_batch_idx += 1

                # Info
                print "Epoch: %06d batch: %03d loss: %06f train acc: %03f" % (epoch + 1, index, loss, acc)
                index += 1

        accs = []
        features_test = np.clip((features_test - 0.38) / 10, -1.0, 1.0)
        for index in range(len(features_test)/BATCH_SIZE):
            features = features_test[index*BATCH_SIZE:(index+1)*BATCH_SIZE]
            labels = labels_test[index*BATCH_SIZE:(index+1)*BATCH_SIZE]
            pred = sess.run(model_classifier.get_classification_prediction(),
                            feed_dict={model_classifier.placeholder_embed: features})
            acc = float(sum(np.argmax(pred, axis=-1) == labels)) / labels.shape[0]
            accs.append(acc)
        print "TEST ACC = ", np.mean(accs)

def main(argv):

    # Parser
    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--features", help="supported image features: hog, cnn", type=str)
    parser.add_argument("-c", "--classifier", help="supported image features: svm, mlp", type=str)
    parser.add_argument("-a", "--augmentation_level", help="if cnn features were chosen, which dataset should I use?  (0, 1 or 2)", type=int)
    parser.add_argument('-i', '--images_vis', help='visualize class images', action='store_true')
    parser.add_argument('-s', '--hparam_search', help='search for best C param for SVM classifier', action='store_true')
    parser.add_argument('-d', '--decompose', help='decompose features with PCA and t-SNE', action='store_true')
    parser.add_argument('-v', '--verbose', help='should I print some additional info?', action='store_true')
    parser.add_argument("-p", "--c_param", help="C param of the SVM classifier", type=float, default=1.0)
    args = vars(parser.parse_args())

    ######################################################################################
    ################################ IMAGES VISUALIZATION ################################
    ######################################################################################

    if args['images_vis']:
        images_visualization()

    ######################################################################################
    ################################## Extract features ##################################
    ######################################################################################

    if args['features'] == 'hog':
        generator = gen.CifarImages('GRAY')
        features_train, labels_train = extract_HOG(generator, train=True, verbose=args['verbose'])
        features_test, labels_test = extract_HOG(generator, train=False, verbose=args['verbose'])
    elif args['features'] == 'cnn':
        if args['augmentation_level'] == None:
            raise ValueError("augmentation_level option should be used here")
            exit()
        generator = gen.CifarCNNFeatures()
        for data in generator.generate_random_batch(train=True, permute=False,
                                                    batch_size=generator.size(args['augmentation_level'], train=True),
                                                    aug_level=args['augmentation_level']):
            features_train = data[0]
            labels_train = data[1]
        for data in generator.generate_random_batch(train=False, permute=False,
                                                    batch_size=generator.size(args['augmentation_level'], train=False),
                                                    aug_level=args['augmentation_level']):
            features_test = data[0]
            labels_test = data[1]
    elif args['features'] == None:
        return
    else:
        raise ValueError("Features not supported..")
        exit()

    ######################################################################################
    ################################## PCA DECOMPOSITION #################################
    ######################################################################################

    if args['decompose']:
        decompose(features_train, labels_train)

    ######################################################################################
    #################################### HPARAM SEARCH ###################################
    ######################################################################################

    if args['hparam_search']:
        hparam_search(features_train, labels_train, features_test, labels_test)

    ######################################################################################
    ################################ FINAL CLASSIFICATION ################################
    ######################################################################################

    if args['classifier'] == 'svm':
        classify_svm(features_train, labels_train, features_test, labels_test, args['c_param'], args['verbose'])
    elif args['classifier'] == 'mlp':
        classify_mlp(features_train, labels_train, features_test, labels_test)

if __name__ == "__main__":
    main(sys.argv[1:])

max_stars_repo_name: dkoguciuk/yacc

max_issues_repo_name: dkoguciuk/yacc

max_forks_repo_name: dkoguciuk/yacc


text:
import json
import os

import numpy as np
import torch

from zerogercrnn.lib.constants import EMPTY_TOKEN_ID, UNKNOWN_TOKEN_ID
from zerogercrnn.experiments.ast_level.utils import read_non_terminals
from zerogercrnn.lib.constants import EMPTY_TOKEN_ID, UNKNOWN_TOKEN_ID, EOF_TOKEN
from zerogercrnn.lib.metrics import Metrics, BaseAccuracyMetrics, IndexedAccuracyMetrics, MaxPredictionAccuracyMetrics, TopKAccuracy

class NonTerminalsMetricsWrapper(Metrics):
    """Metrics that extract non-terminals from target and pass non-terminals tensor to base metrics."""

    def __init__(self, base: Metrics):
        super().__init__()
        self.base = base

    def drop_state(self):
        self.base.drop_state()

    def report(self, prediction_target):
        prediction, target = prediction_target
        self.base.report((prediction, target.non_terminals))

    def get_current_value(self, should_print=False):
        return self.base.get_current_value(should_print)

    def decrease_hits(self, number):
        self.base.decrease_hits(number)

class SingleNonTerminalAccuracyMetrics(Metrics):
    """Metrics that show accuracies per non-terminal. It should not be used for plotting, but to
    print results on console during model evaluation."""

    def __init__(self, non_terminals_file, results_dir=None, group=False, dim=2):
        """

        :param non_terminals_file: file with json of non-terminals
        :param results_dir: where to save json with accuracies per non-terminal
        :param dim: dimension to run max function on for predicted values
        """
        super().__init__()
        print('SingleNonTerminalAccuracyMetrics created!')

        self.non_terminals = read_non_terminals(non_terminals_file)
        self.non_terminals_number = len(self.non_terminals)
        self.results_dir = results_dir
        self.group = group
        self.dim = dim

        self.accuracies = [IndexedAccuracyMetrics(label='ERROR') for _ in self.non_terminals]

    def drop_state(self):
        for accuracy in self.accuracies:
            accuracy.drop_state()

    def report(self, data):
        prediction, target = data
        if self.dim is None:
            predicted = prediction
        else:
            _, predicted = torch.max(prediction, dim=self.dim)
        predicted = predicted.view(-1)
        target = target.non_terminals.view(-1)

        for cur in range(len(self.non_terminals)):
            indices = (target == cur).nonzero().squeeze()
            self.accuracies[cur].report(predicted, target, indices)

    def get_current_value(self, should_print=False):
        result = []
        for cur in range(len(self.non_terminals)):
            cur_accuracy = self.accuracies[cur].get_current_value(should_print=False)
            result.append(cur_accuracy)
            # if should_print:
            #     print('Accuracy on {} is {}'.format(self.non_terminals[cur], cur_accuracy))

        self.save_to_file(result)

        return 0  # this metrics if only for printing

    def save_to_file(self, result):
        if self.results_dir is not None:
            if self.group:
                nt, res = self.get_grouped_result()
            else:
                nt, res = self.non_terminals, result

            with open(os.path.join(self.results_dir, 'nt_acc.txt'), mode='w') as f:
                f.write(json.dumps(nt))
                f.write('\n')
                f.write(json.dumps(res))

    def get_grouped_result(self):
        """Calc accuracies ignoring last two bits of information."""
        nt = set()
        hits = {}
        misses = {}
        for i in range(len(self.non_terminals)):
            base = self.non_terminals[i]
            if self.non_terminals[i] != EOF_TOKEN:
                base = base[:-2]  # remove last two bits

            nt.add(base)

            if base not in hits:
                hits[base] = 0
            if base not in misses:
                misses[base] = 0

            hits[base] += self.accuracies[i].metrics.hits
            misses[base] += self.accuracies[i].metrics.misses

        nt = sorted(list(nt))
        result = []

        nt.remove('Program')
        nt.remove('AssignmentPattern')
        for cur in nt:
            if hits[cur] + misses[cur] == 0:
                result.append(0)
            else:
                result.append(float(hits[cur]) / (hits[cur] + misses[cur]))

        return nt, result

class TerminalAccuracyMetrics(Metrics):
    def __init__(self, dim=2):
        super().__init__()
        self.dim = dim
        self.general_accuracy = BaseAccuracyMetrics()
        self.empty_accuracy = IndexedAccuracyMetrics(
            label='Accuracy on terminals that ground truth is <empty>'
        )
        self.non_empty_accuracy = IndexedAccuracyMetrics(
            label='Accuracy on terminals that ground truth is not <empty>'
        )
        self.ground_not_unk_accuracy = IndexedAccuracyMetrics(
            label='Accuracy on terminals that ground truth is not <unk> (and ground truth is not <empty>)'
        )
        self.model_not_unk_accuracy = IndexedAccuracyMetrics(
            label='Accuracy on terminals that model predicted to non <unk> (and ground truth is not <empty>)'
        )

    def drop_state(self):
        self.general_accuracy.drop_state()
        self.empty_accuracy.drop_state()
        self.non_empty_accuracy.drop_state()
        self.ground_not_unk_accuracy.drop_state()
        self.model_not_unk_accuracy.drop_state()

    def report(self, prediction_target):
        prediction, target = prediction_target
        _, predicted = torch.max(prediction, dim=self.dim)
        predicted = predicted.view(-1)
        target = target.view(-1)

        self.general_accuracy.report((predicted, target))

        if not self.is_train:
            empty_indexes = torch.nonzero(target == 0).squeeze()
            self.empty_accuracy.report(predicted, target, empty_indexes)

            non_empty_indexes = torch.nonzero(target - EMPTY_TOKEN_ID).squeeze()
            self.non_empty_accuracy.report(predicted, target, non_empty_indexes)

            predicted = torch.index_select(predicted, 0, non_empty_indexes)
            target = torch.index_select(target, 0, non_empty_indexes)

            ground_not_unk_indexes = torch.nonzero(target - UNKNOWN_TOKEN_ID).squeeze()
            self.ground_not_unk_accuracy.report(predicted, target, ground_not_unk_indexes)

            model_not_unk_indexes = torch.nonzero(predicted - UNKNOWN_TOKEN_ID).squeeze()
            self.model_not_unk_accuracy.report(predicted, target, model_not_unk_indexes)

    def get_current_value(self, should_print=False):
        general_accuracy = self.general_accuracy.get_current_value(should_print=should_print)
        if (not self.is_train) and should_print:
            self.empty_accuracy.get_current_value(should_print=True)
            self.non_empty_accuracy.get_current_value(should_print=True)
            self.ground_not_unk_accuracy.get_current_value(should_print=True)
            self.model_not_unk_accuracy.get_current_value(should_print=True)
        return general_accuracy

class NonTerminalTerminalAccuracyMetrics(Metrics):

    def __init__(self):
        super().__init__()
        self.nt_accuracy = MaxPredictionAccuracyMetrics()
        self.t_accuracy = MaxPredictionAccuracyMetrics()

    def drop_state(self):
        self.nt_accuracy.drop_state()
        self.t_accuracy.drop_state()

    def report(self, data):
        nt_prediction, t_prediction, nt_target, t_target = data
        self.nt_accuracy.report((nt_prediction, nt_target))
        self.t_accuracy.report((t_prediction, t_target))

    def get_current_value(self, should_print=False):
        nt_value = self.nt_accuracy.get_current_value(should_print=False)
        t_value = self.t_accuracy.get_current_value(should_print=False)

        if should_print:
            print('Non terminals accuracy: {}'.format(nt_value))
            print('Terminals accuracy: {}'.format(t_value))

        return nt_value, t_value

class LayeredNodeDepthsAttentionMetrics(Metrics):
    """Metrics that is able to visualize attention coefficient per node depths"""

    def __init__(self):
        super().__init__()
        self.per_depth_attention_sum = np.zeros((50, 50))
        self.per_depth_reports = np.zeros((50))

    def drop_state(self):
        pass

    def report(self, node_depths, attention_coefficients):
        for i in range(50):
            index = torch.nonzero((node_depths == i))
            if index.size()[0] == 0:
                continue
            selected_attention = torch.index_select(attention_coefficients, dim=0, index=index.squeeze())
            selected_attention = selected_attention.squeeze(2)
            to_report = torch.sum(selected_attention, dim=0).cpu().numpy()
            self.per_depth_attention_sum[i] += to_report
            self.per_depth_reports[i] += index.size()[0]

    def get_current_value(self, should_print=False):
        for i in range(50):
            if abs(self.per_depth_reports[i]) > 1e-6:
                self.per_depth_attention_sum[i] /= self.per_depth_reports[i]
        np.save('eval/temp/attention/per_depth_matrix', self.per_depth_attention_sum)
        return 0  # this metrics is only for saving results to file.

class PerNtAttentionMetrics(Metrics):
    def __init__(self):
        super().__init__()

    def report(self, current_input, attention_coefficients):
        nt_ids = torch.argmax(current_input, dim=-1)

        # for i in range(97): # TODO: check
        #     index = torch.nonzero((nt_ids == i))
        #     if index.size()[0] == 0:
        #         continue
        #     selected_attention = torch.index_select(attention_coefficients, dim=0, index=index.squeeze())
        #     selected_attention = selected_attention.squeeze(2)
        #     to_report = torch.sum(selected_attention, dim=0).cpu().numpy()
        #     self.per_depth_attention_sum[i] += to_report
        #     self.per_depth_reports[i] += index.size()[0]

    def drop_state(self):
        pass

    def get_current_value(self, should_print=False):
        pass

class EmptyNonEmptyWrapper(Metrics):
    def __init__(self, non_emp_base: Metrics, with_emp_base:Metrics):
        super().__init__()
        self.non_emp_base = non_emp_base
        self.with_emp_base = with_emp_base

    def drop_state(self):
        self.non_emp_base.drop_state()
        self.with_emp_base.drop_state()

    def report(self, prediction_target):
        prediction, target = prediction_target
        prediction = prediction.view(-1)
        target = target.view(-1)

        self.with_emp_base.report((prediction, target))

        non_emp_indices = (target != EMPTY_TOKEN_ID).nonzero().squeeze()
        prediction = torch.index_select(prediction, 0, non_emp_indices)
        target = torch.index_select(target, 0, non_emp_indices)
        self.non_emp_base.report((prediction, target))

    def get_current_value(self, should_print=False):
        print('Non Empty')
        self.non_emp_base.get_current_value(should_print=should_print)
        print('With Empty')
        self.with_emp_base.get_current_value(should_print=should_print)

class EmptyNonEmptyTerminalTopKAccuracyWrapper(Metrics):
    def __init__(self):
        super().__init__()
        self.non_emp_base = TopKAccuracy(k=5)
        self.with_emp_base = TopKAccuracy(k=5)

    def drop_state(self):
        self.non_emp_base.drop_state()
        self.with_emp_base.drop_state()

    def report(self, prediction_target):
        prediction, target = prediction_target
        prediction = prediction.view(-1, prediction.size()[-1])
        target = target.view(-1)

        self.with_emp_base.report((prediction, target))

        non_emp_indices = (target != EMPTY_TOKEN_ID).nonzero().squeeze()
        prediction = torch.index_select(prediction, 0, non_emp_indices)
        target = torch.index_select(target, 0, non_emp_indices)
        self.non_emp_base.report((prediction, target))

    def get_current_value(self, should_print=False):
        print('Non Empty')
        self.non_emp_base.get_current_value(should_print=should_print)
        print('With Empty')
        self.with_emp_base.get_current_value(should_print=should_print)

# class AggregatedTerminalTopKMetrics(Metrics):
#
#     def __init__(self, k):
#         super().__init__()
#         self.k = k
#         self.common = BaseAccuracyMetrics()
#         self.target_non_unk = Top
#         self.prediction_non_unk = IndexedAccuracyMetrics('Prediction not unk')
#
#     def drop_state(self):
#         self.common.drop_state()
#         self.target_non_unk.drop_state()
#         self.prediction_non_unk.drop_state()
#
#     def report(self, prediction_target):
#         prediction, target = prediction_target
#         prediction = prediction.view(-1)
#         target = target.view(-1)
#
#         self.common.report((prediction, target))
#
#         pred_non_unk_indices = (prediction != UNKNOWN_TOKEN_ID).nonzero().squeeze()
#         target_non_unk_indices = (target != UNKNOWN_TOKEN_ID).nonzero().squeeze()
#
#         self.prediction_non_unk.report(prediction, target, pred_non_unk_indices)
#         self.target_non_unk.report(prediction, target, target_non_unk_indices)
#
#     def get_current_value(self, should_print=False):
#         print('P(hat(t) == t) = {}'.format(self.common.get_current_value(False)))
#         print('P(hat(t) == t && hat(t) != unk) = {}'.format(self.prediction_non_unk.metrics.hits / (self.common.hits + self.common.misses)))
#         print('P(hat(t) == t | t != unk) = {}'.format(self.target_non_unk.get_current_value(False)))
#         print('P(hat(t) == t | hat(t) != unk) = {}'.format(self.prediction_non_unk.get_current_value(False)))

class AggregatedTerminalMetrics(Metrics):

    def __init__(self):
        super().__init__()
        self.common = BaseAccuracyMetrics()
        self.target_non_unk = IndexedAccuracyMetrics('Target not unk')
        self.prediction_non_unk = IndexedAccuracyMetrics('Prediction not unk')

    def drop_state(self):
        self.common.drop_state()
        self.target_non_unk.drop_state()
        self.prediction_non_unk.drop_state()

    def report(self, prediction_target):
        prediction, target = prediction_target
        prediction = prediction.view(-1)
        target = target.view(-1)

        self.common.report((prediction, target))

        pred_non_unk_indices = (prediction != UNKNOWN_TOKEN_ID).nonzero().squeeze()
        target_non_unk_indices = (target != UNKNOWN_TOKEN_ID).nonzero().squeeze()

        self.prediction_non_unk.report(prediction, target, pred_non_unk_indices)
        self.target_non_unk.report(prediction, target, target_non_unk_indices)

    def get_current_value(self, should_print=False):
        print('P(hat(t) == t) = {}'.format(self.common.get_current_value(False)))
        print('P(hat(t) == t && hat(t) != unk) = {}'.format(self.prediction_non_unk.metrics.hits / (self.common.hits + self.common.misses)))
        print('P(hat(t) == t | t != unk) = {}'.format(self.target_non_unk.get_current_value(False)))
        print('P(hat(t) == t | hat(t) != unk) = {}'.format(self.prediction_non_unk.get_current_value(False)))

ext: py

max_stars_repo_name: zerogerc/rnn-autocomplete

max_issues_repo_name: ZeRoGerc/rnn-autocomplete

max_forks_repo_name: ZeRoGerc/rnn-autocomplete

reason: import numpy


text:
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Recurrent neural network (RNN) cells."""
import dataclasses

import jax
import jax.numpy as jnp
from jax.tree_util import register_pytree_node

__all__ = [
    'LinearRNN', 'RNNCell', 'StackedRNN', 'GRU', 'LSTM', 'VanillaRNN', 'UGRNN',
    'embedding'
]

# Aliases for standard initializers and nonlinearities.
fan_in = jax.nn.initializers.variance_scaling(1., 'fan_in', 'normal')
zeros = jax.nn.initializers.zeros

@dataclasses.dataclass
class LinearRNN:
  """Dataclass for storing parameters of a Linear RNN."""
  A: jnp.array  # Input weights.      pylint: disable=invalid-name
  W: jnp.array  # Recurrent weights.  pylint: disable=invalid-name
  b: jnp.array  # Bias.

  def apply(self, x, h) -> jnp.array:
    """Linear RNN Update."""
    return self.A @ x + self.W @ h + self.b

  def flatten(self):
    return (self.A, self.W, self.b)

# Register the LinearRNN dataclass as a pytree, so that we can directly
# pass it to other jax functions (optimizers, flatten, etc.)
register_pytree_node(LinearRNN, lambda node: (node.flatten(), None),
                     lambda _, children: LinearRNN(*children))

class RNNCell:
  """Base class for all RNN Cells.

  An RNNCell must implement the following methods:
    init(PRNGKey, input_shape) -> output_shape, rnn_params
    apply(params, inputs, state) -> next_state
  """

  def __init__(self, num_units, h_init=zeros):
    """Initializes an RNNCell."""
    self.num_units = num_units
    self.h_init = h_init

    # Compute RNN Jacobians.
    self.inp_jac = jax.jacobian(self.apply, argnums=1)
    self.inp_jac.__doc__ = 'Computes the Jacobian of the RNN cell with respect to it\'s inputs.'  # pylint: disable=line-too-long

    self.rec_jac = jax.jacobian(self.apply, argnums=2)
    self.rec_jac.__doc__ = 'Computes the Jacobian of the RNN cell with respect to it\'s own hidden state.'  # pylint: disable=line-too-long

    # Generate cell update function that works on batches of inputs.
    self.batch_apply = jax.vmap(self.apply, in_axes=(None, 0, 0))
    self.batch_apply.__doc__ = 'Applies the RNN cell update to a batch of inputs and states.'  # pylint: disable=line-too-long

  def init(self, key, input_shape):
    raise NotImplementedError

  def apply(self, params, inputs, state):
    raise NotImplementedError

  def init_initial_state(self, key):
    return self.h_init(key, self.num_units)

  def get_initial_state(self, params, batch_size=None):
    """Gets initial RNN states.

    Args:
      params: rnn_parameters
      batch_size: batch size of initial states to create.

    Returns:
      An ndarray with shape (batch size, num_units).
    """
    if batch_size is None:
      return params['initial_state']
    else:
      initial_state = jnp.expand_dims(params['initial_state'], axis=0)
      return jnp.repeat(initial_state, batch_size, axis=0)

class StackedRNN(RNNCell):
  """Stacks multiple RNN cells together.

  A stacked RNN cell is specified by a list of RNN cells and
  (optional) stax.Dense layers in between them.

  Note that the full hidden state for this cell is the concatenation
  of hidden states from all of the cells in the stack.
  """

  def __init__(self, layers):
    """Initializes a Stacked RNN Cell.

    Args:
      layers: list of layers, each of which is a (RNNCell, stax.Dense)
        tuple. The stax.Dense functions are used to transform the hidden
        state from the previous RNN in the stack to the inputs to the next
        RNN. If wish to feed the state in directly, use stax.Identity.
    """
    self.layers = layers

    # Total number of units across all cells in the stack.
    self.units = [c.num_units for c, _ in self.layers]
    super().__init__(sum(self.units))

  def init(self, key, input_shape):
    """Initializes parameters of a Stacked RNN."""

    shape = input_shape
    weights = []

    # Initialize each RNN cell and dense layer in the stack.
    for cell, (dense_init, _) in self.layers:
      key, cell_key, dense_key = jax.random.split(key, 3)
      cell_shape, cell_params = cell.init(cell_key, shape)
      shape, dense_params = dense_init(dense_key, cell_shape)
      weights.append((cell_params, dense_params))

    params = {
        'initial_state': super().init_initial_state(key),
        'weights': weights
    }

    return (input_shape[0], self.num_units), params

  def apply(self, params, inputs, state):
    """Applies a single step of a Stacked RNN."""

    # Split the RNN state vector into the state vector for each cell.
    states = jnp.split(state, jnp.cumsum(jnp.array(self.units))[:-1])
    next_states = []

    # Iteratively apply each RNN layer.
    for p, h, layer in zip(params['weights'], states, self.layers):

      # Get parameters and functions for this layer.
      cell_params, dense_params = p
      cell, (_, dense_apply) = layer

      # Apply the RNN and dense layers.
      next_state = cell.apply(cell_params, inputs, h)
      inputs = dense_apply(dense_params, h)

      # Store next state.
      next_states.append(next_state)

    return jnp.concatenate(next_states)

class VanillaRNN(RNNCell):
  """Vanilla RNN Cell."""

  def __init__(self, num_units, w_init=fan_in, b_init=zeros, h_init=zeros):
    """Vanilla RNN Cell.

    Args:
      num_units: int, Number of units in the RNN.
      w_init: Initializer for the recurrent & input weights (Default: fan_in).
      b_init: Initializer for the bias (Default: zeros).
      h_init: Initializer for the RNN initial condition (Default: zeros).
    """
    self.w_init = w_init
    self.b_init = b_init
    self.h_init = h_init
    super().__init__(num_units, h_init)

  def init(self, key, input_shape):
    """Initializes the parameters of a Vanilla RNN."""
    batch_size, num_inputs = input_shape

    rec_shape = (self.num_units, self.num_units)
    inp_shape = (self.num_units, num_inputs)
    bias_shape = (self.num_units,)
    output_shape = (batch_size, self.num_units)

    keys = jax.random.split(key, 4)
    weights = LinearRNN(self.w_init(keys[0], inp_shape),
                        self.w_init(keys[1], rec_shape),
                        self.b_init(keys[2], bias_shape))
    initial_state = super().init_initial_state(keys[3])
    params = {'initial_state': initial_state, 'weights': weights}
    return output_shape, params

  def apply(self, params, inputs, state):
    """Applies a single step of a Vanilla RNN."""
    linear_update = params['weights']
    return jnp.tanh(linear_update.apply(inputs, state))

class UGRNN(RNNCell):
  """Update-gate RNN Cell."""

  def __init__(self,
               num_units,
               gate_bias=0.,
               w_init=fan_in,
               b_init=zeros,
               h_init=zeros):
    """Update-gate RNN (UGRNN) Cell.

    Args:
      num_units: int, Number of units in the RNN.
      gate_bias: float, Bias to add to the update gate (Default: 0.).
      w_init: Initializer for the recurrent & input weights (Default: fan_in).
      b_init: Initializer for the bias (Default: zeros).
      h_init: Initializer for the RNN initial condition (Default: zeros).
    """
    self.gate_bias = gate_bias
    self.w_init = w_init
    self.b_init = b_init
    self.h_init = h_init
    super().__init__(num_units, h_init)

  def init(self, key, input_shape):
    batch_size, num_inputs = input_shape

    rec_shape = (self.num_units, self.num_units)
    inp_shape = (self.num_units, num_inputs)
    bias_shape = (self.num_units,)
    output_shape = (batch_size, self.num_units)

    def _build_linear_rnn(base_key):
      base_key, *keys = jax.random.split(base_key, 4)
      return base_key, LinearRNN(self.w_init(keys[0], inp_shape),
                                 self.w_init(keys[1], rec_shape),
                                 self.b_init(keys[2], bias_shape))

    # build internal gates / linear rnns.
    # we thread the `key` through the helper function `_build_linear_rnn`,
    # which returns a new key that we then pass to the next function call.
    key, update_gate = _build_linear_rnn(key)
    key, cell_state = _build_linear_rnn(key)

    key, key_ic = jax.random.split(key, 2)
    initial_state = super().init_initial_state(key_ic)
    params = {
        'initial_state': initial_state,
        'update_gate': update_gate,
        'cell_state': cell_state
    }
    return output_shape, params

  def apply(self, params, inputs, state):
    update_gate = params['update_gate']
    cell_state = params['cell_state']

    update = jax.nn.sigmoid(update_gate.apply(inputs, state) + self.gate_bias)
    cell = jnp.tanh(cell_state.apply(inputs, state))

    return update * state + (1 - update) * cell

class GRU(RNNCell):
  """Gated recurrent unit."""

  def __init__(self,
               num_units,
               gate_bias=0.,
               w_init=fan_in,
               b_init=zeros,
               h_init=zeros):
    """Gated recurrent unit (GRU) Cell.

    Args:
      num_units: int, Number of units in the RNN.
      gate_bias: float, Bias to add to the update gate (Default: 0.).
      w_init: Initializer for the recurrent & input weights (Default: fan_in).
      b_init: Initializer for the bias (Default: zeros).
      h_init: Initializer for the RNN initial condition (Default: zeros).
    """
    self.gate_bias = gate_bias
    self.w_init = w_init
    self.b_init = b_init
    self.h_init = h_init
    super().__init__(num_units, h_init)

  def init(self, key, input_shape):
    batch_size, num_inputs = input_shape

    rec_shape = (self.num_units, self.num_units)
    inp_shape = (self.num_units, num_inputs)
    bias_shape = (self.num_units,)
    output_shape = (batch_size, self.num_units)

    def _build_linear_rnn(base_key):
      base_key, *keys = jax.random.split(base_key, 4)
      return base_key, LinearRNN(self.w_init(keys[0], inp_shape),
                                 self.w_init(keys[1], rec_shape),
                                 self.b_init(keys[2], bias_shape))

    # build internal gates / linear rnns.
    # we thread the `key` through the helper function `_build_linear_rnn`,
    # which returns a new key that we then pass to the next function call.
    key, update_gate = _build_linear_rnn(key)
    key, reset_gate = _build_linear_rnn(key)
    key, cell_state = _build_linear_rnn(key)

    key, key_ic = jax.random.split(key, 2)
    initial_state = super().init_initial_state(key_ic)
    params = {
        'initial_state': initial_state,
        'update_gate': update_gate,
        'reset_gate': reset_gate,
        'cell_state': cell_state
    }
    return output_shape, params

  def apply(self, params, inputs, state):
    update_gate = params['update_gate']
    reset_gate = params['reset_gate']
    cell_state = params['cell_state']

    update = jax.nn.sigmoid(update_gate.apply(inputs, state) + self.gate_bias)
    reset = jax.nn.sigmoid(reset_gate.apply(inputs, state))
    cell = jnp.tanh(cell_state.apply(inputs, reset * state))

    return update * state + (1 - update) * cell

class LSTM(RNNCell):
  """Long-short term memory (LSTM)."""

  def __init__(self,
               num_units,
               forget_bias=1.,
               w_init=fan_in,
               b_init=zeros,
               h_init=zeros):
    """Long-short term memory (LSTM) Cell.

    Args:
      num_units: int, Number of units in the RNN.
      forget_bias: float, Bias to add to the forget gate. (Default: 1.0).
      w_init: Initializer for the recurrent & input weights (Default: fan_in).
      b_init: Initializer for the bias (Default: zeros).
      h_init: Initializer for the RNN initial condition (Default: zeros).
    """
    self.forget_bias = forget_bias
    self.w_init = w_init
    self.b_init = b_init
    self.h_init = h_init

    # Note that we double the number of units here, since the LSTM
    # contains units for both the hidden and cell states.
    # These are h(t) and c(t) in most LSTM equations.
    super().__init__(num_units * 2, h_init)

  def init(self, key, input_shape):
    batch_size, num_inputs = input_shape

    # We divide by 2 since for the LSTM, there are separate
    # updates for the cell and hidden state. The full state
    # will be split in two in the apply() method.
    n = int(self.num_units / 2)
    rec_shape = (n, n)
    inp_shape = (n, num_inputs)
    bias_shape = (n,)

    # The output contains the full number of units.
    output_shape = (batch_size, self.num_units)

    def _build_linear_rnn(key):
      key, *prngs = jax.random.split(key, 4)
      return key, LinearRNN(self.w_init(prngs[0], inp_shape),
                            self.w_init(prngs[1], rec_shape),
                            self.b_init(prngs[2], bias_shape))

    # Build internal gates / linear RNNs.
    # We thread the `key` through the helper function `_build_linear_rnn`,
    # which returns a new key that we then pass to the next function call.
    key, forget_gate = _build_linear_rnn(key)
    key, update_gate = _build_linear_rnn(key)
    key, output_gate = _build_linear_rnn(key)
    key, cell_state = _build_linear_rnn(key)

    key, key_ic = jax.random.split(key, 2)
    initial_state = super().init_initial_state(key_ic)
    params = {
        'initial_state': initial_state,
        'forget_gate': forget_gate,
        'update_gate': update_gate,
        'output_gate': output_gate,
        'cell_state': cell_state
    }
    return output_shape, params

  def apply(self, params, inputs, full_state):
    forget_gate = params['forget_gate']
    update_gate = params['update_gate']
    output_gate = params['output_gate']
    cell_state = params['cell_state']

    # Split full state into the hidden and cell states.
    state, cell = jnp.split(full_state, 2, axis=-1)

    f = jax.nn.sigmoid(forget_gate.apply(inputs, state) + self.forget_bias)
    i = jax.nn.sigmoid(update_gate.apply(inputs, state))
    o = jax.nn.sigmoid(output_gate.apply(inputs, state))
    c = jnp.tanh(cell_state.apply(inputs, state))

    new_cell = f * cell + i * c
    new_state = o * jnp.tanh(new_cell)

    return jnp.concatenate((new_state, new_cell), axis=-1)

def embedding(vocab_size,
              embedding_size,
              initializer=jax.nn.initializers.orthogonal()):
  """Builds a token embedding.

  Args:
    vocab_size: int, Size of the vocabulary (number of tokens).
    embedding_size: int, Dimensionality of the embedding.
    initializer: Initializer for the embedding (Default: orthogonal).

  Returns:
    init_fun: callable, Initializes the embedding given a key and input_shape.
    apply_fun: callable, Converts a set of tokens to embedded vectors.
  """

  def init_fun(key, input_shape):
    """Build an embedding matrix."""
    shape = (vocab_size, embedding_size)
    emb = initializer(key, shape)
    output_shape = input_shape + (embedding_size,)
    return output_shape, emb

  def apply_fun(emb, tokens, **kwargs):
    """Embedding lookup."""
    del kwargs  # unused
    return jnp.take(emb, tokens, axis=0)

  return init_fun, apply_fun

reason: import jax,from jax


text:
from qcodes import VisaInstrument
from qcodes.utils import validators as vals
from cmath import phase
import numpy as np
from qcodes import MultiParameter, Parameter

class ZNB20(VisaInstrument):

    """
    qcodes driver for the Rohde & Schwarz ZNB20

    Author: Stefano Poletto (QuTech)
    """

    def __init__(self, name, address, **kwargs):

        super().__init__(name=name, address=address, **kwargs)

        self.add_parameter(name='power',
                           label='Power',
                           unit='dBm',
                           get_cmd='SOUR:POW?',
                           set_cmd='SOUR:POW {:.4f}',
                           get_parser=VISA_str_to_int,
                           vals=vals.Numbers(-150, 25))

        self.add_function('tooltip_on', call_cmd='SYST:ERR:DISP ON')
        self.add_function('tooltip_off', call_cmd='SYST:ERR:DISP OFF')
        self.add_function('cont_meas_on', call_cmd='INIT:CONT:ALL ON')
        self.add_function('cont_meas_off', call_cmd='INIT:CONT:ALL OFF')
        self.add_function('update_display_once', call_cmd='SYST:DISP:UPD ONCE')
        self.add_function('update_display_on', call_cmd='SYST:DISP:UPD ON')
        self.add_function('update_display_off', call_cmd='SYST:DISP:UPD OFF')
        self.add_function('rf_off', call_cmd='OUTP1 OFF')
        self.add_function('rf_on', call_cmd='OUTP1 ON')

        ###################
        # Common commands #
        ###################
        # common commands for all devices as described in IEEE 488.2
        self.add_function('reset', call_cmd='*RST')
        self.add_function('wait_to_continue', call_cmd='*WAI')

        ######################
        # CALCULATE commands #
        ######################
        # commands for post-acquisition data processing
        self.add_parameter('format',
                           set_cmd='CALCULATE:FORMAT {:s}',
                           vals=vals.Enum('mlin', 'mlog', 'phas', 'uph', 'pol',
                                          'smit', 'ism', 'gdel', 'real', 'imag',
                                          'swr'))

        ####################
        # DISPLAY commands #
        ####################
        # Commands to select and present data on screen
        self.add_function('autoscale_trace', call_cmd='DISP:TRAC:Y:AUTO ONCE')

        #####################
        # INITIATE commands #
        #####################
        # commands to control the initialization of the trigger system and define
        # the scope ot the triggered measurement
        self.add_parameter(name='continuous_mode_all',
                           docstring='My explanation',
                           set_cmd='INIT:CONT {:s}',
                           vals=vals.OnOff())

        self.add_function('start_sweep_all', call_cmd='INITIATE:IMMEDIATE:ALL')

        ##################
        # SENSE commands #
        ##################
        # commands affecting the receiver settings
        self.add_function('clear_avg', call_cmd='SENS:AVER:CLEAR')

        self.add_parameter(name='avg',
                           label='Averages',
                           unit='',
                           get_cmd='SENS:AVER:COUN?',
                           set_cmd='SENS:AVER:COUN {:.4f}',
                           get_parser=VISA_str_to_int,
                           vals=vals.Numbers(1, 1000))

        self.add_parameter(name='average_mode',
                           get_cmd='SENS:AVER:MODE?',
                           set_cmd='SENS:AVER:MODE {:s}',
                           vals=vals.Enum('auto', 'flatten', 'reduce', 'moving'))

        self.add_parameter(name='average_state',
                           get_cmd='SENS:AVER:STAT?',
                           set_cmd='SENS:AVER:STAT {:s}',
                           vals=vals.OnOff())

        self.add_parameter(name='bandwidth',
                           label='Bandwidth',
                           unit='Hz',
                           get_cmd='SENS:BAND?',
                           set_cmd='SENS:BAND {:.4f}',
                           get_parser=VISA_str_to_int,
                           vals=vals.Numbers(1, 1e6))

        self.add_parameter(name='center_frequency',
                           unit='Hz',
                           get_cmd='SENSE:FREQUENCY:CENTER?',
                           set_cmd='SENSE:FREQUENCY:CENTER {:.4f}',
                           get_parser=VISA_str_to_int,
                           vals=vals.Numbers(100e3, 20e9))

        self.add_parameter(name='span_frequency',
                           unit='Hz',
                           get_cmd='SENSE:FREQUENCY:SPAN?',
                           set_cmd='SENSE:FREQUENCY:SPAN {:.4f}',
                           get_parser=VISA_str_to_int,
                           vals=vals.Numbers(0, 20e9))

        self.add_parameter(name='start_frequency',
                           unit='Hz',
                           get_cmd='SENSE:FREQUENCY:START?',
                           set_cmd='SENSE:FREQUENCY:START {:.4f}',
                           get_parser=VISA_str_to_int,
                           vals=vals.Numbers(100e3, 20e9))

        self.add_parameter(name='stop_frequency',
                           unit='Hz',
                           get_cmd='SENSE:FREQUENCY:STOP?',
                           set_cmd='SENSE:FREQUENCY:STOP {:.4f}',
                           get_parser=VISA_str_to_int,
                           vals=vals.Numbers(100e3, 20e9))

        self.add_function('delete_all_segments', call_cmd='SENS:SEGM:DEL:ALL')

        self.add_parameter(name='number_sweeps_all',
                           set_cmd='SENS:SWE:COUN:ALL {:.4f}',
                           vals=vals.Ints(1, 100000))

        self.add_parameter(name='npts',
                           get_cmd='SENS:SWE:POIN?',
                           set_cmd='SENS:SWE:POIN {:.4f}',
                           get_parser=VISA_str_to_int,
                           vals=vals.Ints(1, 100001))

        self.add_parameter(name='min_sweep_time',
                           get_cmd='SENS:SWE:TIME:AUTO?',
                           set_cmd='SENS:SWE:TIME:AUTO {:s}',
                           get_parser=VISA_str_to_int,
                           vals=vals.OnOff())

        self.add_parameter(name='sweep_time',
                           get_cmd='SENS:SWE:TIME?',
                           set_cmd='SENS:SWE:TIME {:.4f}',
                           get_parser=VISA_str_to_float,
                           vals=vals.Numbers(0, 1e5))

        self.add_parameter(name='sweep_type',
                           get_cmd='SENS:SWE:TYPE?',
                           set_cmd='SENS:SWE:TYPE {:s}',
                           get_parser=str,
                           vals=vals.Enum('lin', 'linear', 'log', 'logarithmic', 'pow', 'power',
                                          'cw', 'poin', 'point', 'segm', 'segment'))

        #####################
        #  TRIGGER commands #
        #####################
        # commands to syncronize analyzer's actions
        self.add_parameter(name='trigger_source',
                           set_cmd='TRIGGER:SEQUENCE:SOURCE {:s}',
                           vals=vals.Enum('immediate', 'external', 'manual', 'multiple'))

        self.reset()
        self.connect_message()

    def get_stimulus(self):
        '''
        get the frequencies used in the sweep
        '''
        stimulus_str = self.ask('CALC:DATA:STIM?')
        stimulus_double = np.array(stimulus_str.split(','), dtype=np.double)

        return stimulus_double

    def get_real_imaginary_data(self):
        data_str = self.ask('CALC:DATA? SDAT')
        data_double = np.array(data_str.split(','), dtype=np.double)

        real_data = data_double[::2]
        imag_data = data_double[1::2]

        return real_data, imag_data

    def get_formatted_data(selft, format):
        print('in progress')

def VISA_str_to_int(message):
    return int(float(message.strip('\\n')))

def VISA_str_to_float(message):
    return float(message.strip('\\n'))

# Ensuring backwards compatibility
print('This is the version by Stefano, there is another version in QCoDeS')
# from .ZNB import ZNB as ZNB20


text:
import torch
from torch import nn
from torchvision import transforms
import numpy as np

from .Algorithm import Algorithm

class SaliencyMap(Algorithm):
    """
        Saliency Map
        Taken from the paper https://arxiv.org/abs/1312.6034
    """

    def __init__(self, model: nn.Module,
                 image: np.ndarray,
                 transform: transforms = None,
                 gpu: bool = True):
        """
        :param model: pytorch model
        :param image: raw image data, only support one image once,
                      if it's 3 channel, you have better to change it to RGB mode
        :param transform: the same as your model's transform
        :param gpu: whether use cuda or not
        """
        super().__init__(model, image, transform, gpu)

    def forward(self):
        img = self.transform(self.image).unsqueeze(0).to(self.device)
        img.requires_grad = True

        output = self.model(img)
        self.model.zero_grad()
        index = output.argmax(1).cpu().detach().numpy()[0]
        output[0][index].backward()

        self.mask, _ = torch.max(abs(img.grad[0].detach()), dim=0)
        self.mask = self.mask.cpu().numpy()

        return self.mask

max_stars_repo_name: Torato-Taraka/VINN

max_issues_repo_name: Torato-Taraka/VINN

max_forks_repo_name: Torato-Taraka/VINN