Example #1
0
    def __init__(self, env_name):
        self.env_name = env_name
        env = gym.make(self.env_name)
        self.action_size = env.action_space.n
        self.state_size = env.observation_space.shape
        env.close()
        self.EPISODES = 10000 # total episodes to train through all environments
        self.episode = 0 # used to track the episodes total count of episodes played through all thread environments
        self.max_average = 0 # when average score is above 0 model will be saved
        self.lr = 0.00025
        self.epochs = 10 # training epochs
        self.shuffle=False
        self.Training_batch = 1000
        self.replay_count = 0
        self.optimizer = optim.Adam
        self.writer = SummaryWriter(comment="_"+self.env_name+"_"+self.optimizer.__name__+"_"+str(self.lr))

        # Instantiate plot memory
        self.scores_, self.episodes_, self.average_ = [], [], [] # used in matplotlib plots

        # Instantiate games and plot memory
        self.eps = np.finfo(np.float32).eps.item()
        self.scores, self.episodes, self.average = [], [], []

        self.save_path = 'Models'
        if not os.path.exists(self.save_path): os.makedirs(self.save_path)
        self.actor_path = os.path.join(self.save_path, f'{self.env_name}_Actor_{self.lr}_torch.h5')
        self.critic_path = os.path.join(self.save_path, f'{self.env_name}_Critic_{self.lr}_torch.h5')

        # Create ActorCritic network model
        self.actor, self.critic = getModels(input_shape=self.state_size, action_space = self.action_size)
        self.actor_optimizer = self.optimizer(params = self.actor.parameters(), lr=self.lr)
        self.critic_optimizer = self.optimizer(params = self.critic.parameters(), lr=self.lr)
Example #2
0
def solution5():
    print("Start problem 5")
    print("Start data preparation for problem 5")
    dataPreparation_for_5()
    print("Results: ")

    model = getModels()

    for i in range(1, 11):
        for j in range(1, 4):
            filename = 'problem 5 sample' + str(i) + '_period' + str(
                j) + '.txt'
            tweets, parameters = [], []
            prediction = []
            #f = open('problem 5 sample'+str(i)+'_period'+'j'+'.txt')
            if os.path.isfile('problem 5 sample' + str(i) + '_period' +
                              str(j) + '.txt'):
                print filename, 'problem 5 sample' + str(i) + '_period' + str(
                    j) + '.txt'
                f = open('problem 5 sample' + str(i) + '_period' + str(j) +
                         '.txt')
                line = f.readline()
                while len(line):
                    p = line.split()
                    tweets.append(float(p[0]))
                    parameters.append([float(p[i]) for i in range(len(p))])
                    line = f.readline()

                    # del(tweets[0])
                    # next_hour_tweets = tweets
                    # parameters.pop()
                    #print parameters, tweets
                # X=[1,1,1,1,1]
                # y=[1]
                # model=[]
                # for i in range(3):
                #     clf=linear_model.LinearRegression()
                #     clf.fit(X,y)
                #     model.append(clf)

                clf = model[j - 1]
                prediction = clf.predict(parameters)
                #print parameters, tweets
                print "prediction of tweets in next hour of " + filename
                print prediction[-1]
                f.close()
Example #3
0
def solution5():
    print("Start problem 5")
    print("Start data preparation for problem 5")
    dataPreparation_for_5()
    print("Results: ")
    
    
    model=getModels()
    
    for i in range(1,11):
        for j in range(1,4):
            filename='problem 5 sample'+str(i)+'_period'+str(j)+'.txt'
            tweets,parameters=[],[]
            prediction=[]
            #f = open('problem 5 sample'+str(i)+'_period'+'j'+'.txt')
            if os.path.isfile('problem 5 sample'+str(i)+'_period'+str(j)+'.txt'):
                print filename,'problem 5 sample'+str(i)+'_period'+str(j)+'.txt'
                f = open('problem 5 sample'+str(i)+'_period'+str(j)+'.txt')
                line = f.readline()
                while len(line):
                    p = line.split()
                    tweets.append(float(p[0]))
                    parameters.append([float(p[i]) for i in range(len(p))])
                    line = f.readline()
    
                    # del(tweets[0])
                    # next_hour_tweets = tweets
                    # parameters.pop()
                    #print parameters, tweets
                # X=[1,1,1,1,1]
                # y=[1]
                # model=[]
                # for i in range(3):
                #     clf=linear_model.LinearRegression()
                #     clf.fit(X,y)
                #     model.append(clf)
    
                clf=model[j-1]
                prediction=clf.predict(parameters)
                #print parameters, tweets
                print "prediction of tweets in next hour of "+filename
                print prediction[-1]
                f.close()
