Ejemplo n.º 1
0
 def test_minmax(self):
     data = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]
     standard = Scaler(scheme='minmax')
     self.assertEqual(
         standard.fit_transform(data).tolist(),
         np.array([[0., 0.], [0.25, 0.25], [0.5, 0.5], [1., 1.]]).tolist())
     self.assertEqual(
         standard.transform([[2, 2]]).tolist(),
         np.array([[1.5, 0.]]).tolist())
Ejemplo n.º 2
0
 def test_standard(self):
     data = np.array([[0, 0], [0, 0], [1, 1], [1, 1]])
     standard = Scaler(scheme='standard')
     self.assertEqual(
         standard.fit_transform(data).tolist(),
         np.array([[-1., -1.], [-1., -1.], [1., 1.], [1., 1.]]).tolist())
     self.assertEqual(
         standard.transform([[2, 2]]).tolist(),
         np.array([[3., 3.]]).tolist())
Ejemplo n.º 3
0
 def _parse_scaler_configs(self, location):
     scalers = []
     for scaler_config in list(glob(path.abspath(location) + '/*.cfg')):
         parsed_config = self._read_config(scaler_config)
         scaler = Scaler(parsed_config)
         scalers.append(scaler)
     return scalers
Ejemplo n.º 4
0
  def scaleImage(self, percent):
    if not self.loadedImage: return
    scale = percent / 100.0

    # Nearest neighbour interpolation
    if self.isNearestNeighbourButton.isChecked():
      self.scaledImage = Scaler.nearestNeighbourInterpolation(self.loadedImage, scale)

    # Bicubic interpolation
    if self.isBicubicButton.isChecked():
      pass

    # Bilinear interpolation
    if self.isBilinearButton.isChecked():
      self.scaledImage = Scaler.bilinearInterpolation(self.loadedImage, scale)

    imageLabel = QLabel()
    imageLabel.setPixmap(QPixmap.fromImage(self.scaledImage))
    self.scrollArea.setWidget(imageLabel)
Ejemplo n.º 5
0
def main():
    env = gym.make(args.env)
    env = ActionMappingWrapper(env)

    obs_dim = env.observation_space.shape[0]
    act_dim = env.action_space.shape[0]
    obs_dim += 1  # add 1 to obs dim for time step feature

    scaler = Scaler(obs_dim)

    model = MujocoModel(obs_dim, act_dim)
    alg = parl.algorithms.PPO(model,
                              act_dim=act_dim,
                              policy_lr=model.policy_lr,
                              value_lr=model.value_lr)
    agent = MujocoAgent(alg,
                        obs_dim,
                        act_dim,
                        args.kl_targ,
                        loss_type=args.loss_type)

    # run a few episodes to initialize scaler
    collect_trajectories(env, agent, scaler, episodes=5)

    test_flag = 0
    total_steps = 0
    while total_steps < args.train_total_steps:
        trajectories = collect_trajectories(env,
                                            agent,
                                            scaler,
                                            episodes=args.episodes_per_batch)
        total_steps += sum([t['obs'].shape[0] for t in trajectories])
        total_train_rewards = sum([np.sum(t['rewards']) for t in trajectories])

        train_obs, train_actions, train_advantages, train_discount_sum_rewards = build_train_data(
            trajectories, agent)

        policy_loss, kl = agent.policy_learn(train_obs, train_actions,
                                             train_advantages)
        value_loss = agent.value_learn(train_obs, train_discount_sum_rewards)

        logger.info(
            'Steps {}, Train reward: {}, Policy loss: {}, KL: {}, Value loss: {}'
            .format(total_steps, total_train_rewards / args.episodes_per_batch,
                    policy_loss, kl, value_loss))
        if total_steps // args.test_every_steps >= test_flag:
            while total_steps // args.test_every_steps >= test_flag:
                test_flag += 1
            eval_reward = run_evaluate_episode(env, agent, scaler)
            logger.info('Steps {}, Evaluate reward: {}'.format(
                total_steps, eval_reward))
