Training multiple targets
Begin with a few package imports:
[1]:
from IPython.display import HTML
import numpy as np
import matplotlib.pyplot as plt
from packing_utils import *
from allosteric_utils import *
from plot_imports import *
Network generation
Load in a saved, untrained network from file:
[2]:
%matplotlib notebook
n = 64
filename = 'allo_{:d}.txt'.format(n)
allo = Allosteric(filename)
allo.plot()
Automatically add one more source and two more targets:
[3]:
allo.add_sources(1)
allo.add_targets(2)
Save the new network:
[4]:
allo.save('multi_{:d}.txt'.format(n))
Training
Specify a source and target strain to apply, training time, and training method.
[5]:
es = 0.2 # source strain of 20%
et = 0.2 # target strain of 20% applied to all targets
ka = 100. # stiffness of spring for applied strain
duration = 4e7 # training time
frames = 200 # number of output frames
train = 2 # 1 trains bond rest lengths, 2 trains bond stiffnesses
method = 'learning' # learning rule to use, 'aging' (directed aging) or 'learning' (coupled learning)
# compute the mean square bond length as a normalization factor
l2 = np.mean([edge[2]['length']**2 for edge in allo.graph.edges(data=True)])
eta = 1e-1 # nudge factor
alpha = 1e-3/l2 # learning rate
vmin = 1e-3 # minimum allowed stiffness
vsmooth = 1e-2 # value of k at which to begin smooth ramp down to vmin
Since the network is untrained, applying a strain at the source (blue curve) produces no strain at either target (red curves):
[6]:
%matplotlib notebook
allo.reset_init()
sol = allo.solve(duration=duration, frames=frames, T=0, applied_args=(es, 0, ka))
allo.strain_plot()
progress: 100%|###############################################9| 40000000.00/40000000.00 [00:03<00:00]
Train the system:
[7]:
sol = allo.solve(duration=duration, frames=frames, T=0, applied_args=(es, et, ka),
train=train, method=method, eta=eta, alpha=alpha, vmin=vmin, vsmooth=vsmooth)
progress: 100%|################################################| 40000000.00/40000000.00 [00:08<00:00]
We can view the how the strain at all targets (red) varies with training while the network has constant applied source strains (blue).
[8]:
%matplotlib notebook
allo.strain_plot()
Visualization
View the final distribution of stiffnesses:
[9]:
%matplotlib notebook
k = allo.distribution_plot(kind='stiffness', vmin=0, vmax=2, nbins=25)
Also visualize which bonds weakened (reds) and which bonds strengthened (blues):
[10]:
%matplotlib notebook
allo.reset_init() # reset network to its equilibrium state for visualization
cmap = continuous_cmap([pal['red'], pal['yellow'], np.array([0.8,0.8,0.8]),
pal['blue'], pal['purple']], [0,0.1,0.5,0.9,1])
allo.color_plot(cmap, vmin=0, vmax=2)
Animation
Apply a sinusoidal strain at the sources and monitor the targets:
[11]:
%matplotlib notebook
allo.reset_init()
duration = 4e7
frames = 200
period = 2e7
allo.solve(duration=duration, frames=frames, T=period, applied_args=(es, 0, ka))
allo.strain_plot()
progress: 100%|################################################| 40000000.00/40000000.00 [00:00<00:00]
The following cell produces a movie of the network as it undergoes the sinusoidal strain:
[ ]:
%matplotlib inline
ani = allo.animate()
HTML(ani.to_html5_video())