<jupyter_start><jupyter_text>Test DCWM with Balanced CYCLE Network (BCN) generalized first order task Task FF controller dim=input+interface/512/1024/512/output+interface Interface Vector Dim= 350<jupyter_code>import logging
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt
from model import BCN
from trainer import trainer

import pickle
import dill

logger = tf.get_logger()
logger.setLevel(logging.ERROR)<jupyter_output><empty_output><jupyter_text>Generate or Loading training data<jupyter_code>cols = 4
shapesize = 2 #2^2=4 shapes
remembered = 8 # doesnt work with other number must change y by yourself
n=400 # all posible sequences is (2^4)^rem(8)= 4,294,967,296<jupyter_output><empty_output><jupyter_text>X<jupyter_code>x=np.random.randint(0,2,(1,1,cols))
for i in range(n):
    temp=np.random.randint(0,2,(1,1,cols))
    x=np.append(x,temp,axis=0)
#x<jupyter_output><empty_output><jupyter_text>Y<jupyter_code>#for first
temp=np.zeros((1,1,remembered*cols))
for j in range(remembered):
    temp[0,0,j*cols:(j+1)*cols]=x[-j,0,:]
y=temp

#for others
i=1
while i<=n:
    temp=np.zeros((1,1,remembered*cols))
    for j in range(remembered):
        temp[0,0,j*cols:(j+1)*cols]=x[i-j,0,:]
    y=np.append(y,temp,axis=0)
    i=i+1

#y
#saving for first run
#dill.dump(x, file = open("x.pickle", "wb"))
#dill.dump(y, file = open("y.pickle", "wb"))
#loading for next runs
x=dill.load(open("x.pickle", "rb"))
y=dill.load(open("y.pickle", "rb"))<jupyter_output><empty_output><jupyter_text>see data format<jupyter_code>np.shape(x)
np.shape(y)<jupyter_output><empty_output><jupyter_text>Initialize and train modelInitialize or loading:<jupyter_code>interface_vactor_dim=350 #defult 35
bcn = BCN(input_dim=cols,output_dim=remembered*cols,controller_dime=512,interface_dim=interface_vactor_dim,netsize=1000,degree=20)
history=[]
#loading old model
bcn= dill.load(open("trained_bcn.pickle", "rb"))
history= dill.load(open("history.pickle", "rb"))<jupyter_output><empty_output><jupyter_text>Train:<jupyter_code>#n*epochs=all_number_of_epochs_with_this_training_rate
n=0 #set 2 for the train , set 0 for loading
lastnum=0 #last number of last run_shows
epochsnum=25
#I use loop because i dont have powerfull device to run whole epochs at ones and I need save and check point in each round
for i in range(n):
    h=trainer(
        model=bcn,
        loss_fn=tf.keras.losses.mse,
        X_train=x,
        y_train=y,
        optimizer=tf.keras.optimizers.RMSprop(learning_rate=1e-4),
        epochs=epochsnum,
        batch_size=1,
        verbose=True)
    
    print((i+1)*epochsnum+lastnum)
    history=np.append(history,h)

    #save model
    dill.dump(bcn, file = open("trained_bcn.pickle", "wb"))
    dill.dump(history, file = open("history.pickle", "wb"))

#n*epochs=all_number_of_epochs_with_this_training_rate
n=0 #set 2 for the train , set 0 for loading
lastnum=50 #last number of last run_shows
epochsnum=25
#I use loop because i dont have powerfull device to run whole epochs at ones and I need save and check point in each round
for i in range(n):
    h=trainer(
        model=bcn,
        loss_fn=tf.keras.losses.mse,
        X_train=x,
        y_train=y,
        optimizer=tf.keras.optimizers.RMSprop(learning_rate=1e-5),
        epochs=epochsnum,
        batch_size=1,
        verbose=True)
    
    print((i+1)*epochsnum+lastnum)
    history=np.append(history,h)

    #save model
    dill.dump(bcn, file = open("trained_bcn.pickle", "wb"))
    dill.dump(history, file = open("history.pickle", "wb"))<jupyter_output><empty_output><jupyter_text>Printing History and Loss Graph<jupyter_code>#history
#plot history
%matplotlib notebook
plt.plot(history, label='non-random Balanced Networks')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
plt.show()
plt.savefig('nonrandom balanced NoConvergence.png')<jupyter_output><empty_output><jupyter_text>Pridiction Predict on `new X_test`:<jupyter_code>test_num=19 #one less
x_test=np.random.randint(0,2,(1,1,cols))
for i in range(test_num):
    temp=np.random.randint(0,2,(1,1,cols))
    x_test=np.append(x_test,temp,axis=0)