Ejemplo n.º 6
0
        def ShowAll(self, event=None):
            wx.GetApp().Yield(True)

            self.UnBindAllMouseEvents()
            Canvas = self.Canvas
            Canvas.InitAll()
            imageFile = 'kopejais.tif' # this was back.tif
            data = open(imageFile, "rb").read()
            # convert to a data stream
            stream = cStringIO.StringIO(data)
            # this allows us to disable logging from tif reader module
            noLog = wx.LogNull()
            image = wx.ImageFromStream( stream )
            # this enables logging back
            del noLog
            # would this become fast if we did not have a scaled bitmap?
            ORIGINAL_Y = 200
            BitMap = Canvas.AddScaledBitmap(image, (0, ORIGINAL_Y), Height=ORIGINAL_Y)
            from tiff_size import GetTiffSize
            sizing = GetTiffSize(imageFile)
            from scaler import Scaler
            self.scaler = Scaler(sizing[4]-sizing[2], sizing[5]-sizing[3], sizing[2], sizing[3])
            self.scaler.set_scale_y(ORIGINAL_Y)
            self.Canvas.ZoomToBB()
Ejemplo n.º 7
0
    def setup(self):
        # metadata
        num_pegged = self.metadata['num_pegged']
        num_airfoils = self.metadata['num_airfoils']
        num_nodes = self.metadata['num_nodes']
        num_bins = self.metadata['num_bins']
        safety_factor = self.metadata['safety_factor']
        gearbox_stages = self.metadata['gearbox_stages']
        gear_configuration = self.metadata['gear_configuration']
        mb1_type = self.metadata['mb1_type']
        mb2_type = self.metadata['mb2_type']
        drivetrain_design = self.metadata['drivetrain_design']
        uptower_transformer = self.metadata['uptower_transformer']
        has_crane = self.metadata['has_crane']
        reference_turbine = self.metadata['reference_turbine']
        reference_turbine_cost = self.metadata['reference_turbine_cost']
        power_file = self.metadata['power_file']
        ct_file = self.metadata['ct_file']

        #         # design variables
        #         i = self.add_subsystem('dof', IndepVarComp(), promotes=['*'])
        #         i.add_output('design_tsr', desc='design tip speed ratio')
        #         i.add_output('blade_number', desc='number of blades')
        #         i.add_output('rotor_diameter', units='m', desc='rotor diameter')
        #         i.add_output('chord_coefficients', units = 'm', desc = 'coefficients of polynomial chord profile', shape=num_pegged)
        #         i.add_output('twist_coefficients', units = 'deg', desc = 'coefficients of polynomial twist profile', shape=num_pegged)
        #         i.add_output('span_airfoil_r', units='m', desc='list of blade node radial location at which the airfoils are specified', shape=num_airfoils)
        #         i.add_output('span_airfoil_id', desc='list of blade node Airfoil ID', shape=num_airfoils)
        #         i.add_output('pitch', units='deg', desc = 'pitch angle')
        #         i.add_output('thickness_factor', desc='scaling factor for laminate thickness')
        #         i.add_output('shaft_angle', units='deg', desc='angle of the LSS inclindation with respect to the horizontal')
        #         i.add_output('cut_in_speed', units = 'm/s', desc = 'cut-in wind speed')
        #         i.add_output('cut_out_speed', units = 'm/s', desc = 'cut-out wind speed')
        #         i.add_output('machine_rating', units='kW', desc='machine rating')
        #         i.add_output('drive_train_efficiency', desc='efficiency of aerodynamic to electrical conversion')
        #         i.add_output('gear_ratio', desc='overall gearbox ratio')
        #         i.add_output('Np', desc='number of planets in each stage', shape=3)

        # sub-systems
        self.add_subsystem('scale', Scaler(), \
                           promotes_inputs=['machine_rating', 'rotor_diameter'], \
                           promotes_outputs=['generator_voltage', 'collection_voltage', 'turbine_rated_current', \
                                             'warranty_percentage', 'solidity_rotor', \
                                             'cd_nacelle', 'cd_rotor_idle_vane', \
                                             'mass_eccentricity', 'yaw_to_hub_height', \
                                             'front_area_nacelle', 'hub_height', \
                                             'tower_top_diameter'])

        self.add_subsystem('blade', Blade(num_pegged=num_pegged, \
                                          num_airfoils=num_airfoils, \
                                          num_nodes=num_nodes, \
                                          num_bins=num_bins, \
                                          reference_turbine=reference_turbine, \
                                          power_file=power_file, ct_file=ct_file, \
                                          rho_air=1.225, E_blade=36.233e9, g=9.8), \
                           promotes_inputs=['design_tsr', 'blade_number', 'rotor_diameter', \
                                            'chord_coefficients', 'twist_coefficients', 'span_airfoil_r', 'span_airfoil_id', \
                                            'pitch','thickness_factor', 'shaft_angle',\
                                            'cut_in_speed', 'cut_out_speed', 'machine_rating', 'drive_train_efficiency'], \
                           promotes_outputs=['rotor_cp', 'rotor_ct', 'rotor_torque', 'rotor_thrust', \
                                             'rated_wind_speed', 'wind_bin', 'elec_power_bin', 'ct_bin', \
                                             'span_stress_max', 'tip_deflection', 'blade_mass', 'rotor_speed'])

        self.add_subsystem('hub', Hub(safety_factor=safety_factor, g=9.8), \
                           promotes_inputs=['machine_rating', 'blade_number', 'rotor_diameter', 'shaft_angle'], \
                           promotes_outputs=[('hub_assembly_mass', 'hub_mass'), 'rotor_mass', 'rotor_force', 'rotor_moment'])


        self.add_subsystem('nacelle', Nacelle(safety_factor=safety_factor, \
                                               gearbox_stages=gearbox_stages, \
                                               gear_configuration=gear_configuration, \
                                               mb1_type=mb1_type, \
                                               mb2_type=mb2_type, \
                                               drivetrain_design=drivetrain_design, \
                                               uptower_transformer=uptower_transformer, \
                                               has_crane=has_crane), \
                           promotes_inputs=['rotor_diameter', 'machine_rating', 'shaft_angle', 'gear_ratio', 'Np'], \
                           promotes_outputs=['nacelle_mass'])

        self.add_subsystem('mass', ExecComp('rna_mass = rotor_mass + nacelle_mass', \
                                           rna_mass = {'units': 'kg'}, \
                                           rotor_mass = {'units': 'kg'}, \
                                           nacelle_mass = {'units': 'kg'}), \
                            promotes_outputs=['rna_mass'])

        self.add_subsystem('cost', CSMCalibrated(reference_turbine_cost=reference_turbine_cost), \
                           promotes_inputs=['machine_rating', 'rotor_diameter', 'blade_number'], \
                           promotes_outputs=['cost_rna'])

        # connections
        self.connect('scale.hub_radius', 'blade.hub_radius')
        self.connect('scale.overhang', 'nacelle.overhang')
        self.connect('tower_top_diameter', 'nacelle.tower_top_diameter')
        self.connect('scale.gearbox_cm_x', 'nacelle.gearbox_cm_x')

        self.connect('blade.span_chord', ['hub.blade_root_diameter'],
                     src_indices=[0])
        self.connect('blade_mass', ['hub.blade_mass', 'cost.blade_mass'])
        self.connect('blade.root_moment_flap', ['hub.rotor_bending_moment'])
        self.connect('rotor_torque',
                     ['hub.rotor_torque', 'nacelle.rotor_torque'])
        self.connect('rotor_thrust',
                     ['hub.rotor_thrust', 'nacelle.rotor_thrust'])

        self.connect('rotor_speed', ['nacelle.rotor_speed'])
        self.connect('rotor_force', ['nacelle.rotor_force'])
        self.connect('rotor_moment', ['nacelle.rotor_bending_moment'])
        self.connect('rotor_mass', ['nacelle.rotor_mass'])

        self.connect('rotor_mass', ['mass.rotor_mass'])
        self.connect('nacelle_mass', ['mass.nacelle_mass'])

        self.connect('hub.hub_mass', 'cost.hub_mass')
        self.connect('hub.pitch_mass', 'cost.pitch_mass')
        self.connect('hub.spinner_mass', 'cost.spinner_mass')
        self.connect('nacelle.lss_mass', 'cost.lss_mass')
        self.connect('nacelle.main_bearing_mass', 'cost.main_bearing_mass')
        self.connect('nacelle.second_bearing_mass', 'cost.second_bearing_mass')
        self.connect('nacelle.gearbox_mass', 'cost.gearbox_mass')
        self.connect('nacelle.hss_mass', 'cost.hss_mass')
        self.connect('nacelle.generator_mass', 'cost.generator_mass')
        self.connect('nacelle.bedplate_mass', 'cost.bedplate_mass')
        self.connect('nacelle.platforms_mass', 'cost.platform_mass')
        self.connect('nacelle.crane_mass', 'cost.crane_mass')
        self.connect('nacelle.yaw_mass', 'cost.yaw_mass')
        self.connect('nacelle.vs_electronics_mass', 'cost.vs_electronics_mass')
        self.connect('nacelle.hvac_mass', 'cost.hvac_mass')
        self.connect('nacelle.cover_mass', 'cost.cover_mass')
        self.connect('nacelle.transformer_mass', 'cost.transformer_mass')
