def __init__(self, input_x_vector, num_hidden_layers, num_units_per_layer, activation_func=None, unit_weight_mean=0, unit_bias_mean=0, unit_weight_logsigma=1, unit_bias_logsigma=1): if num_units_per_layer < 1: print( "num units per layer should be at least 1. If you want no hidden layers, set num hidden layers to 0" ) exit(1) self.input_layer = [Unit(x) for x in input_x_vector ] # weights are not important here self.hidden_layers = [] for layer_index in range(0, num_hidden_layers): new_layer = [] for unit_index in range(0, num_units_per_layer): # Implementation detail: the units in first hidden layer should have weight vectors of length equal # to the input vector length. In the rest of hidden layers, the units should have weight vectors with # length of the previous layer length. In this implementation, all hidden layers have same num of units! # Therefore, the units have a weight vector of the length of num_units_per_layer if layer_index == 0: # first hidden layer (not the input layer) new_unit = Unit(0, len(self.input_layer), unit_weight_mean, ag_np.exp(unit_weight_logsigma)**2, unit_bias_mean, ag_np.exp(unit_bias_logsigma)**2) else: new_unit = Unit(0, num_units_per_layer, unit_weight_mean, ag_np.exp(unit_weight_logsigma)**2, unit_bias_mean, ag_np.exp(unit_bias_logsigma)**2) new_layer.append(new_unit) self.hidden_layers.append(new_layer) # should be set separately self.activation_func = activation_func self.output_unit = Unit(0, num_units_per_layer)
def instance_entities(data): """ Given a kind and some json, call insert on that kind and return the results. A little safer. """ fields = ('id', 'created', 'modified', 'entity_id', 'previous_id', 'status', 'available') entities = [] if 'cards' in data: for card_data in data['cards']: kind = card_data.get('kind') if kind in card_map: entities.push( card_map[kind](omit(card_data, fields)) ) if 'units' in data: entities = entities + [ Unit(omit(unit_data, fields)) for unit_data in data['units'] ] if 'subjects' in data: entities = entities + [ Subject(omit(subject_data, fields)) for subject_data in data['subjects'] ] return entities
def log_likelihood(self, n_outputs, n_inputs, sigma=0.1): loglik = 0 for index, input in enumerate(n_inputs): f_x_w_b = self.propagate_forward_and_calculate_output(Unit(input)) loglik += ag_norm.logpdf(n_outputs[index], f_x_w_b, sigma**2) return loglik
def approximate_loss(m_tilda, m_bar, s_tilda, s_bar, x_train_N, y_train_N): bnn = Bnn([Unit()], 0, 1, ag_np.tanh, m_tilda, m_bar, s_tilda, s_bar) log_p_w = bnn.get_log_p_w() log_p_b = bnn.get_log_p_b() log_p_w_b = log_p_w + log_p_b log_p_y_given_x_w_b = bnn.log_likelihood(y_train_N, x_train_N) log_q = bnn.get_log_q_w_b_given_m_s(m_tilda, m_bar, s_tilda, s_bar) L = log_p_w_b + log_p_y_given_x_w_b - log_q return L
def test_submit(): u = Unit('kg', 'Kilogram') u2 = Unit('g', 'Grams') u3 = Unit('km', 'KiloMeter') u.save() u2.save() u3.save() t = Theme('Environment') t2 = Theme('Transport') t.save() t2.save() st = SubTheme(t.id, 'Airquality') st2 = SubTheme(t2.id, 'Traffic') st.save() st2.save() db.session.commit()
def get_dummy_unit(self): unit = Unit.get_by_symbol("_A_UNIT_FOR_TESTING") if not unit: unit = Unit("_A_UNIT_FOR_TESTING", "_A_TEST_UNIT_DESCRIPTION_") unit.save() unit.commit() if unit not in self.clean_up: self.clean_up.append(unit) return unit
def add_unit(self, board_id, text, position): pith, transclusions = self.gm._get_pith(board_id, text) position = Position(x=position["x"], y=position["y"]) unit = Unit(board_id=board_id, pith=pith, position=position) unit.id = "{}:{}".format(unit.board_id, unit.short_id) self.gm.units.insert_one(unit.to_mongo()) unit_id = unit.short_id self.gm._insert_transclusions(board_id, unit_id, transclusions) self._record_unit_update(board_id, unit_id) return {"unit": self.gm._get_extended_unit(board_id, unit_id)}
def dummy_unit() -> Unit: """ Creates a dummy unit :yields: A unit instance """ dummy_unit = Unit("NOT_A_SYMBOL", "DUMMY_DESCRIPTION") dummy_unit.save() dummy_unit.commit() yield dummy_unit dummy_unit.delete() dummy_unit.commit()
def create_unit(self, _type: str, description: str) -> db.Model: """ Create Unit :param _type: Unit type :param description: Description of unit :return: Unit """ unit = Unit(_type=_type, description=description) unit.save() return unit
def instance(data): """ Given a kind and some json, call insert on that kind and return the results. """ if 'card' in data: return Card(data['card']) # TODO-1 This needs to also get the right card kind... elif 'unit' in data: return Unit(data['unit']) elif 'set' in data: return Set(data['set'])
def instance_new_entity(data): """ Save as above, but a little safer. """ fields = ('id', 'created', 'modified', 'entity_id', 'previous_id', 'status', 'available') if 'card' in data: return Card(omit(data['card'], fields)) # TODO-1 This needs to also get the right card kind... elif 'unit' in data: return Unit(omit(data['unit'], fields)) elif 'set' in data: return Set(omit(data['set'], fields))
def post(self, board_id, discussion_id, user_id, text, flairs): pith, transclusions = self.gm._get_pith(board_id, text) user = self.gm.users.find_one({"board_id": board_id, "short_id": user_id}) unit = Unit(board_id=board_id, pith=pith, chat=True, author=user_id, author_name=user["nickname"], flairs=flairs) unit.id = "{}:{}".format(unit.board_id, unit.short_id) self.gm.units.insert_one(unit.to_mongo()) unit_id = unit.short_id self.gm._insert_transclusions(board_id, unit_id, transclusions) self.gm.discussions.update_one( {"short_id" : discussion_id, "board_id": board_id}, {"$push": {"chat": unit_id}} ) return {"unit": self.gm._get_chat_unit(board_id, unit_id)}
def create_unit(self, number: int) -> Unit: """ Create unit/s :param number: a number to make the unit type unique """ unit = Unit.get_by_symbol("Unit_type_{}".format(number)) if unit: self.units.append(unit) return unit unit = Unit("Unit_type_{}".format(number), "Description_{}".format(number)) unit.save() unit.commit() self.units.append(unit) return unit
def create_unit(symbol: str = 'kg', description: str = 'Kilogram') -> None: """ Create Unit :param symbol: Units symbol :param description: Units description :raises ValueError: If the new Unit is not persisted to the dB """ unit = Unit.get_by_symbol(symbol) if not unit: unit = Unit(symbol, description) unit.save() unit.commit() if not unit: logger.critical('ValueError raised while creating Unit') raise ValueError
def publish(self, board_id, discussion_id, unit_id, position): # make copy orig_unit = self.gm.units.find_one({ "short_id": unit_id, "board_id": board_id }) position = Position(x=position["x"], y=position["y"]) pith, transclusions = self.gm._get_pith(board_id, orig_unit["pith"]) unit = Unit(board_id=board_id, pith=pith, position=position) unit.id = "{}:{}".format(unit.board_id, unit.short_id) self.gm.units.insert_one(unit.to_mongo()) unit_id = unit.short_id self.gm._insert_transclusions(board_id, unit_id, transclusions) self._record_unit_update(board_id, unit_id) return {"unit": self.gm._get_extended_unit(board_id, unit_id)}
def instance_new_entity(data): """ Given a kind and some json, call insert on that kind and return the results. A little safer. """ fields = ('id', 'created', 'modified', 'entity_id', 'previous_id', 'status', 'available') if 'card' in data: kind = data['card'].get('kind') if kind in card_map: return card_map[kind](omit(data['card'], fields)) elif 'unit' in data: return Unit(omit(data['unit'], fields)) elif 'set' in data: return Set(omit(data['set'], fields))
def setUp(self): """ Setup a FlaskClient, AppContext, Admin user, Authorization header for requests to the Flask Client and a dummy Theme """ self.client, self.app_context = self.create_test_client() self.user = self.create_admin_user() self.auth_header = self.get_auth_header() self.unit = Unit("_test_unit_type_", "A test unit") self.unit.save() self.unit.commit() self.attribute = Attributes.get_by_name("_test_attribute_") self.theme = self.create_dummy_theme() self.subtheme = self.create_dummy_subtheme() if not self.attribute: self.attribute = Attributes("1234567890-123456789-123456789", "_test_attribute_", "_table_name_", self.subtheme.id, self.unit.id) self.attribute.save() self.attribute.commit() self.clean_up = [self.user, self.attribute, self.theme, self.subtheme, self.theme, self.unit]
def post( self) -> ({ "message": str, "symbol": str, "description": str }, HTTPStatus): """ Creates a new unit of measure in the unit database table; with a symbol and a description for example symbol = 'kg' description = 'Kilogram Parameters can be passed using a POST request that contains a JSON with the following fields: :param symbol: units symbol eg kg for kilograms :param description: a description of the measurement unit eg.. kilograms :type symbol: string :type description: string :return: A json response containing a message, unit type and unit description with an http status code of 200 otherwise a JSON with the error discription and the appropriate HTTPStatus code """ if not get_jwt_claims()['admin']: return {"message": "Not Authorized."}, HTTPStatus.UNAUTHORIZED args = self.reqparser.parse_args() # does the measurement unit already exist? if Unit.get_by_symbol(args["symbol"]): # measurement unit already exists return { "message": "Unit already exists.", "symbol": args["symbol"], "description": args["description"] }, HTTPStatus.BAD_REQUEST # Create new measurement unit new_unit = Unit(args["symbol"], args["description"]) new_unit.save() new_unit.commit() return { "message": "New unit created", "symbol": args["symbol"], "description": args["description"] }, HTTPStatus.OK
def create_notice_unit(self, board_id, discussion_id, message, user_id): # clean it of transclusions message = self._wipe_pith(message) user = self.users.find_one({"short_id": user_id, "board_id": board_id}) unit = Unit(board_id=board_id, pith=message, chat=True, author=user_id, author_name=user["nickname"], flairs=[], notice=True) unit.id = "{}:{}".format(unit.board_id, unit.short_id) self.units.insert_one(unit.to_mongo()) unit_id = unit.short_id self.discussions.update_one( { "short_id": discussion_id, "board_id": board_id }, {"$push": { "chat": unit_id }}) return unit
def respond_to_card_route(request, card_id): """ Record and process a learner's response to a card. NEXT STATE POST Respond Card -> GET Learn Card ...when not ready -> GET Choose Unit ...when ready, but still units -> GET View Set Tree ...when ready and done """ # TODO-3 simplify this method. # perhaps smaller methods or move to model layer? current_user = get_current_user(request) if not current_user: return abort(401) card = get_card_by_kind(card_id) if not card: return abort(404) # Make sure the card is the current one context = current_user.get_learning_context() if context.get('card', {}).get('entity_id') != card['entity_id']: return abort(400) r = seq_update(current_user, card, request['params'].get('response')) errors, response, feedback = (r.get('errors'), r.get('response'), r.get('feedback')) if errors: return 400, { 'errors': errors, 'ref': 'wtyOJPoy4bh76OIbYp8mS3LP', } set_ = Set(context.get('set')) unit = Unit(context.get('unit')) status = judge(unit, current_user) # If we are done with this current unit... if status == "done": buckets = traverse(current_user, set_) # If there are units to be diagnosed... if buckets['diagnose']: unit = buckets['diagnose'][0] next_card = choose_card(current_user, unit) next_ = { 'method': 'GET', 'path': '/s/cards/{card_id}/learn'.format( card_id=next_card['entity_id']), } current_user.set_learning_context(card=next_card.data, unit=unit.data, next=next_) # If there are units to be learned or reviewed... elif buckets['learn'] or buckets['review']: next_ = { 'method': 'GET', 'path': '/s/sets/{set_id}/units'.format(set_id=set_['entity_id']), } current_user.set_learning_context(card=None, unit=None, next=next_) # If we are out of units... else: next_ = { 'method': 'GET', 'path': '/s/sets/{set_id}/tree'.format(set_id=set_['entity_id']), } current_user.set_learning_context(card=None, unit=None, next=next_) # If we are still reviewing, learning or diagnosing this unit... else: next_card = choose_card(current_user, unit) if next_card: next_ = { 'method': 'GET', 'path': '/s/cards/{card_id}/learn'.format( card_id=next_card['entity_id']), } current_user.set_learning_context(card=next_card.data, next=next_) else: next_ = {} current_user.set_learning_context(next=next_) return 200, { 'response': response.deliver(), 'feedback': feedback, 'next': next_, }
'name': 'High Templar', 'description': 'High templar are protoss warriors that use powerful psionic powers to support other protoss forces.', 'mineral_cost': 50, 'vespene_cost': 150, 'supply_cost': 2, 'build_time': 50 }] } for race in race_units: race_id = race_ids[race] for unit in race_units[race]: try: new_unit = Unit(name=unit['name'], description=unit['description'], mineral_cost=unit['mineral_cost'], vespene_cost=unit['vespene_cost'], supply_cost=unit['supply_cost'], build_time=unit['build_time']) session.add(new_unit) session.commit() new_race_unit = RaceUnit(race_id=race_id, unit_id=new_unit.id) session.add(new_race_unit) session.commit() except: session.rollback() raise
def list_units(self): """ Get the list of units contained within the set. Recursive. Connecting. TODO-2 OMG break into smaller functions TODO-2 Should this method be part of the Unit class/module, as it returns units? """ def _(): # *** First, we need to break down # the set into a list of known units. *** unit_ids = set() sets = [self] while sets: set_ids = set() for set_ in sets: unit_ids.update({ member['id'] for member in set_.data.get('members') if member['kind'] == 'unit' }) set_ids.update({ member['id'] for member in set_.data.get('members') if member['kind'] == 'set' }) sets = Set.list_by_entity_ids(set_ids) # *** Second, we need to find all # the required connecting units. *** next_grab, units, unit_requires = unit_ids, [], {} while next_grab: tier_units = Unit.list_by_entity_ids(next_grab) units += tier_units next_grab = set() for unit in tier_units: if 'require_ids' not in unit: continue unit_id = unit['entity_id'] require_ids = unit_requires[unit_id] = \ set(unit['require_ids']) for require_id in require_ids: if require_id in unit_ids: ids = {unit_id} while ids: unit_ids.update(ids) ids = { unit_id for unit_id, require_ids in unit_requires.items() if unit_id not in unit_ids and require_ids & ids } elif require_id not in unit_requires: next_grab.add(require_id) units = [ unit.data for unit in units if unit['entity_id'] in unit_ids ] return units # If we already have it stored, use that key = 'set_units_{id}'.format(id=self['entity_id']) return [Unit(data) for data in memoize_redis(key, _)]
def create_unit(self, _type, description): unit = Unit(_type=_type, description=description) unit.save() return unit