Example #1
0
    def __init__(self, inputnodes, hiddennotes, outputnodes, learningrate):
        self.inodes = inputnodes
        self.hnotes = hiddennotes
        self.onodes = outputnodes

        self.wih = numpy.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))
        self.who = numpy.normal(0.0,pow(self.hnodes,-0.5),(self.onodes,self.hnodes))

        self.lr = learningrate

        self.activation_function = lambda(x: scipy.special.expit(x))

        pass
Example #2
0
def estiamcion():
    for i in range(2000):
        Rnew = np.normal(R_walk[i], 0.1)
        Cnew = np.normal(C_walk[i], 0.1)
        if likelihood(y_data, modelo(x_data, Rnew, Cnew)) / likelihood(
                y_data, modelo(x_data, R_walk[i], C_walk[i])) >= 1:
            R_walk = np.append(R_walk, Rnew)
            C_walk = np.append(C_walk, Cnew)
            if l_walk[i] < likelihood(y_data, modelo(x_data, Rnew, Cnew)):
                lmax = likelihood(y_data, modelo(x_data, Rnew, Cnew))
                rmax = Rnew
                cmax = Cnew
        elif likelihood(y_data, modelo(x_data, Rnew, Cnew)) / likelihood(
                y_data, modelo(x_data, R_walk[i],
                               C_walk[i])) > np.random.random():
            R_walk = np.append(R_walk, Rnew)
            C_walk = np.append(C_walk, Cnew)
        else:
            R_walk = np.append(R_walk, R_walk[i])
            C_walk = np.append(C_walk, C_walk[i])
Example #3
0
I_PlantAvWatSub = [np.zeros(Subcatchment) for i in range(0, 4)]
I_PlantAvWatSubNow = FracProcess(I_PlantAvWatSub, I_Flag1, I_Flag2, I_Simulation_Time , I_InputDataYears)
I_TopSoilBD_BDRef = [np.zeros(Subcatchment) for i in range(0, 4)]
I_BD_BDRefVegNow = FracProcess(I_TopSoilBD_BDRef, I_Flag1, I_Flag2, I_Simulation_Time , I_InputDataYears)

# I_Rainfall
I_Warmedup = 1 if I_WarmEdUp[-1] == I_Simulation_Time  else 0
update(I_WarmEdUp, I_Warmedup, dt)
I_CaDOYStart = 0
I_DailyRain = I_DailyRainYear[I_Simulation_Time ] * I_RainMultiplier
I_DailyRainAmount = np.multiply(np.multiply(I_RainPerday, I_FracVegClassNow), I_RelArea)
I_RainCycle = 0
I_RainDoY = I_Simulation_Time  if I_RainCycle == 0 else 1 + (I_Simulation_Time  % 365)
I_RainDuration = ((I_RainPerDay/I_Rain_IntensMean) *
                  min(max(0,1 - 3 * I_Rain_IntensCoefVar,
                          np.normal(1, I_Rain_IntensCoefVar, I_Rain_GenSeed + 11250)),
                      1 + 3 * I_Rain_IntensCoefVar))
I_RainPerDay = I_SpatRainTime if I_UseSpatVarRain == 1 else I_DailyRain

I_Simulation_Time = TIME + I_CaDOYStart + 365 * I_RainYearStart - I_WarmEdUp * (I_WarmUpTime + 1)
I_SpatRainTime = I_SpatRain[TIME]
I_StillWarmUp = 1 if time <= I_WarmUpTime + 1 else 0
I_UseSpatVarRain = 0
I_WarmUpTime = 730
I_WUCorrection = 1 if time == (I_WarmUpTime + 1) else 0

# I SubcatchmParam
if I_SoilPropConst == 1:
    I_AvailWaterClass = I_AvailWaterConst * np.multiply(I_FracVegClassNow, I_RelArea)
else:
    I_AvailWaterClass = np.multiply(np.multiply(I_AvailWatClassNow, I_FracVegClassNow), I_RelArea)
