<jupyter_start><jupyter_text>Test Six for the Classical SRNN This is a notebook for testing the classical SRNN.  Import everything Modify setting for pytorch<jupyter_code>import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
currentPath=os.getcwd()<jupyter_output><empty_output><jupyter_text>Import matplotlib and others<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import torch<jupyter_output><empty_output><jupyter_text>Import the classical SRNN and others<jupyter_code>#Modify path for the notebooks
currentPath=os.path.join(currentPath,'..')
currentPath=os.path.join(currentPath,'src')
os.chdir(currentPath)
from DataGenerator.HenonMapDataGen import HenonMapDataGen
from ClassicalModels.ClassicalSRNNs import ClassicalSRNN
from ClassicalModels.ClassicalSRNNs import SuportFunction
from GradientFreeOptimizers.CostFunc import GradFreeMSELoss
from GradientFreeOptimizers.Optimizers import MCSOptimizer
import GradientFreeOptimizers.Helpers as hp<jupyter_output><empty_output><jupyter_text>Test Six  Get the data  Set save path<jupyter_code>savepath=os.path.join(currentPath,'..\data\HenonMap\Test')
filename='ClassicalSRNNTest1.csv'
'''
hmap=HenonMapDataGen(savepath=savepath)
hmap(10000)
hmap.save_to_CSV(filename)
''';<jupyter_output><empty_output><jupyter_text>Read the data<jupyter_code>hmap=HenonMapDataGen(savepath=savepath)
hmap.read_from_CSV(filename)
print(hmap)<jupyter_output>Data Info:
----------------------------------------
Data Size: 10000
Data Interval: 1
Data ParamA: 1.4, Data ParamB: 0.3
Data Bound: -1.2
Data HeavyMem: True
Data Seed:
 [0.004080481176836049, 0.06596289908127698]
----------------------------------------<jupyter_text>Generate the data iter<jupyter_code>testSetRatio=0.2
numStep=10
batchSize=4
trainIter,testIter=hmap.get_data_iter(testSetRatio,numStep,batchSize)
X,Y=next(iter(trainIter))
print('Train Data Size:',len(trainIter))
#print('X=',torch.squeeze(X))
#print('Y=',torch.squeeze(Y))
X,Y=next(iter(testIter))
print('Test Data Size:',len(testIter))
#print('X=',torch.squeeze(X))
#print('Y=',torch.squeeze(Y))<jupyter_output>Test Data Size: 199<jupyter_text>Define the SRNN  Get neccesary functions<jupyter_code>srnnTestSup=SuportFunction()
transform=lambda Xs:[torch.squeeze(x) for x in Xs]
init_rnn_state=srnnTestSup.get_init_state_fun()
get_params=srnnTestSup.get_get_params_fun()
rnn=srnnTestSup.get_forward_fn_fun()
predict_fun=srnnTestSup.get_predict_fun(outputTransoform=transform)<jupyter_output><empty_output><jupyter_text>Create the SRNN<jupyter_code>inputSize=outputSize=1
hiddenSize=6
net=ClassicalSRNN(inputSize,hiddenSize,outputSize,get_params,init_rnn_state,rnn)<jupyter_output><empty_output><jupyter_text>Test prediction<jupyter_code>state=net.begin_state(batchSize)
Y,newState=net(X,state)
Y.shape, len(newState), newState[0].shape
preX,preY=hmap.data_as_tensor
preX,preY=torch.unsqueeze(preX[:2],-1),torch.unsqueeze(preY[:10],-1)
print('preX=',preX)
preY=[y for y in torch.cat((preX[:2],preY[1:]),dim=0)]
print('preY=',preY)
preX=torch.unsqueeze(preX,-1)
YHat=predict_fun(preX,net,numPreds=5)
print('YHat=',YHat)<jupyter_output>YHat= [tensor(0.0041), tensor(0.0660), tensor(-1.3199e-05), tensor(2.8045e-08), tensor(-3.4623e-09), tensor(1.0193e-11), tensor(-1.8250e-12)]<jupyter_text>Train the network  Parameters<jupyter_code>num_epochs= 600
maxLevyStepSize=[1.0,1.0,0.1,1.0,1.0]<jupyter_output><empty_output><jupyter_text>Loss function<jupyter_code>lossFunc=GradFreeMSELoss(net)<jupyter_output><empty_output><jupyter_text>Trainer<jupyter_code>mcs=MCSOptimizer(net.params,lossFunc,trainIter,\
    maxLevyStepSize=maxLevyStepSize,randInit=True,\
    nestNum=40,epochToGeneration=lambda x:max(int(x/100),1))<jupyter_output><empty_output><jupyter_text>Initial loss<jupyter_code>l_epochs=[]