Ejemplo n.º 8
0
    class DrawFrame(wx.Frame):
        def __init__(self,parent, id,title,position,size):
            wx.Frame.__init__(self,parent, id,title,position, size)

            MenuBar = wx.MenuBar()                     

            file_menu = wx.Menu()
            item = file_menu.Append(-1, "&Close","Close")
            self.Bind(wx.EVT_MENU, self.OnQuit, item)
            item = file_menu.Append(-1, "&Save File","Save the updated dxf")
            self.Bind(wx.EVT_MENU, self.OnSaveFILE, item)
            item = file_menu.Append(-1, "&Open File","Open vector file")
            self.Bind(wx.EVT_MENU, self.OnOpenFILE, item)

            MenuBar.Append(file_menu, "&File")
            
            draw_menu = wx.Menu()

            view_menu = wx.Menu()
            item = view_menu.Append(-1, "Zoom to &Fit","Zoom to fit the window")
            self.Bind(wx.EVT_MENU, self.ZoomToFit, item)
            MenuBar.Append(view_menu, "&View")

            help_menu = wx.Menu()
            item = help_menu.Append(-1, "&About",
                                    "More information About this program")
            self.Bind(wx.EVT_MENU, self.OnAbout, item)
            MenuBar.Append(help_menu, "&Help")

            self.SetMenuBar(MenuBar)

            self.CreateStatusBar()

            
            # Add the Canvas
            NC = NavCanvas.NavCanvas(self,
                                     Debug = 0,
                                     BackgroundColor = "DARK SLATE BLUE")

            self.Canvas = NC.Canvas # reference the contained FloatCanvas

            self.ElevationWindow = wx.TextCtrl(self, wx.ID_ANY,
                                         "?",
                                         style = (wx.TE_MULTILINE |
                                                  wx.SUNKEN_BORDER)
                                         )
            self.ElevationWindow.Bind(wx.EVT_TEXT, self.OnMsgUpdate)
            self.ElevationWindow.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
            
            ##Create a sizer to manage the Canvas and message window
            MainSizer = wx.BoxSizer(wx.VERTICAL)
            MainSizer.Add(NC, 1000, wx.EXPAND)
            MainSizer.Add(self.ElevationWindow, 1, wx.EXPAND | wx.ALL, 5)

            self.SetSizer(MainSizer)
            self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

            self.EventsAreBound = False

            ## getting all the colors for random objects
            wx.lib.colourdb.updateColourDB()
            self.colors = wx.lib.colourdb.getColourList()

            self.lastline = None

            return None
        def OnKillFocus(self, object):
            """ 
            we always want to be on 
            focuse for the elevation window
            if we move away and we move to our floatcanvas, then 
            we put it back in elevation window
            there might be a better way to do so, but I have 
            not found one
            """
            if type(object.GetWindow()) is wx.lib.floatcanvas.FloatCanvas.FloatCanvas:
                self.ElevationWindow.SetFocus()
                self.ElevationWindow.SelectAll()
                
        def OnMsgUpdate(self, object):
            if self.lastline != None:
                try:
                    self.elevations[self.lastline.line_idx] = float(self.ElevationWindow.GetValue())
                except ValueError:
                    self.elevations[self.lastline.line_idx]

        def UnBindAllMouseEvents(self):
            ## Here is how you unbind FloatCanvas mouse events
            self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DOWN)
            self.Canvas.Unbind(FloatCanvas.EVT_LEFT_UP)
            self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DCLICK)

            self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_DOWN)
            self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_UP)
            self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_DCLICK)

            self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_DOWN)
            self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_UP)
            self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_DCLICK)

            self.EventsAreBound = False


        def DrawLines(self):
            for i in range(len(self.lines)):
                L = self.Canvas.AddLine(self.lines[i], LineWidth = 2, LineColor = _ColorFromElevation(self.elevations[i]))
                L.line_idx = i
                L.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.LineGotHit)
            self.Canvas.Draw(Force = True)

        def DrawDxf(self, path):
            from dxf_reader import DXFReader
            dxf = DXFReader(path)
            self.lines = []
            self.elevations = []
            for entity in dxf.lines():
                line = []
                el = -1
                for point in entity.points():
                    line.append(self.scaler.locate((point[0], point[1])))
                    lastpoint = point
                if len(point) > 2:
                    el = point[2] 
                self.lines.append(line)
                self.elevations.append(el)
            self.DrawLines()
        def DrawElv(self, path):
            lines = file(path, "rb").readlines();
            self.lines = []
            self.elevations = []
            for line in lines:
                splits = [float(i) for i in line.split(",")]
                self.elevations.append(splits[0])
                self.lines.append([])
                for i in range(1, len(splits), 2):
                    self.lines[-1].append((splits[i],splits[i+1]))
            self.DrawLines()
                

        def OnOpenFILE(self, event=None):
            import os
            dlg = wx.FileDialog(
                self, message="Open file ...", defaultDir=os.getcwd(), 
                defaultFile="", wildcard="*.dxf;*.elv", style=wx.FD_OPEN
                )
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                self.vector_file = path
                if len(path)>4 and path[-4:] == ".dxf":
                    self.DrawDxf(path)
                else: 
                    self.DrawElv(path)
        # make elv model
        def WriteElv(self, path, lines, elevations):
            f = file(path, "wt")
            for i in range(len(lines)):
                s = str(elevations[i])+","+",".join([str(point[0])+","+str(point[1]) for point in lines[i]])
                f.write(s+"\n")
            f.close()
                
        def OnSaveFILE(self, event=None):
            import os
            import dxf_writer
            dlg = wx.FileDialog(
                self, message="Save file as ...", defaultDir=os.getcwd(), 
                defaultFile="output.elv", wildcard="*.dxf;*.elv", style=wx.SAVE
                )
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                if path[-4:].lower() == ".dxf": #todo: make "isDxfFile" 
                    dxf_writer.WriteDXF(path, self.lines, self.elevations, self.scaler)
                if path[-4:].lower() == ".elv":
                    self.WriteElv(path, self.lines, self.elevations)

        def OnAbout(self, event):
            dlg = wx.MessageDialog(self,
                                   "This is a small program to attach elevation\n"
                                   "to lines traced over contours\n",
                                   "About Me",
                                   wx.OK | wx.ICON_INFORMATION)
            dlg.ShowModal()
            dlg.Destroy()

        def ZoomToFit(self,event):
            self.Canvas.ZoomToBB()

        def OnQuit(self,event):
            self.Close(True)

        def OnCloseWindow(self, event):
            self.Destroy()
 
        def ShowAll(self, event=None):
            wx.GetApp().Yield(True)

            self.UnBindAllMouseEvents()
            Canvas = self.Canvas
            Canvas.InitAll()
            imageFile = 'kopejais.tif' # this was back.tif
            data = open(imageFile, "rb").read()
            # convert to a data stream
            stream = cStringIO.StringIO(data)
            # this allows us to disable logging from tif reader module
            noLog = wx.LogNull()
            image = wx.ImageFromStream( stream )
            # this enables logging back
            del noLog
            # would this become fast if we did not have a scaled bitmap?
            ORIGINAL_Y = 200
            BitMap = Canvas.AddScaledBitmap(image, (0, ORIGINAL_Y), Height=ORIGINAL_Y)
            from tiff_size import GetTiffSize
            sizing = GetTiffSize(imageFile)
            from scaler import Scaler
            self.scaler = Scaler(sizing[4]-sizing[2], sizing[5]-sizing[3], sizing[2], sizing[3])
            self.scaler.set_scale_y(ORIGINAL_Y)
            self.Canvas.ZoomToBB()

        def LineGotHit(self, Object):
            if self.lastline!=None:
                self.lastline.SetLineColor(_ColorFromElevation(self.elevations[self.lastline.line_idx]))
            self.lastline = Object
            self.ElevationWindow.ChangeValue(str(self.elevations[Object.line_idx]))
            self.ElevationWindow.SetFocus()
            Object.SetLineColor("Green")
            self.Canvas.Draw(Force=True)