def train_network(nhidden=3, scale=0.3, learningrate=0.1, rseed=42424242):
    """
        How to setup and evaluate a Boltzmann machine. Please note that in
        order to instantiate BMs all needed neuron parameters need to be in the
        database and calibrated.

        Does the same thing as sbs.tools.sample_network(...).
    """

    targetdist = {
        tuple(int(i) for i in '{:04b}'.format(int(k))): v
        for k, v in targetstates.items()
    }
    duration = 1e5

    sampler_config = sbs.db.SamplerConfiguration.load(
        "tutorial_calibration.json")

    # @Stefanie: num_samplers ist die Gesamtzahl der Neuronen (i.e. hidden + visible)
    nvisible = 4
    nunits = nvisible + nhidden
    bm = sbs.network.ThoroughBM(num_samplers=nunits,
                                sim_name=sim_name,
                                sampler_config=sampler_config)

    weights = np.zeros((nunits, nunits))
    weights[nvisible:, :nvisible] = np.normal(0., scale, (nhidden, nvisible))
    weights += weights.T
    bm.weights_theo = weights

    # Set random biases.
    bm.biases_theo = np.normal(0., scale, nunits)

    bm.saturating_synapses_enabled = True
    bm.use_proper_tso = True

    for iternumber in range(10000):
        # Korrelationen fuer CD-Training
        wcorrelations = np.zeros((nvisible, nhidden))
        bcorrelations = np.zeros(bm.num_samplers)

        for state, p in targetdist.items():
            sampleprobs = get_samples(bm, 1000. * (np.array(state) - .5))
            wcorrelations += p * get_wcorrelations(sampleprobs,
                                                   bm.num_samplers - 4)
            bcorrelations += p * get_bcorrelations(sampleprobs,
                                                   bm.num_samplers - 4)
        sampleprobs = get_samples(bm, np.zeros(4))

        wcorrelations -= get_wcorrelations(sampleprobs, bm.num_samplers - 4)
        bcorrelations -= get_bcorrelations(sampleprobs, bm.num_samplers - 4)

        visibleprobs = defaultdict(float)
        for state, p in sampleprobs.items():
            visibleprobs[tuple(int(i) for i in state[:4])] += p

        (mag1z, mag2z, mag1x, mag2x, corrzz, corrxx) = evaluate(visibleprobs)
        vp = [visibleprobs[k] for k in targetdist.keys()]
        tp = [targetdist[k] for k in targetdist.keys()]
        print('\n\n\n\n\n\n\n\n\n\n')
        print(vp, tp)
        print(dkl(tp, vp))
        with open('dkls_{}.txt'.format(name), 'a') as f:
            f.write('{}\n'.format(dkl(tp, vp)))
        np.savetxt('weights_{}_{:05d}.txt'.format(name, iternumber),
                   bm.weights_theo)
        np.savetxt('bias_{}_{:05d}.txt'.format(name, iternumber),
                   bm.weights_theo)
        with open('mag1z_{}.txt'.format(name), 'a') as f:
            f.write('{}\n'.format(mag1z))
        with open('mag2z_{}.txt'.format(name), 'a') as f:
            f.write('{}\n'.format(mag2z))
        with open('mag1x_{}.txt'.format(name), 'a') as f:
            f.write('{}\n'.format(mag1x))
        with open('mag2x_{}.txt'.format(name), 'a') as f:
            f.write('{}\n'.format(mag2x))
        with open('corrzz_{}.txt'.format(name), 'a') as f:
            f.write('{}\n'.format(corrzz))
        with open('corrxx_{}.txt'.format(name), 'a') as f:
            f.write('{}\n'.format(corrxx))

        # Lernrate (muss moeglicherweise angepasst werden)
        bm.weights_theo[:4, 4:] += 0.01 * wcorrelations
        bm.weights_theo[4:, :4] += 0.01 * wcorrelations.T
        bm.biases_theo += 0.01 * bcorrelations
Example #5
0
def gen_dist(dist_str, params):
    if dist_str == 'normal':
        return np.normal(**params)