Example #4
0
    def fitSermivariogramModel(self, modelname, nlag=15, tsill=None,
                               trange=None, tnugget=0.0):
        """Fit a semivariogram model to the datas
        Returns a model with the best parameters (sill, range, nugget) to
        fit the datas (semivariance against distance)

        Parameters
        ----------
        modelname : String
                    The desired model name to use. One of:
                    Spherical, Exponential, Gaussian, Pentaspherical, Nugget
        nlag :  Integer
                If not None, the semivariogram will be fitted using nlags
                bins (empirical semivariogram), between distance[0, max], else
                all the datas will be used (global semivariogram).
        tsill : Float
                Temporary initial sill, used by the fitted algorithm. If
                None it will be estimated from the datas.
        trange : Float
                 Temporary initial range, used by the fitted algorithm.
                 If None it will be estimated from the datas.
        tnugget : Float
                  Temporary initial nugget, used by the fitted algorithm.

        Returns
        ----------
        a model instance with sill, range and nugget computed to best fit
        the datas. Highly depending from the initial inputs, due to the used
        algorithm (scipy.optimize.leastsqr)

        """
        # the input datas
        dist = self.infos.dist
        svar = self.infos.svar

        # lag?
        if nlag is None:
            nlag = len(dist)
        if nlag < 1:
            raise ValueError, "nlag must be >=1"
        # Initials parameters estimations
        # TODO : find best solutions
        if tsill is None:
            tsill = 9/10*np.max(svar)
        if trange is None:
            trange = 0.5*np.max(dist)
        # Sort by distance
        sortind = np.argsort(dist)
        sortdist = dist[sortind]
        sortsvar = svar[sortind]
        # Select by bins
        index = sortdist.searchsorted(np.linspace(0, dist.max(), nlag+1))
        dist = [sortdist[index[i-1]:index[i]].mean() for i in range(1, len(index))]
        svar = [sortsvar[index[i-1]:index[i]].mean() for i in range(1, len(index))]

        # Retrieve the model class from his name
        model = models.getModels(sill=tsill, range=trange, nugget=tnugget)[modelname]

        # these are our inital guesses for the sill and range
        params = (model.sill, model.range, model.nugget)

        # perform the least squares
        lstsqResult = optimize.leastsq(model.residual, params, args=(dist, svar), full_output=0)

        if(lstsqResult != 1):
            p = lstsqResult[0]
            model.sill, model.range, model.nugget = p
            if model.range > 0:
                # work out the square deviation too
                squareDeviates = model.residual(p, dist, svar)**2
                # we divide by number of points minus the degrees of freedom
                denom = 1 / float(len(svar)-len(p))
                model.variance = denom*sum(squareDeviates)
                pprint([["Model Type", model.type], ["Sill", model.sill],
                ["Range", model.range], ["Nugget", model.nugget]])
                return model
            else:
                pprint([["Error", "Computed range <=0 :("],])
                return None
        else:
            pprint([["Error", "Bad fitting computation :("],])
            return None
Example #5
0
"""
	helps with stuff
"""
from sys import argv
from os import system
from models import getModels
from peewee import SqliteDatabase

if "--create" in argv:
    print "[+] Creating Tables..."
    db = SqliteDatabase("devClone.db")
    Rant, Action = getModels(db)

    db.create_tables([Rant, Action])

elif "--recreate" in argv:
    print "[+] Recreating..."
    system("rm devClone.db")
    system("python tool.py --create")
                            padding='post',
                            value=0)
decoder_out = pad_sequences(decoder_out,
                            maxlen=MAX_SEQ_LEN,
                            padding="post",
                            value=0)
print("decoder input shape", decoder_inp.shape, "decoder output shape",
      decoder_out.shape)