Ejemplo n.º 9
0
    },
    "cylinders": {
        3: 6,
        5: 6
    }
}

impute_lvl1_list = ["horsepower"]

lvl1_imputer_col_transf_pipe = FeatureColumnTransformer(
    transformers=[("impute1", Imputer(method="median"), impute_lvl1_list)],
    remainder="passthrough",
    n_jobs=-1)

scaler_pipe = FeatureColumnTransformer(transformers=[
    ("scaler", Scaler(method="minmax"),
     make_column_selector(dtype_include=np.number))
],
                                       remainder="passthrough",
                                       n_jobs=-1)

dummifier_pipe = FeatureColumnTransformer(transformers=[
    ("encoder", CategoricalEncoder(drop_first=False),
     make_column_selector(dtype_include=object))
],
                                          remainder="passthrough",
                                          n_jobs=-1)

# binner_pipe = FeatureColumnTransformer(
#     transformers=[
#         ("binner",FeatureKBinner(),["weight"])
Ejemplo n.º 10
0
Archivo: core.py Proyecto: rcthomas/qnd
def random_design(point_type, lower, upper, size=100, rng=None):
    scaler = Scaler(lower, upper, rng)
    design = Design(point_type, scaler, scipy.zeros((size, scaler.dims)))
    design.randomize()
    return design
Ejemplo n.º 11
0
    def two_way_interactions(self, collapsed_type, deep_out,
                             deep_kernel_constraint):
        # Calculate interactions with a dot product

        inputs = [None] * len(self.feature_names)
        biases = [None] * len(self.feature_names)
        factors = [None] * len(self.feature_names)
        interactions = []

        for i, groups in enumerate(self.deep_weight_groups):

            for grp, (feature_i, feature_j) in enumerate(groups):
                #factor_i = factors[self.feature_names.index(feature_i)]
                index_i = self.feature_names.index(feature_i)
                factor_i = self.build_variables(index_i, inputs, biases,
                                                factors)
                if isinstance(feature_j, str) or isinstance(
                        feature_j, unicode):
                    #factor_j = factors[ self.feature_names.index(feature_j) ]
                    index_j = self.feature_names.index(feature_j)
                    factor_j = self.build_variables(index_j, inputs, biases,
                                                    factors)
                    name_j = feature_j
                elif isinstance(feature_j, list):
                    name_j = "grp_{}".format("{:03d}".format(i))

                    if collapsed_type is not None:
                        #constant = np.zeros( (self.embedding_dimensions,) )
                        #k_constant = K.constant(constant, shape=(self.embedding_dimensions,))

                        collapsed_input = Input(shape=(1, ),
                                                name="input_{}".format(name_j))
                        embedding = Embedding(
                            input_dim=collapsed_type,
                            name="embedding_{}".format(name_j),
                            output_dim=self.embedding_dimensions)(
                                collapsed_input)

                        inputs.append(collapsed_input)
                        factor_j = embedding
                    else:
                        factors_j = []
                        for feature_j_n in feature_j:
                            #factor =  factors[ self.feature_names.index(feature_j_n) ]
                            index_j_n = self.feature_names.index(feature_j_n)
                            factor = self.build_variables(
                                index_j_n, inputs, biases, factors)

                            if deep_out:
                                if self.dropout_layer > 0:
                                    factor = Dropout(
                                        self.dropout_layer,
                                        name="dropout_terms_{}_{}".format(
                                            feature_j_n, i))(factor)
                                factor = Scaler(
                                    name="scaler_{}_{}".format(feature_j_n, i),
                                    constraint=deep_kernel_constraint)(factor)
                            factors_j.append(factor)

                        factor_j = Add(name=name_j)(factors_j)  # collapse them

                if factor_i is None:
                    print("Warning... {} does not have an embedding".format(
                        feature_i))
                    continue
                if factor_j is None:
                    print("Warning... {} does not have an embedding".format(
                        feature_j))
                    continue

                dot_product = Dot(axes=-1,
                                  name="interaction_{}X{}".format(
                                      feature_i, name_j))
                interactions.append(dot_product([factor_i, factor_j]))

        # Add whatever you have now:
        if len(interactions) == 0:
            two_way = None
        elif len(interactions) == 1:
            two_way = interactions[0:]
        else:
            two_way = Add(name="factors_term")(interactions)

        return inputs, biases, two_way