Example #6
0
    def StimulusMaker(self, mode='tone', amp=1, freq=(1000, 3000, 4000), delay=0, duration=2000,
                  rf=2.5, phase0=0, samplefreq=44100, ipi=20, nPresent=1,
                  alternate=1, level=70,
                  playSignal=False, plotSignal=False, channel=0):
# generate a tsound (tone, bb noise, bpnoise)  pip with amplitude (V), frequency (Hz) (or frequencies, using a tuple)
# delay (msec), duration (msec).
# if no rf (risefall) time is given (units, msec), cosine^2 shaping with 5 msec ramp duration is applied.
# if no phase is given, phase starts on 0, with positive slope.
# level is in dB SPL as given by the reference calibration data above...
#
        clock = 1000.0/samplefreq # calculate the sample clock rate, and convert to points per msec (khz)
#        uclock = 1000.*clock # microsecond clock
#        phi = 2*np.pi*phase0/360.0 # convert phase from degrees to radians...
        Fs = 1000./clock
        # phi = 0. # actually, always 0 phase for start
        w = []
        fil = self.rfShape(0, duration, samplefreq, rf) # make the shape filter with 0 delay
        jd = int(np.floor(delay/clock)) # beginning of signal buildup (delay time)
        if jd < 0:
            jd = 0
        jpts = np.arange(0,len(fil))
        signal = np.zeros(len(jpts))
        siglen = len(signal)
 
        if mode =='tone':
            for i in range(0, len(freq)):
                signal = signal + fil*amp*np.sin(2*np.pi*freq[i]*jpts/Fs)
                if self.debugFlag:
                    print "Generated Tone at %7.1fHz" % (freq[i])
                
        if mode == 'bbnoise':
            signal = signal + fil*amp*np.normal(0,1,siglen)
            if self.debugFlag:
                print "BroadBand Noise " 
            
        if mode == 'bpnoise':
            tsignal = fil*amp*np.normal(0,1,siglen)
            # use freq[0] and freq[1] to set bandpass on the noise
            if self.debugFlag:
                print "freqs: HP: %6.1f    LP: %6.1f" % (freq[0], freq[1])
            sf2 = samplefreq*2.0 # nyquist limit
            if freq[0] > sf2 or freq[1] > sf2:
                print 'freqs: ', freq
                print 'nyquist limit: ', sf2
                print 'sample frequ: ', samplefreq
                print 'coefficients not bounded [0, 1] for w... '
                return np.array(signal)
            wp = [float(freq[0])/sf2, float(freq[1])/sf2]
            ws = [0.75*float(freq[0])/sf2, 1.25*float(freq[1])/sf2]
            (filter_b,filter_a) = scipy.signal.iirdesign(wp, ws,
                    gpass=2.0,
                    gstop=60.0,
                    ftype="ellip")
            if self.debugFlag:
                print "BandPass Noise %7.1f-%7.1f" % (freq[0], freq[1])
            signal=scipy.signal.lfilter(filter_b, filter_a, tsignal)
        
        if mode == 'notchnoise':
            return np.array(signal)
            
        if mode == 'multitones':
            return np.array(signal)

        if mode == 'silence':
            return np.array(signal)
 
# now build the waveform from the components
        w = np.zeros(int(np.ceil(ipi*(nPresent-1)/clock)+jd+siglen))
        sign = np.ones(nPresent)
        if alternate == True:
            sign[range(1,nPresent,2)] = -1
        id = int(np.floor(ipi/clock))
        for i in range(0, nPresent): # for each pulse in the waveform
            j0 = jd + i*id # compute start time  
            w[range(j0,j0+siglen)] = sign[i]*signal
        
        w = w*self.dbconvert(spl=level, chan=channel) # aftera all the shaping ane scaling, we convert to generate a signal of w dB
        if playSignal == True:
            self.playSound(w, w, samplefreq)
        
#        if plotSignal == True:
#            self.plotSignal(w, w, clock)
        return np.array(w)
Example #7
0
__author__ = 'Ibis'

import numpy as np

mu, sigma = 0, 0.1 # mean and standard deviation
s = np.normal(mu, sigma)
s = np.

print(s)