Skip to content
Snippets Groups Projects
Commit ba9b5d81 authored by MazdakFatahi's avatar MazdakFatahi
Browse files

HelperFunctions is added

parent 59246e7b
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Here you can find some useful functions for working with the SpiNNaker ;)
%% Cell type:markdown id: tags:
~~~
ExtractConnectionFile(Input_File, Connections_File,Non_Zero, Connections_shape, n_samples_to_plot)
~~~
Using the CSNN simulator, by exporting the weights as binary files, using ***ExtractConnectionFile*** we can extract the weights as connection files to use as projections between the Populations on the SpiNNaker
%% Cell type:code id: tags:
``` python
import numpy as np
import pathlib
import math
from mpl_toolkits.axes_grid1 import ImageGrid
import matplotlib.pyplot as plt
def plot_and_save_grid(input_data,nrows_ncols,lable='Sample Plost',save=False,file_name=None):
fig = plt.figure(figsize=(10, 10))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols, # creates nrow x ncol grid of axes
axes_pad=0.1, # pad between axes in inch.
)
for ax, inx in zip(grid, list(range(nrows_ncols[0]*nrows_ncols[1]))):
ax.imshow(input_data[inx],cmap="jet")
ax.set_axis_off()
if save:
plt.savefig(file_name)
return
def Plot_extracted_weights(Input_File, Connections_File,Connections_shape={'x':28, 'y':28, 'z':400}, n_samples_to_plot=0):
w = np.fromfile(Input_File, dtype=np.dtype(np.float32))#[28, 28, 1, 400]
print(f'Input file shape = {w.shape}')
w=w.reshape(Connections_shape['x'], Connections_shape['y'], Connections_shape['z'])#x, y, z
w=np.array([w[:, :, i].transpose() for i in range(Connections_shape['z']) ])
w=w.reshape(Connections_shape['z'], Connections_shape['x']*Connections_shape['y']).transpose()# 400rows of 28*28 to 400 rows of 784 and then transposed==> 784*400
if n_samples_to_plot:
w_to_plot=[w.reshape(Connections_shape['x'], Connections_shape['y'],-1)[:,:,i] for i in range(n_samples_to_plot)]
plot_and_save_grid(w_to_plot,(int(math.sqrt(n_samples_to_plot)),int(math.sqrt(n_samples_to_plot))))
return
def ExtractConnectionFile(Input_File, Connections_File,Non_Zero=False, Connections_shape={'x':28, 'y':28, 'z':400}, n_samples_to_plot=0):
extracted_weights=[]
w = np.fromfile(Input_File, dtype=np.dtype(np.float32))#[28, 28, 1, 400]
print(f'Input file shape = {w.shape}')
# w=w.reshape(Connections_shape['x'], Connections_shape['y'], Connections_shape['z'])#x, y, z
# w=np.array([w[:, :, i].transpose() for i in range(Connections_shape['z']) ])
f=open(Connections_File, 'w')
f.write('# columns = ["i", "j", "weight", "delay"]\n')
w=w.reshape(Connections_shape['x']*Connections_shape['y'], Connections_shape['z'])# 400rows of 28*28 to 400 rows of 784 and then transposed==> 784*400
X,Y=w.shape
for x in range(X):
for y in range(Y):
if Non_Zero:
if w[x][y]:
line=f'{x} {y} {w[x][y]} 1\n'
f.write(line)
extracted_weights.append(w[x][y])
else:
line=f'{x} {y} {w[x][y]} 1\n'
f.write(line)
extracted_weights.append(w[x][y])
f.close()
if n_samples_to_plot:
w_to_plot=[w.reshape(Connections_shape['x'], Connections_shape['y'],-1)[:,:,i] for i in range(n_samples_to_plot)]
plot_and_save_grid(w_to_plot,(int(math.sqrt(n_samples_to_plot)),int(math.sqrt(n_samples_to_plot))))
print(f'Output file shape = {w.shape}')
print(f'Connections File Path = {pathlib.Path(Connections_File).parent.resolve()}/{Connections_File}')
return pathlib.Path(Connections_File).parent.resolve(), extracted_weights
```
%% Cell type:markdown id: tags:
## HOw to use:
Just put the binary files in an array as below and call the function for all the files:
%% Cell type:code id: tags:
``` python
files_name=['weights_fc1_0_0_25_0.7', 'weights_fc2_0_0_25_0.7', 'weights_fc1_1_1_25_0.7', 'weights_fc2_1_1_25_0.7']
weights=[[],[],[],[]]
shape=[{'x':28, 'y':28, 'z':400}, {'x':20, 'y':20, 'z':1600}]
Non_Zero=False
for i, file_name in enumerate(files_name):
_, weights[i]=ExtractConnectionFile(Input_File=file_name,
Connections_File=f'{file_name}_Zero_Removed_{str(Non_Zero)}.txt',
Non_Zero=Non_Zero,
Connections_shape=shape[i%2],
n_samples_to_plot=0
)
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment