def error(self, input_x, labels): """ error value """ labels = one_hot(labels, self.n_classes) predicts = self.predict(input_x) error = predicts != labels return np.mean(error)
def loss(self, input_x, labels): """ loss function """ labels = one_hot(labels, self.n_classes) for layer in self.layers: input_x = layer.forward(input_x) return self.cost_fun.loss(labels, input_x)
def gradient_descent(self, input_x, input_y): """ grandient descent """ input_a = input_x for layer in self.layers: input_a = layer.forward(input_a) input_grad = self.cost_fun.grad(one_hot(input_y, self.n_classes), input_a) for layer in reversed(self.layers): input_grad = layer.backward(input_grad)
def fixInputs(df): # turn smoking marker into 2 vars (assume missing are former-smokers) # never_smoke captures never-smokers (1) vs ever-smokers (2 & 3) # still_smoke captures current-smokers (3) vs not-current-smokers (1 & 2) df['never_smoke'] = df['smoke_3cat'].values == 1 df['still_smoke'] = df['smoke_3cat'].values == 3 ################################## ### blood pressure calculations # mean artierial pressure (MAP) df['map'] = (df['sbp'] + 2 * df['dbp']) / 3 # pulse pressure (PP) df['pp'] = df['sbp'] - df['dbp'] #df['bp_ratio'] = df['dbp']/df['sbp'] # clinical cvd implies subclinical cvd df.loc[df['sub_clinicalcvd'] == 1, 'sub_subclinicalcvd'] = 1 # scale count variable #df['n_agents'] = df['n_agents']/np.max(df['n_agents'].values) # artifact of way data was recorded: if value was calculated to be less than 2, it was marked nan due to inaccuracy df.loc[np.logical_and(df['umalcr'].values == np.nan, df['screat'].values != np.nan), 'umalcr'] = 1.5 ############################################################################################### ### lipid calculations ### chr = cholesterol, trr = triglycerides, ldl/hdl = low/high density lipoproteins ### already checked that trr, chr, hdl are nans at the same time (implying same panel) df['non_hdl'] = df['chr'] - df['hdl'] df['ldl'] = df['non_hdl'] - 0.2 * df['trr'] df['trr_hdl'] = df['trr'] / df['hdl'] df['chr_hdl'] = df['chr'] / df['hdl'] ## assume all others are hispanics? #df.loc[df['race4'].values == 'OTHER', 'race4'] = 'HISPANIC' df = one_hot(df, 'race4') return df.drop(columns=[ 'newsiteid', 'race4', 'sbptertile', 'smoke_3cat', 'race_black', 'sub_senior', 'inclusionfrs', 'sub_cvd' ] + ['noagents', 'sub_ckd'])
def play_from( board, positions, moves, values, lock ): #Possible improvement could be placing our data into tuples?? post-processing required tho import chess import chess.uci import numpy as np from helper import conversion, one_hot stockfish = chess.uci.popen_engine("stockfish") info_handler = chess.uci.InfoHandler() stockfish.info_handlers.append(info_handler) stockfish.uci() while not board.is_game_over(): stockfish.position(board) stock_move = stockfish.go( movetime=250) #Set stockfish to 5 moves per second #Generate position turn = board.turn k_castle = board.has_kingside_castling_rights(turn) q_castle = board.has_queenside_castling_rights(turn) kb_castle = board.has_kingside_castling_rights(not turn) qb_castle = board.has_queenside_castling_rights(not turn) check = board.is_check() position = conversion(str(board), turn, k_castle, q_castle, check, kb_castle, qb_castle) #Append position to training data mate = info_handler.info["score"][1].mate #Acquire lock so that all data is appended to our lists in the correct order! lock.acquire() positions.append(position) moves.append(one_hot( stock_move[0])) #Add stockfish's choice to training data if mate is None: value = np.tanh(info_handler.info["score"][1].cp / 300) values.append(value) else: values.append(mate / np.abs(mate)) lock.release() board.push(stock_move[0])