Ejemplo n.º 12
0
from apscheduler.schedulers.blocking import BlockingScheduler
from aggregator import InfluxAggregator
from client import DockerSDKClient
from scaler import Scaler
import yaml

if __name__ == "__main__":
    with open("config.yml", 'r') as conf_file:
        config = yaml.safe_load(conf_file)
        print(config)

        docker_client = DockerSDKClient()
        scheduler = BlockingScheduler()
        metrics_aggregator = InfluxAggregator(config['metrics_store'])

        autoscaler = Scaler(config, docker_client, scheduler,
                            metrics_aggregator)

    autoscaler.start()
Ejemplo n.º 13
0
from keras.callbacks import EarlyStopping

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.metrics import accuracy_score, confusion_matrix

from reader import Reader
from scaler import Scaler

seed = 7
np.random.seed(seed)
reader = Reader()
scaler = Scaler()

data = reader.read_dataset('Dataset/training_gold.csv')
X = reader.get_features(data, name='tempo')
y = reader.get_label(data, name='genre')

X = scaler.normalize(X)
y_categorical = scaler.categorical_binary(y)

# Creating a Neural Networks Model
model = Sequential()
model.add(Dense(28, input_shape=(X.shape[1], ), activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
Ejemplo n.º 14
0
def main(env_name,
         num_episodes,
         gamma=0.995,
         lamb=0.98,
         kl_targ=0.003,
         batch_size,
         hid1_mult=10,
         policy_logvar=1,
         clipping_range=[0.2, 0.2]):
    """
    Main training loop
    Args:
        env_name: OpenAI Gym environment name
        num_episodes: maximum number of episodes to run
        gamma: reward discount factor (float)
        lamb: lambda from Generalized Advantage Estimate
        kl_targ: D_KL target for policy update [D_KL(pi_old || pi_new)
        batch_size: number of episodes per policy training batch
        hid1_mult: hid1 size for policy and value_f (mutliplier of obs dimension)
        policy_logvar: natural log of initial policy variance
    """
    env, obs_dim, act_dim = init_gym(env_name)
    obs_dim += 1
    scaler = Scaler(obs_dim)
    val_func = ValueFunction(obs_dim, hid1_mult)
    policy = Policy(obs_dim, act_dim, kl_targ, hid1_mult, policy_logvar,
                    clipping_range)

    # Keeps track of training information
    EpisodeStats = collections.namedtuple(
        "Stats", ["episode_lengths", "episode_rewards"])

    stats = EpisodeStats(episode_lengths=np.zeros(num_episodes),
                         episode_rewards=np.zeros(num_episodes))

    # run a few episodes of untrained policy to initialize scaler:
    run_policy(env, policy, scaler, stats, episodes=5, index=0)

    episode = 0
    counter = 0
    while episode < num_episodes:
        print("Episode Num:", episode)
        trajectories = run_policy(env,
                                  policy,
                                  scaler,
                                  stats,
                                  episodes=batch_size,
                                  index=counter)
        counter += 1
        episode += len(trajectories)
        insert_value_estimate(trajectories,
                              val_func)  # Insert estimated values to episodes
        insert_disc_sum_rew(trajectories,
                            gamma)  # Calculate discounter sum of rewards
        insert_GAE(trajectories, gamma, lamb)  # Calculate advantages

        # Concatenate all episodes into a single array
        observations, actions, advantages, disc_sum_rew = create_train_set(
            trajectories)

        policy.update(observations, actions, advantages)  # Policy update
        val_func.fit(observations, disc_sum_rew)  # Value Function update

    policy.close_sess()
    val_func.close_sess()
    plot(stats)
Ejemplo n.º 15
0
 def __init__(self, riak_img, width):
     self.riak_img = riak_img
     self.width = self.validate_widths(width)
     self.scaler = Scaler(self.riak_img, self.width)