Memristor_Crossbar Documentation

class memristor_crossbar.Memristor_Crossbar.Memristor_Crossbar(beta: float, positive_target: float, negative_target: float, multiplication_factor: int, training_set_width: int = 6, epochs: int = 51, number_of_neurons: int = 2, number_of_rows: int = 4, number_of_columns: int = 4, max_value: int = 1e-07, min_value: int = 1e-08, division_factor: int = 5, test_set_width: int = 10, num_elements: int = 16, conductance_data: numpy.ndarray = None, shifts: numpy.ndarray = None, logic_currents: numpy.ndarray = None, all_delta_ij: numpy.ndarray = None, conductances: numpy.ndarray = None, all_conductances: numpy.ndarray = None, saved_correct_conductances: numpy.ndarray = None, errors: numpy.ndarray = None, all_errors: numpy.ndarray = None, result: numpy.ndarray = None, predictions: numpy.ndarray = None)[source][source]

Bases: object

activation_function() ndarray[source][source]

Apply the activation function (hyperbolic tangent) to the logic currents.

Returns:

Activation values.

Return type:

np.ndarray

activation_function_derivative() ndarray[source][source]

Calculate the derivative of the activation function.

Returns:

Derivative of the activation values.

Return type:

np.ndarray

all_conductances: ndarray = None[source]
all_delta_ij: ndarray = None[source]
all_errors: ndarray = None[source]
beta: float[source]
calculate_DeltaW_ij() ndarray[source][source]

Calculate the DeltaW_ij values by summing and transposing the Delta_ij values.

Returns:

The transposed array of DeltaW_ij values.

Return type:

np.ndarray

Notes

  • DeltaW_ij values are calculated by taking the sign of the sum of all Delta_ij values and transposing the result.

  • Logs the DeltaW_ij values for debugging or verification.

calculate_Delta_ij(output: ndarray, pattern: ndarray, i: int) None[source][source]

Calculate and store Delta_ij values as the outer product of voltages and delta_i.

Parameters:
  • output (np.ndarray) – Target output values.

  • pattern (np.ndarray) – Input pattern determining applied voltages.

  • i (int) – Index to store calculated Delta_ij values.

Return type:

None

calculate_delta_i(output: ndarray) ndarray[source][source]

Compute delta values for a given output using activation function and its derivative.

Parameters:

output (np.ndarray) – Target output, where each element is either 1 (positive target) or 0 (negative target).

Returns:

Delta values calculated as (target_value - activation) * activation_derivative.

Return type:

np.ndarray

Examples

>>> output = np.array([1, 0, 1, 0, 1])
>>> delta_i = obj.calculate_delta_i(output)
>>> delta_i
array([delta_value_1, delta_value_2, ..., delta_value_n])
calculate_hardware_currents(pattern: ndarray, conductances: ndarray) ndarray[source][source]

Calculate hardware currents as the vector-by-matrix product of the pattern and conductances.

Parameters:
  • pattern (np.ndarray) – Input pattern.

  • conductances (np.ndarray) – Array of conductances.

Returns:

Calculated hardware currents.

Return type:

np.ndarray

Examples

>>> pattern = np.array([1, 0, 1, 1])
>>> conductances = np.array([[0.2, -1.0,  0.8, -0.6],
...                          [0.4, -0.2,  1.0, -0.8],
...                          [0.6, -0.4,  1.2, -1.0],
...                          [0.8, -0.6,  1.4, -1.2]])
>>> hardware_currents = obj.calculate_hardware_currents(pattern, conductances)
>>> hardware_currents
array([0.12, -0.18, 0.24, -0.2])
calculate_logic_currents(pattern: ndarray, conductances: ndarray) None[source][source]

Calculate logic currents by subtracting alternate hardware currents.

Parameters:
  • pattern (np.ndarray) – Input pattern determining applied voltages.

  • conductances (np.ndarray) – Conductance values.

Return type:

None

check_convergence(i) bool[source][source]

Check the convergence of the activation function for a given pattern.

Parameters:

i (int) – The index of the pattern to check.

Returns:

Whether the activation function converged for the given pattern.

Return type:

bool

Notes

Updates the predictions array based on the comparison of the activation function results with the positive and negative targets.

conductance_data: ndarray = None[source]
conductance_init() None[source][source]

Initializes the conductance values with random shifts and a multiplication factor. The first element is the sum of the normalized conductance data and shifts, multiplied by the multiplication factor while the second element is the index zero.

Return type:

None

Examples

