def AND(C, B): # Local Variables: UB0, varargout, numRankSigma, nout, k, Ux, UC0, Wt, tol, C, B, numRankB, numRankC, UtC, Vx, Wgk, W, Sigma, UtB, dim, Sx, CandB, SC, SB, dSB, dSC, UC, UB # Function calls: AND, eye, diag, svd, nargout, inv, pinv, max, sum, size dim = matcompat.size(C, 1.0) tol = 1e-14 [UC, SC, UtC] = plt.svd(C) [UB, SB, UtB] = plt.svd(B) dSC = np.diag(SC) dSB = np.diag(SB) numRankC = np.sum(np.dot(1.0, dSC > tol)) numRankB = np.sum(np.dot(1.0, dSB > tol)) UC0 = UC[:, int(numRankC + 1.0) - 1 :] UB0 = UB[:, int(numRankB + 1.0) - 1 :] [W, Sigma, Wt] = plt.svd((np.dot(UC0, UC0.conj().T) + np.dot(UB0, UB0.conj().T))) numRankSigma = np.sum(np.dot(1.0, np.diag(Sigma) > tol)) Wgk = W[:, int(numRankSigma + 1.0) - 1 :] CandB = np.dot( np.dot( Wgk, linalg.inv(np.dot(np.dot(Wgk.conj().T, linalg.pinv(C, tol) + linalg.pinv(B, tol) - np.eye(dim)), Wgk)) ), Wgk.conj().T, ) nout = matcompat.max(nargout, 1.0) - 1.0 if nout > 0.0: [Ux, Sx, Vx] = plt.svd(CandB) for k in np.arange(1.0, (nout) + 1): if k == 1.0: varargout[int(k) - 1] = cellarray(np.hstack((Ux))) elif k == 2.0: varargout[int(k) - 1] = cellarray(np.hstack((Sx))) return [CandB, varargout]
def AND(C, B): # Local Variables: UB0, varargout, numRankSigma, nout, k, Ux, UC0, Wt, tol, C, B, numRankB, numRankC, UtC, Vx, Wgk, W, Sigma, UtB, dim, Sx, CandB, SC, SB, dSB, dSC, UC, UB # Function calls: AND, eye, diag, svd, nargout, inv, pinv, max, sum, size dim = matcompat.size(C, 1.) tol = 1e-14 [UC, SC, UtC] = plt.svd(C) [UB, SB, UtB] = plt.svd(B) dSC = np.diag(SC) dSB = np.diag(SB) numRankC = np.sum(np.dot(1.0, dSC > tol)) numRankB = np.sum(np.dot(1.0, dSB > tol)) UC0 = UC[:, int(numRankC + 1.) - 1:] UB0 = UB[:, int(numRankB + 1.) - 1:] [W, Sigma, Wt] = plt.svd((np.dot(UC0, UC0.conj().T) + np.dot(UB0, UB0.conj().T))) numRankSigma = np.sum(np.dot(1.0, np.diag(Sigma) > tol)) Wgk = W[:, int(numRankSigma + 1.) - 1:] CandB = np.dot( np.dot( Wgk, linalg.inv( np.dot( np.dot( Wgk.conj().T, linalg.pinv(C, tol) + linalg.pinv(B, tol) - np.eye(dim)), Wgk))), Wgk.conj().T) nout = matcompat.max(nargout, 1.) - 1. if nout > 0.: [Ux, Sx, Vx] = plt.svd(CandB) for k in np.arange(1., (nout) + 1): if k == 1.: varargout[int(k) - 1] = cellarray(np.hstack((Ux))) elif k == 2.: varargout[int(k) - 1] = cellarray(np.hstack((Sx))) return [CandB, varargout]
pPL.cell[0,int(p)-1] = pCollector[0,0:signalPlotLength] R = matdiv(np.dot(xOldCollector, xOldCollector.conj().T), learnLength+1.) Cnative = np.dot(R, linalg.inv((R+I))) nativeCs.cell[0,int(p)-1] = Cnative startxs[:,int(p)-1] = x #% compute D increment Dtargs = np.dot(Win, pCollector) F = NOT(Call) Dargs = xOldCollector Dinc = matdiv(np.dot(np.dot(linalg.pinv((matdiv(np.dot(Dargs, Dargs.conj().T), learnLength)+np.dot(matixpower(aperture, -2.), I))), Dargs), Dtargs.conj().T), learnLength).conj().T #% update D and Call D = D+Dinc Cap = PHI(Cnative, aperture) Call = OR(Call, Cap) Calls.cell[0,int(p)-1] = Call [Ux, Sx, Vx] = plt.svd(Call) sizesCall[0,int(p)-1] = np.mean(np.diag(Sx)) [Ux, Sx, Vx] = plt.svd(Cap) sizesCap[0,int(p)-1] = np.mean(np.diag(Sx)) elif learnType == 2.: #% extension exploiting redundancies patternCollectors = cell(1., Np) pPL = cell(1., Np) sizesCall = np.zeros(1., Np) sizesCap = np.zeros(1., Np) Calls = cell(1., Np) startxs = np.zeros(Netsize, Np) Call = np.zeros(Netsize, Netsize) D = np.zeros(Netsize, Netsize) nativeCs = cell(1., Np)
for n in np.arange(1., (washoutLength + learnLength) + 1): u = patt[int(n) - 1] #% pattern input xOld = x x = np.tanh((np.dot(Wstar, x) + np.dot(Win, u) + Wbias)) if n > washoutLength: xCollector[:, int((n - washoutLength)) - 1] = x xOldCollector[:, int((n - washoutLength)) - 1] = xOld pCollector[0, int((n - washoutLength)) - 1] = u xCollectorCentered = xCollector - matcompat.repmat(np.mean(xCollector, 2.), 1., learnLength) xCollectorsCentered.cell[0, int(p) - 1] = xCollectorCentered xCollectors.cell[0, int(p) - 1] = xCollector R = matdiv(np.dot(xCollector, xCollector.conj().T), learnLength) [Ux, Sx, Vx] = plt.svd(R) SRCollectors.cell[0, int(p) - 1] = Sx URCollectors.cell[0, int(p) - 1] = Ux patternRs.cell[int(p) - 1] = R startXs[:, int(p) - 1] = x train_xPL.cell[0, int(p) - 1] = xCollector[0:5., 0:signalPlotLength] train_pPL.cell[0, int(p) - 1] = pCollector[0, 0:signalPlotLength] patternCollectors.cell[0, int(p) - 1] = pCollector allTrainArgs[:, int(np.dot(p - 1., learnLength) + 1.) - 1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:, int(np.dot(p - 1., learnLength) + 1.) - 1:np.dot(p, learnLength)] = xOldCollector allTrainOuts[0, int(np.dot(p - 1., learnLength) + 1.) -
u = patt[int(n)-1] #% pattern input xOld = x x = np.tanh((np.dot(Wstar, x)+np.dot(Win, u)+Wbias)) if n > washoutLength: xCollector[:,int((n-washoutLength))-1] = x xOldCollector[:,int((n-washoutLength))-1] = xOld pCollector[0,int((n-washoutLength))-1] = u xCollectorCentered = xCollector-matcompat.repmat(np.mean(xCollector, 2.), 1., learnLength) xCollectorsCentered.cell[0,int(p)-1] = xCollectorCentered xCollectors.cell[0,int(p)-1] = xCollector R = matdiv(np.dot(xCollector, xCollector.conj().T), learnLength) [Ux, Sx, Vx] = plt.svd(R) SRCollectors.cell[0,int(p)-1] = Sx URCollectors.cell[0,int(p)-1] = Ux patternRs.cell[int(p)-1] = R startXs[:,int(p)-1] = x train_xPL.cell[0,int(p)-1] = xCollector[:,0:signalPlotLength] train_pPL.cell[0,int(p)-1] = pCollector[0,0:signalPlotLength] patternCollectors.cell[0,int(p)-1] = pCollector allTrainArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xOldCollector allTrainOuts[0,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = pCollector allTrainTargs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = np.dot(Win, pCollector) #%%% compute readout Wout = np.dot(np.dot(linalg.inv((np.dot(allTrainArgs, allTrainArgs.conj().T)+np.dot(TychonovAlphaReadout, np.eye(Netsize)))), allTrainArgs), allTrainOuts.conj().T).conj().T #% training error
u = patt[int(n)-1] #% pattern input xOld = x x = np.tanh((np.dot(Wstar, x)+np.dot(Win, u)+Wbias)) if n > washoutLength: xCollector[:,int((n-washoutLength))-1] = x xOldCollector[:,int((n-washoutLength))-1] = xOld pCollector[0,int((n-washoutLength))-1] = u xCollectorCentered = xCollector-matcompat.repmat(np.mean(xCollector, 2.), 1., learnLength) xCollectorsCentered.cell[0,int(p)-1] = xCollectorCentered xCollectors.cell[0,int(p)-1] = xCollector R = matdiv(np.dot(xCollector, xCollector.conj().T), learnLength) [Ux, Sx, Vx] = plt.svd(R) SRCollectors.cell[0,int(p)-1] = Sx URCollectors.cell[0,int(p)-1] = Ux patternRs.cell[int(p)-1] = R startXs[:,int(p)-1] = x train_xPL.cell[0,int(p)-1] = xCollector[:,0:signalPlotLength] train_pPL.cell[0,int(p)-1] = pCollector[0,0:signalPlotLength] patternCollectors.cell[0,int(p)-1] = pCollector allTrainArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xOldCollector allTrainOuts[0,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = pCollector allTrainTargs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = np.dot(Win, pCollector) #%%% compute readout Wout = np.dot(np.dot(linalg.inv((np.dot(allTrainArgs, allTrainArgs.conj().T)+np.dot(TychonovAlphaReadout, np.eye(Netsize)))), allTrainArgs), allTrainOuts.conj().T).conj().T #% training error
for n in np.arange(1., (washoutLength + learnLength) + 1): u = patt[int(n) - 1] #% pattern input xOld = x x = np.tanh((np.dot(Wstar, x) + np.dot(Win, u) + Wbias)) if n > washoutLength: xCollector[:, int((n - washoutLength)) - 1] = x xOldCollector[:, int((n - washoutLength)) - 1] = xOld pCollector[0, int((n - washoutLength)) - 1] = u xCollectorCentered = xCollector - matcompat.repmat(np.mean(xCollector, 2.), 1., learnLength) xCollectorsCentered.cell[0, int(p) - 1] = xCollectorCentered xCollectors.cell[0, int(p) - 1] = xCollector R = matdiv(np.dot(xCollector, xCollector.conj().T), learnLength) [Ux, Sx, Vx] = plt.svd(R) SRCollectors.cell[0, int(p) - 1] = Sx URCollectors.cell[0, int(p) - 1] = Ux patternRs.cell[int(p) - 1] = R startXs[:, int(p) - 1] = x train_xPL.cell[0, int(p) - 1] = xCollector[0:5., 0:signalPlotLength] train_pPL.cell[0, int(p) - 1] = pCollector[0, 0:signalPlotLength] patternCollectors.cell[0, int(p) - 1] = pCollector allTrainArgs[:, int(np.dot(p - 1., learnLength) + 1.) - 1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:, int(np.dot(p - 1., learnLength) + 1.) - 1:np.dot(p, learnLength)] = xOldCollector allTrainOuts[0, int(np.dot(p - 1., learnLength) + 1.) -
xOldCollector[:,int((n-washoutLength))-1] = xOld if p == 2. or p == 3.: #% the Lorenz and MG observers are rescaled to [0 1] #% the other two are already in that range pCollector[0,int((n-washoutLength))-1] = np.dot(0.5, u[0]+1.) else: pCollector[0,int((n-washoutLength))-1] = u[0] xCollectorCentered = xCollector-matcompat.repmat(np.mean(xCollector, 2.), 1., learnLength) xCollectorsCentered.cell[0,int(p)-1] = xCollectorCentered xCollectors.cell[0,int(p)-1] = xCollector [Ux, Sx, Vx] = plt.svd(matdiv(np.dot(xCollector, xCollector.conj().T), learnLength)) startXs[:,int(p)-1] = x diagSx = np.diag(Sx) #%diagSx(diagSx < 1e-6,1) = zeros(sum(diagSx < 1e-6),1); R = np.dot(np.dot(Ux, np.diag(diagSx)), Ux.conj().T) patternRs.cell[int(p)-1] = R patternCollectors.cell[0,int(p)-1] = pCollector allTrainArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xOldCollector allTrainOuts[0,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = pCollector #%%% compute readout Wout = np.dot(np.dot(linalg.inv((np.dot(allTrainArgs, allTrainArgs.conj().T)+np.dot(TychonovAlphaReadout, np.eye(Netsize)))), allTrainArgs), allTrainOuts.conj().T).conj().T #% training error NRMSE_readout = nrmse(np.dot(Wout, allTrainArgs), allTrainOuts) np.disp(sprintf('NRMSE readout: %g', NRMSE_readout))
u = patt[int(n)-1] #% pattern input xOld = x x = np.tanh((np.dot(Wstar, x)+np.dot(Win, u)+Wbias)) if n > washoutLength: xCollector[:,int((n-washoutLength))-1] = x xOldCollector[:,int((n-washoutLength))-1] = xOld pCollector[0,int((n-washoutLength))-1] = u xCollectorCentered = xCollector-matcompat.repmat(np.mean(xCollector, 2.), 1., learnLength) xCollectorsCentered.cell[0,int(p)-1] = xCollectorCentered xCollectors.cell[0,int(p)-1] = xCollector R = matdiv(np.dot(xCollector, xCollector.conj().T), learnLength) [Ux, Sx, Vx] = plt.svd(R) SRCollectors.cell[0,int(p)-1] = Sx URCollectors.cell[0,int(p)-1] = Ux patternRs.cell[int(p)-1] = R startXs[:,int(p)-1] = x train_xPL.cell[0,int(p)-1] = xCollector[0:5.,0:signalPlotLength] train_pPL.cell[0,int(p)-1] = pCollector[0,0:signalPlotLength] patternCollectors.cell[0,int(p)-1] = pCollector allTrainArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xOldCollector allTrainOuts[0,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = pCollector allTrainTargs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = np.dot(Win, pCollector) #%%% compute readout Wout = np.dot(np.dot(linalg.inv((np.dot(allTrainArgs, allTrainArgs.conj().T)+np.dot(TychonovAlphaReadout, np.eye(Netsize)))), allTrainArgs), allTrainOuts.conj().T).conj().T #% training error
u = patt[int(n)-1] #% pattern input xOld = x x = np.tanh((np.dot(Wstar, x)+np.dot(Win, u)+Wbias)) if n > washoutLength: xCollector[:,int((n-washoutLength))-1] = x xOldCollector[:,int((n-washoutLength))-1] = xOld pCollector[0,int((n-washoutLength))-1] = u xCollectorCentered = xCollector-matcompat.repmat(np.mean(xCollector, 2.), 1., learnLength) xCollectorsCentered.cell[0,int(p)-1] = xCollectorCentered xCollectors.cell[0,int(p)-1] = xCollector R = matdiv(np.dot(xCollector, xCollector.conj().T), learnLength) [Ux, Sx, Vx] = plt.svd(R) SRCollectors.cell[0,int(p)-1] = Sx URCollectors.cell[0,int(p)-1] = Ux patternRs.cell[int(p)-1] = R startXs[:,int(p)-1] = x train_xPL.cell[0,int(p)-1] = xCollector[0:5.,0:signalPlotLength] train_pPL.cell[0,int(p)-1] = pCollector[0,0:signalPlotLength] patternCollectors.cell[0,int(p)-1] = pCollector allTrainArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xOldCollector allTrainOuts[0,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = pCollector allTrainTargs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = np.dot(Win, pCollector) #%%% compute readout Wout = np.dot(np.dot(linalg.inv((np.dot(allTrainArgs, allTrainArgs.conj().T)+np.dot(TychonovAlphaReadout, np.eye(Netsize)))), allTrainArgs), allTrainOuts.conj().T).conj().T #% training error