#x_test
np.shape(x_test)
#temp=np.concatenate((np.concatenate((x_test[0],x[-1]),axis=1),x[-2]),axis=1)
temp=np.concatenate((x_test[0],x[-1]),axis=1)
j=2
while j<remembered:
    temp=np.concatenate((temp,x[-j]),axis=1)
    j=j+1
y_test=temp.reshape(1,1,cols*remembered)

for i in range(test_num):
    temp=np.concatenate((x_test[i+1],x_test[i]),axis=1)
    j=2
    while j < remembered:
        if (i-j+1>=0) : temp=np.concatenate((temp,x_test[i-j+1]),axis=1)
        else : temp=np.concatenate((temp,x[i-j+1]),axis=1)
        j=j+1
    y_test=np.append(y_test,temp.reshape(1,1,cols*remembered),axis=0)

#y_test
np.shape(y_test)
y_test_show=np.zeros((cols*remembered,test_num+1))
test_num=test_num+1
for i in range(cols*remembered):
    for j in range(test_num):
        y_test_show[i,j]=y_test[j,0,i]
y_temp , v = bcn(x_test[0]) 
y_pred=y_temp.numpy().reshape(1,remembered*cols)
i=1
while i < test_num:
    y_pred_temp , v = bcn(x_test[i]) 
    y_pred=np.append(y_pred,y_pred_temp.numpy().reshape(1,remembered*cols),0)
    i=i+1
    
y_pred=y_pred.T
np.shape(y_pred)
np.shape(v)<jupyter_output><empty_output><jupyter_text>Plot Data Remembering<jupyter_code>%matplotlib notebook 
copy = np.full(y_pred.shape, np.nan)
rem1 = np.full(y_pred.shape, np.nan)
copy[:cols,:] = y_pred[:cols,:]
rem1[cols:,:] = y_pred[cols:,:]
plt.imshow(copy,cmap='Greys')
plt.colorbar()
plt.imshow(rem1,cmap='Blues')
plt.colorbar()
plt.savefig('Test.png')
%matplotlib notebook 
copy = np.full(y_test_show.shape, np.nan)
rem1 = np.full(y_test_show.shape, np.nan)
copy[:cols,:] = y_test_show[:cols,:]
rem1[cols:,:] = y_test_show[cols:,:]
plt.imshow(copy,cmap='Greys')
plt.colorbar()
plt.imshow(rem1,cmap='Blues')
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Interface Vector Implimentiont<jupyter_code>v[0][0][0] #how work with vector
bit=np.zeros((interface_vactor_dim,2*test_num))
for i in range(2*test_num):
    for j in range(interface_vactor_dim):
        bit[j,i]=v[i][0][j]
plt.imshow(bit,cmap='Greys')
plt.colorbar()
%matplotlib notebook
write = np.full(bit.shape, np.nan)
read = np.full(bit.shape, np.nan)
read[:,::2] = bit[:,::2]
write[:,1::2] = bit[:,1::2]
plt.imshow(read,cmap='Reds')
plt.colorbar()
plt.imshow(write,cmap='Greens')
plt.colorbar()

read_sep=np.zeros(shape=(interface_vactor_dim,test_num))
write_sep=np.zeros(shape=(interface_vactor_dim,test_num))
read_sep[:,::]=bit[:,::2]
write_sep[:,::]=bit[:,1::2]<jupyter_output><empty_output><jupyter_text>Read Vectro from CNET to Use in current step<jupyter_code>%matplotlib notebook
plt.imshow(read_sep,cmap='Reds')
plt.colorbar()
plt.xlabel("steps from BRN to FFN")
plt.ylabel("Interface Vector Nodes")
plt.savefig('Read_steps.png')<jupyter_output><empty_output><jupyter_text>Write Vector to CNET for Next step<jupyter_code>%matplotlib notebook
plt.imshow(write_sep,cmap='Greens')
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Model Summery<jupyter_code>bcn.summary()<jupyter_output>Model: "bcn"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dcnw_controller (Dense)      multiple                  181760    
_________________________________________________________________
dcnw_controller (Dense)      multiple                  525312    
_________________________________________________________________
dcnw_controller (Dense)      multiple                  524800    
_________________________________________________________________
dcnw_controller (Dense)      multiple                  195966    
=================================================================
Total params: 1,573,762
Trainable params: 1,427,838
Non-trainable params: 145,924
_________________________________________________________________
StarcoderdataJupyterStructuredCleanDedup