>>> self.conductance_data[0] = np.array([0.0, 1.0, 2.0, 3.0])
>>> self.shifts = np.array([[ 0.1, -0.5,  0.4, -0.3],
...                         [ 0.2, -0.1,  0.5, -0.4],
...                         [ 0.3, -0.2,  0.6, -0.5],
...                         [ 0.4, -0.3,  0.7, -0.6]])
>>> self.multiplication_factor = 2
>>> self.conductances[0] = np.array([[ 0.2, -1.0,  0.8, -0.6],
...                                  [ 0.4, -0.2,  1.0, -0.8],
...                                  [ 0.6, -0.4,  1.2, -1.0],
...                                  [ 0.8, -0.6,  1.4, -1.2]])
>>> self.conductances[1] = 0

Logs the initialized conductances and the epoch number:

>>> "Initial Conductances: [0.2 -1.0 0.8 -0.6 ...]"
>>> "Epoch: 0"
conductances: ndarray = None[source]
convergence_criterion(output: ndarray, i: int, epoch: int) bool[source][source]

Check if the model’s activation values meet the convergence criterion.

Parameters:
  • output (np.ndarray) – The target output values.

  • i (int) – Index of the current pattern.

  • epoch (int) – The current epoch index.

Returns:

True if the model has converged, False otherwise.

Return type:

bool

custom_shift(custom_shifts: ndarray) None[source][source]

Sets custom shifts for the conductance values based on a user-defined 4x4 array.

Parameters:

custom_shifts (np.ndarray) – A 4x4 NumPy array containing user-defined shifts for the conductance values.

Return type:

None

division_factor: int = 5[source]
epochs: int = 51[source]
errors: ndarray = None[source]
experimental_data(conductance_data: ndarray)[source][source]

Initializes the conductance data by normalizing it to the first value.

Parameters:

conductance_data (-) – The raw set of conductance data to be processed.

Return type:

None

Examples

>>> conductance_data = np.array([5.0, 6.0, 7.0, 8.0])
>>> self.conductance_data = np.array([0.0, 1.0, 2.0, 3.0])
fit(patterns: ndarray, outputs: ndarray, conductance_data: ndarray | None = None, custom_shifts: ndarray | None = None, save_data: bool = False, filename: str = 'simulation') None[source][source]

Trains the model using the provided patterns and outputs. This method iteratively updates the model weights based on the training patterns and target outputs until convergence is achieved. It logs key events during the training process, including convergence status and conductance values.

Parameters:
  • patterns (np.ndarray) – The input patterns for training, where each row is a sample and each column represents a feature.

  • outputs (np.ndarray) – The target outputs for training, corresponding to the input patterns.

  • conductance_data (np.ndarray, optional) – Initial conductance data used for training. Defaults to None.

  • custom_shifts (np.ndarray, optional) – Custom shift values for conductance updates. Defaults to None.

  • save_data (bool, optional) – Whether to save the simulation data. Defaults to False.

  • filename (str, optional) – The base filename for saving data. Defaults to “simulation”.

Return type:

None

Notes

This method iteratively updates the model weights based on the training patterns and target outputs until convergence is achieved. It logs key events during the training process, including convergence status and conductance values.

logic_currents: ndarray = None[source]
max_value: int = 1e-07[source]
min_value: int = 1e-08[source]
multiplication_factor: int[source]
negative_target: float[source]
num_elements: int = 16[source]
number_of_columns: int = 4[source]
number_of_neurons: int = 2[source]
number_of_rows: int = 4[source]
plot_conductances(epochs)[source][source]

Generates a series of subplots showing the evolution of conductances over epochs.

Parameters:

epochs (int) – The number of epochs to plot.

Returns:

A tuple containing the figure and axes objects of the plot.

Return type:

tuple

Notes

This method creates a grid of subplots using matplotlib, where each subplot shows the conductance values for a specific neuron and input pair over the given epochs.

plot_error(epochs)[source][source]

Generates a plot of the total error over epochs.

Parameters:

epochs (int) – The number of epochs to plot.

Returns:

A tuple containing the figure and axes objects of the plot.

Return type:

tuple

Notes

This method creates a line plot using matplotlib to visualize the evolution of the total error over the given epochs.

plot_final_weights()[source][source]

Generates a 3D bar plot of the final conductance weights.

Returns:

A tuple containing the figure and axes objects of the plot.

Return type:

tuple

Notes

This method creates a 3D bar plot using matplotlib to visualize the final conductance weights. The x and y axes represent the neurons and inputs, respectively, while the z-axis represents the conductance values.

plot_results(pattern: ndarray, output: ndarray, epochs)[source][source]

Generates a series of subplots showing the activation results over epochs.

Parameters:
  • pattern (np.ndarray) – The input pattern.

  • output (np.ndarray) – The target output values.

  • epochs (int) – The number of epochs to plot.