train_l=SuportFunction.evaluate_accuracy(net,trainIter,lossFunc,False)
test_l=SuportFunction.evaluate_accuracy(net,testIter,lossFunc,False)
l_epochs.append([train_l,test_l])
print('Initial Train Loss:',train_l)
print('Initial Test Loss:',test_l)<jupyter_output>Initial Train Loss: 0.4927547097206116
Initial Test Loss: 0.5304158449172973<jupyter_text>Training<jupyter_code>animator = hp.Animator(xlabel='epoch', ylabel='Loss',
                            legend=['train','test'], xlim=[10, num_epochs])
    # prediction
predict = lambda prefix: predict_fun(prefix,net, numPreds=9)
# train and predict
for epoch in range(num_epochs):
    with torch.no_grad():
        trainLoss,_ = mcs.step()
        testLoss=SuportFunction.evaluate_accuracy(net, testIter, lossFunc, False)
        if (epoch + 1) % 10 == 0:
            print(predict(preX))
            animator.add(epoch + 1, [trainLoss,testLoss])
        l_epochs.append([trainLoss,testLoss])
testLoss=SuportFunction.evaluate_accuracy(net, testIter, lossFunc, False)
print(f'TestLoss {testLoss:f}')
print('Prediction:\n',predict(preX))
print('Answer:\n',preY)
l_epochs[-1]<jupyter_output><empty_output><jupyter_text>Visualize the performance  One Step Prediction<jupyter_code>X,Y=next(iter(testIter))
state=net.begin_state(batchSize)
Y_hat,newState=net(X,state)
print('X=',torch.squeeze(X))
print('Prediction=',torch.squeeze(Y_hat).detach())
Y=Y.transpose(0,1).reshape([-1,Y.shape[-1]])
print('Y=',torch.squeeze(Y))
#Visualize the data
axes,fig=plt.subplots(1,1,figsize=(4,3))
plt.title('One-Step Prediction')
plt.plot(torch.linspace(0,Y.numel(),Y.numel()),torch.squeeze(Y),label='Y')
plt.plot(torch.linspace(0,Y.numel(),Y.numel()),torch.squeeze(Y_hat).detach(),label=r'$\hat{Y}$')
plt.legend();<jupyter_output><empty_output><jupyter_text>Multi Step Prediction<jupyter_code>prefixSize=10
totalSize=20
testShift=int(len(hmap)*(1-testSetRatio))
preX,preY=hmap.data_as_tensor
preX,preY=torch.unsqueeze(preX[testShift:testShift+prefixSize],-1),torch.unsqueeze(preY[testShift:testShift+totalSize-1],-1)
#print('preX=',preX)
preY=[y for y in torch.cat((preX[:2],preY[1:]),dim=0)]
#print('preY=',preY)
len(preY)
preX=torch.unsqueeze(preX,-1)
YHat=predict_fun(preX,net,numPreds=totalSize-prefixSize)
#print('YHat=',YHat)
len(YHat)
#Visualize the data
axes,fig=plt.subplots(1,1,figsize=(4,3))
plt.title('Multi-Step Prediction')
fig.set_ylim(-2,2)
plt.plot(torch.linspace(0,len(preY),len(preY)),preY,label='Y')
plt.plot(torch.linspace(0,len(preY),len(preY)),YHat,label=r'$\hat{Y}$')
plt.vlines([prefixSize-1],ymin=-2,ymax=2,linestyles='dashed',label='Prediction')
plt.legend();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16321122369446608, 'fraction_numerical': 0.03367108339828527, 'mean_word_length': 9.07221350078493, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '640428', 'license': 'MIT', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2555, 'n_words': 294, 'path': 'JupyterNotebooks/Test_Six_Classical_SRNN.ipynb', 'repo_name': 'yesunhuang/QRNNs_Memory', 'size': '6415'}