def singleLocusSigmoid(): x0=0.001;s=0.5;maxGen=100 times_ = T.ivector("times"); x0_ = T.scalar("x0 ") S__=theano.shared(np.asarray(s, dtype = theano.config.floatX), 'S') pred_= sig_(0.5*S__*times_ + logit_(x0_)) Feedforward_ = theano.function(inputs=[x0_,times_], outputs=pred_) pd.Series(Feedforward_(x0,range(maxGen))).plot(); pd.Series(sig(0.5*s*np.arange(maxGen)+logit(x0))).plot()
def singleMultiLocusHafVariableTime(): from popgen.TimeSeries.RNN.Utils import Z x0=0.001;s=0.5;maxGen=100;numReplicates=3;times=np.tile(np.arange(maxGen+1),(numReplicates,1)).T.astype(np.float32) initC0 = np.arange(numReplicates,dtype=np.float32)*logit(x0) c__=theano.shared(initC0, 'c') # c__=theano.shared(np.asarray(logit(x0), dtype = theano.config.floatX), 'S') times_ = T.fmatrix();rep_ = T.iscalar();maxGenerations_ = T.ivector() target_ = (T.matrix(),T.vector())[numReplicates==1] S__=theano.shared(np.asarray(s, dtype = np.float32), 'S') loss_= (target_[:maxGenerations_[rep_],rep_] - Z(T.nnet.sigmoid(0.5*S__*times_[:maxGenerations_[rep_],rep_] +c__[rep_]),200,20))**2 reps_=T.ivector() cost_, _ = theano.scan(lambda rep: (target_[:maxGenerations_[rep],rep] - times_[:maxGenerations_[rep],rep]).sum() , sequences=reps_); cost_=cost_.sum() Loss_ = theano.function(inputs=[target_, times_, maxGenerations_, reps_], outputs=[results]) times target=times+1 target[:,1]+=1 target[:,2]+=1 target lastGenerationIndex=[10,20,30] reps=range(numReplicates) Loss_(target,times,lastGenerationIndex,reps) target import popgen.TimeSeries.RNN.MultiLocusHAFOptimizingAllVarsVariableTimeOld as RNN numReplicates=3 ; s=0.02;generationStep=100 sim = Simulation.Simulation(numReplicates=numReplicates, s=s, generationStep=generationStep); sim.forwardSimulation(); sim.getAverageHAF().plot() sim.getAverageHAF().diff().plot() y=sim.getAverageHAF();times=sim.getGenerationTimes() replicateIndex=range(sim.numReplicates) reload(RNN) n=2000;theta=20 lastGenerationIndex=sim.filterTimeSamplesWithHighNegDer() rnn=RNN.MultiLocusHAFOptimizingAllVarsVariableTimeOld(sim.X0.min(),lastGenerationIndex=lastGenerationIndex, initS=sim.s, initTheta=theta, times=sim.getGenerationTimes(), numReplicates=sim.numReplicates,initSviaLineSearch=False) rnn.Loss_(y.values,np.tile(times,(3,1)).T.astype(np.float32),n,list(lastGenerationIndex),replicateIndex) Z(sig(0.5*times*sim.s + logit(sim.X0.min())),n,theta) i=10 j=0 for j,i in enumerate(lastGenerationIndex): print ((Z(sig(0.5*times*sim.s + logit(sim.X0.min())),n,theta)[:i] - y.values[:i,j])**2).sum() print lastGenerationIndex
def singleLocusSigmoidX0(): x0=0.001;s=0.5;maxGen=100;numReplicates=3;times=np.arange(maxGen) initC0 = np.ones(numReplicates,dtype=np.float32)*logit(x0) c__=theano.shared(initC0, 'c') # c__=theano.shared(np.asarray(logit(x0), dtype = theano.config.floatX), 'S') times_ = T.fmatrix("times"); numReplicates_ = T.scalar("numReplicates ") S__=theano.shared(np.asarray(s, dtype = np.float32), 'S') pred_= T.nnet.sigmoid(0.5*S__*times_ +c__) Feedforward_ = theano.function(inputs=[times_], outputs=pred_) tt=np.tile(times,(3,1)).T.astype(np.float32) Feedforward_(tt)
def singleMultiLocusHaf(): from popgen.TimeSeries.RNN.Utils import Z x0=0.001;s=0.5;maxGen=100;numReplicates=3;times=np.arange(maxGen) initC0 = np.ones(numReplicates,dtype=np.float32)*logit(x0) c__=theano.shared(initC0, 'c') # c__=theano.shared(np.asarray(logit(x0), dtype = theano.config.floatX), 'S') times_ = T.fmatrix("times"); numReplicates_ = T.scalar("numReplicates ") S__=theano.shared(np.asarray(s, dtype = np.float32), 'S') pred_= Z(T.nnet.sigmoid(0.5*S__*times_ +c__),200,20) Feedforward_ = theano.function(inputs=[times_], outputs=pred_) tt=np.tile(times,(3,1)).T.astype(np.float32) plt.plot(Feedforward_(tt)[:,0])
def Nu0(s,t,nu0,n,theta): return Z(sig(np.array(t)*s/2 +logit(nu0)),n,theta) initNu0=0.005; final_momentum=0.9; initial_momentum=0.5;momentum_switchover=5;times=range(1,801);S=3;lr=1e-2;maxIter=10000;initS=0.05; initTheta= 20; numReplicates=3;n=2000
def Nu(s,t,nu0): return np.array([sig(t*s/2 +logit(nu)) for nu in nu0]).T def Nu0(s,t,nu0,n,theta): return Z(sig(np.array(t)*s/2 +logit(nu0)),n,theta)