Best Python code snippet using fMBT_python
view_results.py
Source:view_results.py
1#! /usr/bin/python32#-*-coding: utf-8-*-3from init_fba import go, iplot, sleep, cl, pd, np, operator, ply4def create_scatter_plot_rxn_c3(D_fba, L_r, title, xaxis_title, save_fig = False):5 data = []6 for r_id in L_r:7 trace = go.Scatter(y = [D_fba[exp][r_id] for exp in sorted(D_fba.keys())],8 x = sorted(D_fba.keys()),9 name = r_id,10 mode = 'lines+markers',11 )12 data.append(trace)13 layout = go.Layout(title = title,14 yaxis = dict(title = 'Flux [µmol/s/m2]'),15 xaxis = dict(title = xaxis_title),16 width = 500,17 )18 fig = go.Figure(data=data, layout=layout)19 20 if save_fig:21 iplot(fig,filename=title,image='svg',image_width=500,image_height=500)22 sleep(5)23 else:24 iplot(fig)25 26def create_scatter_plot_met_c3(model, D_fba, L_r, m_id, title, xaxis_title, save_fig = False):27 data = []28 for r_id in L_r:29 r_obj = model.reactions.get_by_id(r_id)30 m_v = r_obj.get_coefficient(m_id)31 trace = go.Scatter(y = [abs(m_v)*D_fba[exp][r_id] for exp in sorted(D_fba.keys())],32 x = sorted(D_fba.keys()),33 name = r_id,34 mode = 'lines+markers',35 )36 data.append(trace)37 layout = go.Layout(title = title,38 yaxis = dict(title = 'Flux [µmol/s/m2]'),39 xaxis = dict(title = xaxis_title),40 width = 500,41 )42 fig = go.Figure(data=data, layout=layout)43 44 if save_fig:45 iplot(fig,filename=title,image='svg',image_width=500,image_height=500)46 sleep(5)47 else:48 iplot(fig)49 50def create_bar_plot_met_c3(model, D_fba, L_r, m_id, title, xaxis_title, save_fig = False):51 data = []52 for r_id in L_r:53 r_obj = model.reactions.get_by_id(r_id)54 m_v = r_obj.get_coefficient(m_id)55 trace = go.Bar(y = [abs(m_v)*D_fba[exp][r_id] for exp in sorted(D_fba.keys())],56 x = sorted(D_fba.keys()),57 name = r_id,58 )59 data.append(trace)60 layout = go.Layout(title = title,61 yaxis = dict(title = 'Flux [µmol/s/m2]'),62 xaxis = dict(title = xaxis_title),63 width = 500,64 barmode='stack',65 )66 fig = go.Figure(data=data, layout=layout)67 68 if save_fig:69 iplot(fig,filename=title,image='svg',image_width=500,image_height=500)70 sleep(5)71 else:72 iplot(fig)73 74def create_bar_plot_rxn(D_fba, D_exp, D_rxn, title, xaxis_title, stacked = False, y_max = None, c = False, save_fig = False, absolute = True):75 76 #Coloring of bars77 L_colM = cl.scales['5']['seq']['Oranges']78 L_colM = L_colM[::-1]79 L_colB = cl.scales['5']['seq']['Blues']80 L_colB = L_colB[::-1]81 82 83 if len(D_rxn) >= 5 or c:84 L_colM = cl.scales['11']['qual']['Paired']85 L_colB = cl.scales['11']['qual']['Paired']86 87 if isinstance(D_rxn, list):88 D_rxn = {r_id: r_id for r_id in D_rxn}89 90 D_rid_colM = {r_id2: L_colM[i] for i, r_id2 in enumerate([r_id1 for r_id1 in D_rxn if r_id1[1] == 'M'])}91 D_rid_colB = {r_id2: L_colB[i] for i, r_id2 in enumerate([r_id1 for r_id1 in D_rxn if r_id1[1] == 'B'])}92 D_rid_col = D_rid_colM.copy()93 D_rid_col.update(D_rid_colB)94 95 #Create bar plot96 data = []97 for r_id, r_name in sorted(D_rxn.items()):98 trace = go.Bar(name = r_name,99 y = [abs(D_fba[x][r_id]) for x in sorted(D_fba.keys())] if absolute else [D_fba[x][r_id] for x in sorted(D_fba.keys())],100 x = [D_exp[exp] for exp in sorted(D_fba.keys())],101 marker = {'color': D_rid_col[r_id] if D_rid_col else '#1f77b4'} 102 )103 data.append(trace)104 layout = go.Layout(height = 400,105 width = 600,106 margin = dict(b = 100),107 barmode='stack' if stacked else 'group',108 xaxis= {'title': xaxis_title, 'tickangle':45 if len(D_exp) >= 8 else 0},109 title = title,110 yaxis = { 'title': 'Flux [µmol/s/m2]','range': [0, y_max] if y_max else None,'autorange': False if y_max else True}111 )112 fig = go.Figure(data=data, layout=layout)113 114 if save_fig:115 iplot(fig,image='svg', filename=title,image_width=600, image_height=400)116 sleep(5)117 else:118 iplot(fig)119 120 121def create_bar_plot_met(D_fba, D_exp, D_rxn, m_id, title, xaxis_title, c3_model, stacked = False, y_max = None, c = False, save_fig = False):122 123 #Coloring of bars124 L_colM = cl.scales['5']['seq']['Oranges']125 L_colM = L_colM[::-1]126 L_colB = cl.scales['5']['seq']['Blues']127 L_colB = L_colB[::-1]128 129 130 if len(D_rxn) >= 5 or c:131 L_colM = cl.scales['11']['qual']['Paired']132 L_colB = cl.scales['11']['qual']['Paired']133 134 if isinstance(D_rxn, list):135 D_rxn = {r_id: r_id for r_id in D_rxn}136 137 D_rid_colM = {r_id2: L_colM[i] for i, r_id2 in enumerate([r_id1 for r_id1 in D_rxn if r_id1[1] == 'M'])}138 D_rid_colB = {r_id2: L_colB[i] for i, r_id2 in enumerate([r_id1 for r_id1 in D_rxn if r_id1[1] == 'B'])}139 D_rid_col = D_rid_colM.copy()140 D_rid_col.update(D_rid_colB)141 142 #Create bar plot143 data = []144 for r_id, r_name in sorted(D_rxn.items()):145 r_obj = c3_model.reactions.get_by_id(r_id[4:])146 m_v = r_obj.get_coefficient(m_id)147 trace = go.Bar(name = r_name,148 y = [abs(m_v*D_fba[x][r_id]) for x in sorted(D_fba.keys())],149 x = [D_exp[exp] for exp in sorted(D_fba.keys())],150 marker = {'color': D_rid_col[r_id] if D_rid_col else '#1f77b4'} 151 )152 data.append(trace)153 layout = go.Layout(height = 400,154 width = 600,155 margin = dict(b = 100),156 barmode='stack' if stacked else 'group',157 xaxis= {'title': xaxis_title, 'tickangle':45 if len(D_exp) >= 8 else 0},158 title = title,159 yaxis = {'title': 'Flux [µmol/s/m2]','range': [0, y_max] if y_max else None,'autorange': False if y_max else True})160 fig = go.Figure(data=data, layout=layout)161 162 if save_fig:163 iplot(fig,image='svg', filename=title,image_width=500,image_height=500)164 sleep(5)165 else:166 iplot(fig)167 168def plot_transport_MB(D_fba, D_exp, L_r, xaxis_title, save_fig, cut_off):169 170 df = pd.DataFrame(index = [], columns= [D_exp[exp] for exp in sorted(D_fba.keys())])171 172 yTickNames_MB = []173 174 D_rid_meanFlux_MB = {r_id:abs(np.mean([0]+[D_fba[x][r_id] for x in D_exp if D_fba[x][r_id] > 0])) for r_id in L_r if abs(np.mean([0]+[D_fba[x][r_id] for x in D_exp if D_fba[x][r_id] > 0])) > cut_off}175 D_rid_meanFlux_MB = sorted(D_rid_meanFlux_MB.items(), key=operator.itemgetter(1))176 177 for entry in D_rid_meanFlux_MB:178 r_id = entry[0]179 for x in D_exp:180 if D_fba[x][r_id] > 0:181 df.loc[r_id, D_exp[x]] = abs(D_fba[x][r_id]) if not abs(D_fba[x][r_id]) < cut_off else float('nan')182 # .loc[] to locate a specific value in a dataframe183 # always takes the index AND the column => 2 elements184 # if we want to assign this value, once we located it, we assign it185 186 if not r_id[5:-2] in yTickNames_MB:187 yTickNames_MB.append(r_id[5:-2])188 189 scl = cl.scales['9']['seq']['Reds']190 colorscale = [ [ float(i)/float(len(scl)-1), scl[i] ] for i in range(len(scl))]191 192 trace_MB = go.Heatmap(z=df.values.tolist(),193 x=df.columns,194 y=yTickNames_MB,195 colorbar= {'title':'Flux [µmol/s/m2]', 'titleside':'right'},196 colorscale = colorscale)197 198 layout_MB = go.Layout(width = 500,199 margin = {'b':100},200 yaxis = {'tickmode': 'array', 'tickvals': list(range(0,len(yTickNames_MB))), 'ticktext': yTickNames_MB, 'title': 'Transport Metabolites'},201 xaxis= {'title':xaxis_title,'tickangle':45 if len(D_exp) >= 8 else 0},202 title = 'Mesophyll ==> Bundlesheat Transport')203 data_MB=[trace_MB]204 205 fig_MB = go.Figure(data=data_MB, layout=layout_MB)206 if save_fig:207 iplot(fig_MB,image='svg', filename='transport_MB',image_width=500,image_height=500)208 sleep(5)209 else:210 iplot(fig_MB)211 212 213def plot_transport_BM(D_fba, D_exp, L_r, xaxis_title, save_fig, cut_off):214 215 df = pd.DataFrame(index = [], columns= [D_exp[exp] for exp in sorted(D_fba.keys())])216 217 yTickNames_BM = []218 219 D_rid_meanFlux_BM = {r_id:abs(np.mean([0]+[D_fba[x][r_id] for x in D_exp if D_fba[x][r_id] < 0])) for r_id in L_r if abs(np.mean([0]+[D_fba[x][r_id] for x in D_exp if D_fba[x][r_id] < 0])) > cut_off}220 D_rid_meanFlux_BM = sorted(D_rid_meanFlux_BM.items(), key=operator.itemgetter(1))221 222 for entry in D_rid_meanFlux_BM:223 r_id = entry[0]224 for x in D_exp:225 if D_fba[x][r_id] < 0:226 df.loc[r_id, D_exp[x]] = abs(D_fba[x][r_id]) if not abs(D_fba[x][r_id]) < cut_off else float('nan')227 if not r_id[5:-2] in yTickNames_BM:228 yTickNames_BM.append(r_id[5:-2])229 230 scl = cl.scales['9']['seq']['Blues']231 colorscale = [ [ float(i)/float(len(scl)-1), scl[i] ] for i in range(len(scl)) ]232 233 trace_BM = go.Heatmap(z=df.values.tolist(),234 x=df.columns,235 y=yTickNames_BM,236 colorbar= {'title':'Flux [µmol/s/m2]', 'titleside':'right'},237 colorscale = colorscale)238 239 layout_BM = go.Layout(width = 500,240 margin = {'b':100},241 yaxis = {'tickmode': 'array', 'tickvals': list(range(0,len(yTickNames_BM))), 'ticktext': yTickNames_BM, 'title': 'Transport Metabolites'},242 xaxis= {'title':xaxis_title,'tickangle':45 if len(D_exp) >= 8 else 0},243 title = 'Bundlesheat ==> Mesophyll Transport')244 data_BM=[trace_BM]245 246 fig_BM = go.Figure(data=data_BM, layout=layout_BM,)247 248 if save_fig:249 iplot(fig_BM,image='svg', filename='transport_BM',image_width=500,image_height=500)250 sleep(5)251 else:252 iplot(fig_BM) 253 254 255def plot_transport(D_fba, D_exp, L_r, xaxis_title, save_fig = False):256 257 cut_off = 0.1258 259 plot_transport_MB(D_fba, D_exp, L_r, xaxis_title, save_fig, cut_off)260 plot_transport_BM(D_fba, D_exp, L_r, xaxis_title, save_fig, cut_off)261 262def plot_transport_fva(c4_model, D_exp, D_pfva, D_pfba, L_r_org, flux_max, title, save_fig=False, L_r_index=None):263 264 L_colors = ply.colors.DEFAULT_PLOTLY_COLORS265 266 if len(D_exp) > len(L_colors):267 print('Too many experiments.')268 else:269 p_value = 0.1 #p value270 271 #prepare data for plotting272 D_r_flux_range = {}273 D_exp_r_FVA ={}274 data = []275 276 for exp in D_exp:277 D_exp_r_FVA[exp] = {}278 for r_id in L_r_org:279 D_FVA = {}280 r_obj = c4_model.reactions.get_by_id(r_id)281 D_FVA['max'] = D_pfva[exp].loc[r_id,'maximum']282 D_FVA['min'] = D_pfva[exp].loc[r_id,'minimum']283 D_FVA['flux'] = D_pfba[exp][r_id]284 285 #filter flux ranges of particular size (p_value % of maximum C transport rate)286 if abs(D_FVA['max']) > p_value * flux_max or abs(D_FVA['min']) > p_value * flux_max:287 #determine maximum flux range over all exp for each reaction288 if not r_id in D_r_flux_range: 289 D_r_flux_range[r_id] = D_FVA['max'] - D_FVA['min']290 else:291 if D_r_flux_range[r_id] < (D_FVA['max'] - D_FVA['min']):292 D_r_flux_range[r_id] = D_FVA['max'] - D_FVA['min']293 294 D_exp_r_FVA[exp][r_id] = D_FVA295 296 #sort reactions by size of flux range297 L_r = [r_flux_range[0] for r_flux_range in sorted(D_r_flux_range.items(), key=operator.itemgetter(1), reverse= True)]298 299 create_plot = True300 301 if L_r_index:302 L_r_ex1 = list(set(L_r_index)-set(L_r))303 L_r_ex2 = list(set(L_r)-set(L_r_index))304 L_r_ex = L_r_ex1 + L_r_ex2305 if not L_r_ex:306 L_r = L_r_index307 else:308 print('Warning: L_r and L_r_index are not matching: %s' %L_r_ex)309 create_plot = False310 311 if create_plot:312 #set up x-values for each experiment313 L_x_axis = np.arange(1,len(L_r)*2+1,2)314 D_exp_x_axis = {exp: [] for exp in D_exp}315 for x in L_x_axis:316 L_x_exp = np.linspace(x-0.3,x+0.3,len(D_exp))317 for i_exp, exp in enumerate(D_exp.keys()):318 D_exp_x_axis[exp].append(L_x_exp[i_exp]) 319 #prepare trace for each experiment320 for i_exp, (exp,exp_name) in enumerate(D_exp.items()):321 L_r_flux = [D_exp_r_FVA[exp][r_id]['flux'] for r_id in L_r]322 L_r_min= [D_exp_r_FVA[exp][r_id]['flux'] - D_exp_r_FVA[exp][r_id]['min'] for r_id in L_r]323 L_r_max = [D_exp_r_FVA[exp][r_id]['max'] - D_exp_r_FVA[exp][r_id]['flux'] for r_id in L_r]324 325 trace = go.Scatter(326 x = D_exp_x_axis[exp],327 y = L_r_flux,328 error_y={'type':'data', 'symmetric':False, 'array':L_r_max, 'arrayminus':L_r_min},329 marker = {'color' : L_colors[i_exp]},330 name = exp_name,331 mode = 'markers'332 )333 334 data.append(trace)335 #prepare layout336 layout = go.Layout(337 xaxis = {'tickvals':L_x_axis, 'ticktext':[r_id[5:-2] for r_id in L_r], 'title':'Exchange Metabolites'},338 #margin = {'b': 300},339 title = title,340 yaxis = {'title':'Flux [µmol/s/m2]'},341 legend = {'orientation':'h', 'x':0, 'y':-0.15},342 shapes = [343 {344 'type': 'rect',345 'x0': -0.1,346 'y0': -0.5,347 'x1': -1.5,348 'y1': -50,349 'line': {350 'color': 'rgba(255, 255, 255, 1)',351 'width': 1,352 },353 'fillcolor': 'rgba(255, 255, 255, 1)',354 },355 {356 'type': 'rect',357 'x0': -0.1,358 'y0': 0.5,359 'x1': -1.5,360 'y1': 50,361 'line': {362 'color': 'rgba(255, 255, 255, 1)',363 'width': 1,364 },365 'fillcolor': 'rgba(255, 255, 255, 1)',366 },367 ],368 annotations=[369 dict(370 x=-0.5,371 y=25,372 xref='x',373 yref='y',374 text='Mesophyll -> Bundle Sheath',375 textangle= -90,376 showarrow=False,377 font=dict(378 size=10,379 color='#000000'380 ),381 align='center',382 ),383 dict(384 x=-0.5,385 y=-25,386 xref='x',387 yref='y',388 text='Bundle Sheath -> Mesophyll',389 textangle= -90,390 showarrow=False,391 font=dict(392 size=10,393 color='#000000'394 ),395 align='center',396 397 )398 ]399 )400 401 #create figure402 fig = go.Figure(data=data, layout=layout)403 if save_fig:404 iplot(fig,image='svg', filename='FVA')405 sleep(5)406 else:407 iplot(fig) 408 409def get_fluxes_by_metabolite(D_exp, D_fba, model, m_id, cell):410 if cell in ['M','B']:411 df = pd.DataFrame(columns=['rxn']+[D_exp[exp] for exp in D_exp])412 m_obj = model.metabolites.get_by_id('['+cell+']_'+m_id)413 for r_obj in m_obj.reactions:414 if not r_obj.id in df.index:415 df.loc[r_obj.id] = [r_obj.reaction] + [round(D_fba[exp][r_obj.id],3) for exp in D_exp]416 return df417 else:418 return 'Choose either M or B as cell type'...
init_nuisance.py
Source:init_nuisance.py
1#!/usr/bin/env python32from scipy.optimize import minimize3import numpy as np4from scipy.optimize import fmin_slsqp5from scipy.optimize import fsolve6# from scipy.optimize import minimize7from scipy.optimize import leastsq8from instagraal.leastsqbound import *9d0 = 1.0 # distance bias Hi-C10d_exp = -10.011import matplotlib.pyplot as plt12def log_residuals_4_min(param, y, x):13 d_init, alpha_0, alpha_1, A = param14 hic_c = np.zeros(x.shape)15 log_val_lim_0 = (16 np.log(A)17 + (alpha_0 - alpha_1) * np.log(d_init)18 + ((d_exp - 2) / (np.power(d_init, 2) + d_exp))19 )20 for i in range(0, len(hic_c)):21 if x[i] < d_init and x[i] > 0:22 hic_c[i] = (23 np.log(A)24 + np.log(x[i]) * alpha_025 + ((d_exp - 2) / (np.power(x[i], 2) + d_exp))26 )27 else:28 hic_c[i] = log_val_lim_0 + np.log(x[i]) * alpha_129 err = np.sqrt(np.power(y - hic_c, 2).sum())30 return err31def log_residuals(param, y, x):32 alpha_0, alpha_1, A = param33 hic_c = np.zeros(x.shape)34 log_val_lim_0 = (35 np.log(A)36 + (alpha_0 - alpha_1) * np.log(d0)37 + ((d_exp - 2) / (np.power(d0, 2) + d_exp))38 )39 for i in range(0, len(hic_c)):40 if x[i] <= 0:41 hic_c[i] = 042 elif x[i] < d0 and x[i] > 0:43 hic_c[i] = (44 np.log(A)45 + np.log(x[i]) * alpha_046 + ((d_exp - 2) / (np.power(x[i], 2) + d_exp))47 )48 else:49 hic_c[i] = log_val_lim_0 + np.log(x[i]) * alpha_150 err = y - hic_c51 return err52def residuals(param, y, x):53 alpha_0, alpha_1, A = param54 hic_c = np.zeros(x.shape)55 val_lim_0 = A * np.power(d0, alpha_0 - alpha_1)56 for i in range(0, len(hic_c)):57 if x[i] < d0:58 hic_c[i] = A * np.power(x[i], alpha_0)59 else:60 hic_c[i] = val_lim_0 * np.power(x[i], alpha_1)61 err = y - hic_c62 return err63def peval(x, param):64 d_init, alpha_0, alpha_1, A = param65 hic_c = np.zeros(x.shape)66 val_lim_0 = (67 A68 * np.power(d_init, alpha_0 - alpha_1)69 * np.exp((d_exp - 2) / (np.power(d_init, 2) + d_exp))70 )71 for i in range(0, len(hic_c)):72 if x[i] < d_init:73 hic_c[i] = (74 A75 * np.power(x[i], alpha_0)76 * np.exp((d_exp - 2) / (np.power(x[i], 2) + d_exp))77 )78 else:79 hic_c[i] = val_lim_0 * np.power(x[i], alpha_1)80 return hic_c81def estimate_param_hic(y_meas, x_bins):82 alpha_0 = -1083 alpha_1 = -1.584 x0 = x_bins.min()85 print("x0 = ", x0)86 A = (87 y_meas.max()88 * (x0 ** (-alpha_0))89 / np.exp((d_exp - 2) / (x0 ** 2 + d_exp))90 )91 print("A = ", A)92 p0 = [alpha_0, alpha_1, A]93 args = (np.log(y_meas), x_bins)94 plsq = leastsq(log_residuals, p0, args=args)95 plsq[0]96 print(plsq)97 bnds = ((0, 3), (-10, -0.2), (-2, -0.2), (0, None))98 alpha_0, alpha_1, A = plsq[0]99 p0 = [d0, alpha_0, alpha_1, A]100 # cns = ({'type': 'ineq', 'fun': lambda x: x[0] - x[1]})101 # alpha_0 > alpha_1102 cns = {"type": "ineq", "fun": lambda x: x[1] - x[0]}103 res = minimize(104 log_residuals_4_min,105 p0,106 args=args,107 method="L-BFGS-B",108 bounds=bnds,109 constraints=cns,110 options={"disp": True},111 )112 print("res = ", res)113 y_estim_sls = peval(x_bins, res.x)114 plt.loglog(x_bins, y_estim_sls)115 plt.loglog(x_bins, y_meas)116 plt.show()117 return res, y_estim_sls118def residual_4_max_dist(x, p):119 d_init, alpha_0, alpha_1, A, y = p120 hic_c = np.zeros(x.shape)121 val_lim_0 = (122 A123 * np.power(d_init, alpha_0 - alpha_1)124 * np.exp((d_exp - 2) / (np.power(d_init, 2) + d_exp))125 )126 for i in range(0, len(hic_c)):127 if x[i] < d_init:128 hic_c[i] = (129 A130 * np.power(x[i], alpha_0)131 * np.exp((d_exp - 2) / (np.power(x[i], 2) + d_exp))132 )133 else:134 hic_c[i] = val_lim_0 * np.power(x[i], alpha_1)135 err = y - hic_c136 return err137def estimate_max_dist_intra(p, val_inter):138 print("val_inter = ", val_inter)139 d_init, alpha_0, alpha_1, A = p140 p0 = [d_init, alpha_0, alpha_1, A, val_inter]141 s0 = 500142 x = fsolve(residual_4_max_dist, s0, args=(p0))143 print("limit inter/intra distance = ", x)144 print("val model @ dist inter = ", peval(x, p))...
softmax.py
Source:softmax.py
1import numpy as np2from random import shuffle3def softmax_loss_naive(W, X, y, reg):4 """5 Softmax loss function, naive implementation (with loops)6 Inputs have dimension D, there are C classes, and we operate on minibatches7 of N examples.8 Inputs:9 - W: A numpy array of shape (D, C) containing weights.10 - X: A numpy array of shape (N, D) containing a minibatch of data.11 - y: A numpy array of shape (N,) containing training labels; y[i] = c means12 that X[i] has label c, where 0 <= c < C.13 - reg: (float) regularization strength14 Returns a tuple of:15 - loss as single float16 - gradient with respect to weights W; an array of same shape as W17 """18 # Initialize the loss and gradient to zero.19 loss = 0.020 dW = np.zeros_like(W)21 num_train = X.shape[0]22 #############################################################################23 # TODO: Compute the softmax loss and its gradient using explicit loops. #24 # Store the loss in loss and the gradient in dW. If you are not careful #25 # here, it is easy to run into numeric instability. Don't forget the #26 # regularization! #27 #############################################################################28 # Loss29 d = X.dot(W) # NumInstances x Classes30 d -= np.max(d, axis=1).reshape(-1, 1) # For numeric stability31 d_exp = np.exp(d) # Raise all of them to Exp32 d_exp_sums = np.sum(d_exp, axis=1).reshape(-1, 1) # Calculate the denominators33 d_exp_reg = d_exp / d_exp_sums # Normalize34 Li = -np.log(d_exp_reg[np.arange(len(d_exp_reg)), y])35 loss = np.sum(Li) / num_train # Data loss36 loss += 0.5 * reg * np.sum(W * W) # Add the regularization loss37 38 # Gradient39 # https://eli.thegreenplace.net/2016/the-softmax-function-and-its-derivative/40 # for derivation41 probs = d_exp_reg42 probs[np.arange(num_train),y] -= 143 dW = X.T.dot(probs)44 dW /= num_train45 dW += reg * W46 #############################################################################47 # END OF YOUR CODE #48 #############################################################################49 return loss, dW50def softmax_loss_vectorized(W, X, y, reg):51 """52 Softmax loss function, vectorized version.53 Inputs and outputs are the same as softmax_loss_naive.54 """55 # Initialize the loss and gradient to zero.56 loss = 0.057 dW = np.zeros_like(W)58 num_train = X.shape[0]59 #############################################################################60 # TODO: Compute the softmax loss and its gradient using no explicit loops. #61 # Store the loss in loss and the gradient in dW. If you are not careful #62 # here, it is easy to run into numeric instability. Don't forget the #63 # regularization! #64 #############################################################################65 # Loss66 d = X.dot(W) # NumInstances x Classes67 d -= np.max(d, axis=1).reshape(-1, 1) # For numeric stability68 d_exp = np.exp(d) # Raise all of them to Exp69 d_exp_sums = np.sum(d_exp, axis=1).reshape(-1, 1) # Calculate the denominators70 d_exp_reg = d_exp / d_exp_sums # Normalize71 Li = -np.log(d_exp_reg[np.arange(len(d_exp_reg)), y])72 loss = np.sum(Li) / num_train # Data loss73 loss += 0.5 * reg * np.sum(W * W) # Add the regularization loss74 75 # Gradient76 probs = d_exp_reg77 probs[np.arange(num_train),y] -= 178 dW = X.T.dot(probs)79 dW /= num_train80 dW += reg * W81 #############################################################################82 # END OF YOUR CODE #83 #############################################################################...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!