images = result.image_name.apply(
    lambda x: 'flickr30k_images/flickr30k_images/' + x).values
SAMPLE_SIZE = len(images)
print('SAMPLE_SIZE', SAMPLE_SIZE)

model, captionGenerator = getModels(embedding_matrix,
                                    MAX_SEQ_LEN,
                                    LATENT_DIM=100)


#custom acc because we dont want to consider padding
def acc(y_true, y_pred):
    # both are of shape ( _, Ty, VOCAB_SIZE )
    targ = K.argmax(y_true, axis=-1)
    pred = K.argmax(y_pred, axis=-1)
    correct = K.cast(K.equal(targ, pred),
                     dtype='float32')  #cast bool tensor to float

    # 0 is padding, don't include those- mask is tensor representing non-pad value
    mask = K.cast(K.greater(targ, 0),
                  dtype='float32')  #cast bool-tensor to float
    n_correct = K.sum(mask * correct)  #
Example #7
0
File: base.py Project: xyt556/krige
    def fitSermivariogramModel(self,
                               modelname,
                               nlag=15,
                               tsill=None,
                               trange=None,
                               tnugget=0.0):
        """Fit a semivariogram model to the datas
        Returns a model with the best parameters (sill, range, nugget) to
        fit the datas (semivariance against distance)

        Parameters
        ----------
        modelname : String
                    The desired model name to use. One of:
                    Spherical, Exponential, Gaussian, Pentaspherical, Nugget
        nlag :  Integer
                If not None, the semivariogram will be fitted using nlags
                bins (empirical semivariogram), between distance[0, max], else
                all the datas will be used (global semivariogram).
        tsill : Float
                Temporary initial sill, used by the fitted algorithm. If
                None it will be estimated from the datas.
        trange : Float
                 Temporary initial range, used by the fitted algorithm.
                 If None it will be estimated from the datas.
        tnugget : Float
                  Temporary initial nugget, used by the fitted algorithm.

        Returns
        ----------
        a model instance with sill, range and nugget computed to best fit
        the datas. Highly depending from the initial inputs, due to the used
        algorithm (scipy.optimize.leastsqr)

        """
        # the input datas
        dist = self.infos.dist
        svar = self.infos.svar

        # lag?
        if nlag is None:
            nlag = len(dist)
        if nlag < 1:
            raise ValueError, "nlag must be >=1"
        # Initials parameters estimations
        # TODO : find best solutions
        if tsill is None:
            tsill = 9 / 10 * np.max(svar)
        if trange is None:
            trange = 0.5 * np.max(dist)
        # Sort by distance
        sortind = np.argsort(dist)
        sortdist = dist[sortind]
        sortsvar = svar[sortind]
        # Select by bins
        index = sortdist.searchsorted(np.linspace(0, dist.max(), nlag + 1))
        dist = [
            sortdist[index[i - 1]:index[i]].mean()
            for i in range(1, len(index))
        ]
        svar = [
            sortsvar[index[i - 1]:index[i]].mean()
            for i in range(1, len(index))
        ]

        # Retrieve the model class from his name
        model = models.getModels(sill=tsill, range=trange,
                                 nugget=tnugget)[modelname]

        # these are our inital guesses for the sill and range
        params = (model.sill, model.range, model.nugget)

        # perform the least squares
        lstsqResult = optimize.leastsq(model.residual,
                                       params,
                                       args=(dist, svar),
                                       full_output=0)

        if (lstsqResult != 1):
            p = lstsqResult[0]
            model.sill, model.range, model.nugget = p
            if model.range > 0:
                # work out the square deviation too
                squareDeviates = model.residual(p, dist, svar)**2
                # we divide by number of points minus the degrees of freedom
                denom = 1 / float(len(svar) - len(p))
                model.variance = denom * sum(squareDeviates)
                pprint([["Model Type", model.type], ["Sill", model.sill],
                        ["Range", model.range], ["Nugget", model.nugget]])
                return model
            else:
                pprint([
                    ["Error", "Computed range <=0 :("],
                ])
                return None
        else:
            pprint([
                ["Error", "Bad fitting computation :("],
            ])
            return None