Returns:

A tuple containing the figure and axes objects of the plot.

Return type:

tuple

Notes

This method creates a grid of subplots using matplotlib, where each subplot shows the activation results for a specific neuron and input pair over the given epochs. Horizontal lines indicate the positive and negative targets.

plot_weights(epochs)[source][source]

Generates a series of subplots showing the evolution of synaptic weights over epochs.

Parameters:

epochs (int) – The number of epochs to plot.

Returns:

A tuple containing the figure and axes objects of the plot.

Return type:

tuple

Notes

This method creates a grid of subplots using matplotlib, where each subplot shows the difference between paired conductances (synaptic weights) for a specific neuron over the given epochs.

positive_target: float[source]
predict(patterns, outputs)[source][source]

Make predictions based on saved conductances and the provided patterns.

Parameters:
  • patterns (np.ndarray) – The input patterns.

  • outputs (np.ndarray) – The expected output values.

Return type:

None

Notes

Calculates the logic currents for each pattern using the saved correct conductances and checks for convergence. Prints the pattern, prediction, and expected result for each input pattern.

predictions: ndarray = None[source]
result: ndarray = None[source]
save_data(base_filename='simulation', converged=False)[source][source]

Saves the simulation data to a CSV file.

Parameters:
  • base_filename (str, optional) – The base name for the output file. Defaults to “simulation”.

  • converged (bool, optional) – Indicates if the simulation converged. Defaults to False.

Return type:

None

Notes

This method saves various parameters and results of the simulation to a CSV file. The file is stored in a directory named with the current date. Subdirectories for converged and non-converged simulations are created as needed.

saved_correct_conductances: ndarray = None[source]
shift_lognormal() None[source][source]

Generates random shifts for conductance values using a lognormal distribution, scales them to a specified range, and reshapes the result into a 4x4 array.

Parameters:
  • self.min_value (-) – The lower bound of the scaled random shifts.

  • self.max_value (-) – The upper bound of the scaled random shifts.

  • self.division_factor (-) – Controls the spread (standard deviation) of the lognormal distribution.

  • self.num_elements (-) – Total number of elements to be generated (size of reshaped array).

Return type:

None

Examples

>>> crossbar = Memristor_Crossbar()
>>> crossbar.shift_lognormal()
>>> print(crossbar.shifts)
[[1.23e-08 1.56e-08 1.10e-08 1.45e-08]
[1.34e-08 1.29e-08 1.60e-08 1.47e-08]
[1.32e-08 1.50e-08 1.48e-08 1.28e-08]
[1.40e-08 1.36e-08 1.41e-08 1.44e-08]]
shifts: ndarray = None[source]
test_set_width: int = 10[source]
total_error(epoch: int) None[source][source]

Calculate and log the total error for the given epoch.

Parameters:

epoch (int) – The current epoch index.

Return type:

None

Notes

  • The total error is the sum of all individual errors.

  • Logs the total error for debugging or monitoring purposes.

training_set_width: int = 6[source]
update_weights(epoch) None[source][source]

Update the weights based on the DeltaW_ij values and store the conductances for the given epoch.

Parameters:

epoch (int) – The current epoch index.

Return type:

None

Notes

  • Iterates through each element of DeltaW_ij to update conductance values.

  • If DeltaW_ij[i, j] > 0 or < 0, applies shifts and a multiplication factor to update the conductances.

  • If DeltaW_ij[i, j] == 0, no changes are made.

visualize_graphs(epoch: int, patterns: ndarray, outputs: ndarray, converged: bool) None[source][source]

Generate plots for the results after fitting the model.

Parameters:
  • epoch (int) – The epoch at which convergence was reached.

  • patterns (np.ndarray) – The input patterns used for training.

  • outputs (np.ndarray) – The target outputs corresponding to the input patterns.

  • converged (bool) – Indicates whether the simulation converged.

Return type:

None

voltage_array(pattern: ndarray, V0=-0.1, V1=0.1) ndarray[source][source]

Generates an array of voltages based on a given pattern.

Parameters:
  • pattern (np.ndarray) – The input pattern determining the voltage values.

  • V0 (float, optional) – Voltage for pattern value 0. Defaults to -0.1.

  • V1 (float, optional) – Voltage for pattern value 1. Defaults to 0.1.

Returns:

An array of voltages corresponding to the pattern.

Return type:

np.ndarray

Examples

>>> pattern = np.array([0, 1, 0, 1])
>>> V0 = -0.1
>>> V1 = 0.1
>>> voltages_j = np.array([-0.1,  0.1, -0.1,  0.1])