コード例 #1
0
def get_accuracy(x, targets):
    if isinstance(x, torch.Tensor):
        x = x.cpu().data.numpy()
    if isinstance(targets, torch.Tensor):
        targets = targets.cpu().data.numpy()
    accuracy, _, _ = perceptron(x, targets)
    return accuracy
コード例 #2
0
ファイル: gate_finder.py プロジェクト: jtwild/brainspy-tasks
 def find_gate_with_numpy(self, encoded_inputs, encoded_gate, mask):
     excel_results = self.optimize(encoded_inputs, encoded_gate, mask)
     excel_results['accuracy'], _, _ = perceptron(
         excel_results['best_output'][excel_results['mask']],
         encoded_gate[excel_results['mask']])
     excel_results['encoded_gate'] = encoded_gate
     return excel_results
コード例 #3
0
ファイル: gate_finder.py プロジェクト: jtwild/brainspy-tasks
 def find_gate_with_torch(self, encoded_inputs, encoded_gate, mask):
     encoded_gate = TorchUtils.format_tensor(encoded_gate)
     excel_results = self.optimize(encoded_inputs, encoded_gate, mask)
     excel_results['accuracy'], _, _ = perceptron(
         excel_results['best_output'][excel_results['mask']],
         TorchUtils.get_numpy_from_tensor(
             encoded_gate[excel_results['mask']]))
     excel_results['encoded_gate'] = encoded_gate.cpu()
     # excel_results['targets'] = excel_results
     excel_results['correlation'] = corr_coeff(
         excel_results['best_output'][excel_results['mask']].T,
         excel_results['targets'].cpu()[excel_results['mask']].T)
     return excel_results
コード例 #4
0
ファイル: fitness.py プロジェクト: jtwild/brainspy-algorithms
def accuracy_fit(outputpool, target, clipvalue=np.inf):
    genomes = len(outputpool)
    fitpool = np.zeros(genomes)
    for j in range(genomes):
        output = outputpool[j]

        if np.any(np.abs(output) > clipvalue):
            acc = 0
            # print(f'Clipped at {clipvalue} nA')
        else:
            x = output[:, np.newaxis]
            y = target[:, np.newaxis]
            acc, _, _ = perceptron(x, y)

        fitpool[j] = acc
    return fitpool
コード例 #5
0
def validate_outputs(configs):
    '''Validates several optupts given a single input data stream.
    This is useful for example to validate a single VC-dim experiment.
    '''

    validator = Hardware_Validator(configs)
    data_file = os.path.join(validator.validation_dir, configs['npz_file'] + '.npz')
    with np.load(data_file) as data:
        predictions = data['output_array']
        mask = data['mask']
        targets_array = data['targets_array'][:, mask, np.newaxis]
        inputs = data['inputs']
        control_voltages_array = data['control_voltages_per_gate']
        gate_array = data['gate_array']

    N = len(gate_array[0])
    acceptance_threshold = (1 - 0.5 / N) * 100
    correlation_array = np.zeros(len(gate_array))
    accuracy_array = np.zeros_like(correlation_array)
    threshold_array = np.zeros_like(correlation_array)
    found_array = np.zeros_like(correlation_array)
    mserror_array = np.zeros_like(correlation_array)
    measurement_array = np.zeros((len(gate_array), len(inputs[mask, 0])))
    labels_array = np.zeros_like(measurement_array)

    for n, cv in enumerate(control_voltages_array):
        name = ''
        for s in str(gate_array[n]).split('.'):
            name += s
        if len(np.unique(gate_array[n])) == 1:
            print(f"Ignore validation for {name}")
            found_array[n] = True
            accuracy_array[n] = control_voltages_array[n, 0]
            threshold_array[n] = control_voltages_array[n, 0]
            labels_array[n] = control_voltages_array[n, 0] * inputs[mask, 0]
            measurement_array[n] = control_voltages_array[n, 0] * inputs[mask, 0]
            continue
        else:
            mserror, _, measurement = validator.validate_prediction(name, inputs, cv, predictions[n], mask)
            plt_name = os.path.join(validator.validation_dir, name)
            accuracy, predicted_labels, threshold = perceptron(measurement, targets_array[n], plot=plt_name)
            correlation_array[n] = corr_coeff(measurement.T, targets_array[n].T)
            mserror_array[n] = mserror
            print(f"{name} has accuracy {accuracy:.2f} % and correlation {correlation_array[n]:.2f}")
            found_array[n] = accuracy > acceptance_threshold
            accuracy_array[n] = accuracy
            threshold_array[n] = threshold
            labels_array[n] = predicted_labels[:, 0]
            measurement_array[n] = measurement[:, 0]

    capacity = np.mean(found_array)
    print(f"Capacity for N={N}: {capacity}")

    file_name = os.path.join(validator.validation_dir, 'validated_data')
    np.savez(file_name,
             mserror_array=mserror_array,
             correlation_array=correlation_array,
             accuracy_array=accuracy_array,
             threshold_array=threshold_array,
             measurement_array=measurement_array,
             labels_array=labels_array,
             found_array=found_array, capacity=capacity)

    return capacity, correlation_array, accuracy_array, mserror_array