예제 #1
0
def sig(X):
    try:
        y = ADnum(logistic(X.val),
                  der=X.der * logistic(X.val) * (1 - logistic(X.val)),
                  ops=(1 - X.counted) * X.ops + 3,
                  tfops=X.tfops + 2 + X.ins,
                  trops=X.trops + 2 + 2 * X.ins)
        X.counted = 1
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'sig', logistic(X.val) * (1 - logistic(X.val))))
        return y
    except AttributeError:
        return logistic(X)
예제 #2
0
def sin(X):
    try:
        y = ADnum(np.sin(X.val),
                  der=np.cos(X.val) * X.der,
                  ops=(1 - X.counted) * X.ops + 2,
                  rops=1,
                  tfops=X.tfops + 2 + X.ins,
                  trops=X.trops + 2 + 2 * X.ins)
        X.counted = 1
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'sin', np.cos(X.val)))
        return y
    except AttributeError:
        return np.sin(X)
예제 #3
0
def tan(X):
    try:
        y = ADnum(np.tan(X.val),
                  der=(1 / np.cos(X.val)**2) * X.der,
                  ops=(1 - X.counted) * X.ops + 4,
                  rops=3,
                  tfops=X.tfops + 2 + X.ins,
                  trops=X.trops + 2 + 2 * X.ins)
        X.counted = 1
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'tan', (1 / np.cos(X.val))**2))
        return y
    except AttributeError:
        return np.tan(X)
예제 #4
0
def sqrt(X):
    try:
        y = ADnum(np.sqrt(X.val),
                  der=X.der / (2 * np.sqrt(X.val)),
                  ops=(1 - X.counted) * X.ops + 3,
                  rops=3,
                  tfops=X.tfops + 2 + X.ins,
                  trops=X.trops + 2 + 2 * X.ins)
        X.counted = 1
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'sqrt', 1 / (2 * np.sqrt(X.val))))
        return y
    except AttributeError:
        return np.sqrt(X)
예제 #5
0
def arccos(X):
    try:
        y = ADnum(np.arccos(X.val),
                  der=-1 / np.sqrt(1 - X.val**2) * X.der,
                  ops=(1 - X.counted) * X.ops + 5,
                  rops=4,
                  tfops=X.tfops + 2 + X.ins,
                  trops=X.trops + 2 + 2 * X.ins)
        X.counted = 1
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'arccos', -1 / np.sqrt(1 - X.val**2)))
        return y
    except AttributeError:
        return np.arccos(X)
예제 #6
0
def csc(X):
    try:
        y = ADnum(1 / np.sin(X.val),
                  der=(-1 / np.tan(X.val)) * (1 / np.sin(X.val)) * X.der,
                  ops=(1 - X.counted) * X.ops + 5,
                  rops=4,
                  tfops=X.tfops + 2 + X.ins,
                  trops=X.trops + 2 + 2 * X.ins)
        X.counted = 1
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append(
            (y, 'csc', (-1 / np.tan(X.val)) * (1 / np.sin(X.val))))
        return y
    except:
        return 1 / np.sin(X)
예제 #7
0
def relu(X):
    try:
        if X.val > 0:
            y = ADnum(X.val,
                      der=1 * X.der,
                      ops=(1 - X.counted) * X.ops + 1,
                      tfops=X.tfops + 1 + X.ins,
                      trops=X.trops + 1 + X.ins)
        else:
            y = ADnum(0,
                      der=0 * X.der,
                      ops=(1 - X.counted) * X.ops + 1,
                      tfops=X.tfops + 1 + X.ins,
                      trops=X.trops + 1 + X.ins)
        X.counted = 1
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        if X.val > 0:
            y.graph[X].append((y, 'relu', 1))
        else:
            y.graph[X].append((y, 'relu', 0))
        return y
    except AttributeError:
        return np.maximum(X, 0)
예제 #8
0
파일: ADgraph.py 프로젝트: ninonb/Auto-eD
def plot_ADnum(f, ins=1, xmin=-10, xmax=10):
    '''Function to plot f and its derivative for single variable input

    INPUTS
    ======
    x : ADnum
    xmin : starting value of input
    xmax : ending value of input

    OUTPUTS
    =======
    A plot of x evaluated from xmin to xmax and its derivative
    '''
    if ins == 1:
        vals = np.linspace(xmin, xmax, 100)
        evals = [f(ADnum(value, der=1)).val for value in vals]
        ders = [f(ADnum(value, der=1)).der for value in vals]
        fig = plt.figure()
        plt.plot(vals, evals, label='f', linewidth=2)
        plt.plot(vals, ders, label='df/dx', linewidth=2)
        plt.legend(fontsize=20)
        plt.xlabel('x', fontsize=20)
        plt.ylabel('f', fontsize=20)
        plt.xticks(fontsize=12)
        plt.yticks(fontsize=12)
        return fig
    if ins == 2:
        fig = plt.figure()
        ax = fig.gca(projection='3d')
        vals = np.linspace(xmin, xmax, 100)
        z = f(vals, vals)
        ax.plot_trisurf(vals, vals, z, antialiased=True)
        return fig
    if ins > 2:
        fig = plt.figure()
        return fig
예제 #9
0
def graphwindow():
    #global show_table
    #global visfunc
    errors = ""

    # redirect to start page if session expired
    if ('master_ins' not in session):
        session['refresh_message'] = 'Your session has expired, please start again!'
        return redirect(url_for('startup'))

    # also redirect to start page, if user erroneously moved back and forth on web
    # ex. graph page -> click previous arrow back to main -> attempt to click forward arrow back to graph
    if any(session['func_content']) == False: # if all function contents are empty
        session['refresh_message'] = 'Your session has expired, please start again!'
        return redirect(url_for('startup'))


    if request.method == "POST":
        
        action = request.form["action"]
        if request.form["action"] == "Set Input Values":
            session['show_table'] = False
            #global varlist
            session['varlist'] = []
            try:
                #global x
                session['x'] = [None]*session['master_outs']
                for i in range(session['master_outs']):
                    session['x'][i]  = ADnum(float(request.form["x"]), ins=session['master_ins'], ind=0)
                session['var_strs']['x']=request.form["x"]
                session['varlist'].append(session['x'])
                if session['master_ins']>1:
                    #global y
                    session['y'] = [None]*session['master_outs']
                    for i in range(session['master_outs']):
                        session['y'][i]=ADnum(float(request.form["y"]), ins=session['master_ins'], ind=1)
                    session['var_strs']['y']=request.form["y"]
                    session['varlist'].append(session['y'])
                if session['master_ins']>2:
                    #global z
                    session['z'] = [None]*session['master_outs']
                    for i in range(session['master_outs']):
                        session['z'][i]=ADnum(float(request.form["z"]), ins=session['master_ins'], ind=2)
                    session['var_strs']['z'] = request.form['z']
                    session['varlist'].append(session['z'])
                if session['master_ins']>3:
                    #global u
                    session['u'] = [None]*session['master_outs']
                    for i in range(session['master_outs']):
                        session['u'][i] = ADnum(float(request.form["u"]), ins=session['master_ins'], ind=3)
                    session['var_strs']['u'] = request.form["u"]
                    session['varlist'].append(session['u'])
                if session['master_ins']>4:
                    #global v
                    session['v'] = [None]*session['master_outs']
                    for i in range(session['master_outs']):
                        session['v'][i]=ADnum(float(request.form["v"]), ins=session['master_ins'], ind=4)
                    session['var_strs']['v']=request.form["v"]
                    session['varlist'].append(session['v'])
                build_function()    
                
                return render_template('graph2.html', ins=session['master_ins'], outs = session['master_outs'], errors=errors, var_strs=session['var_strs'], flabels=session['flabels'], func_content=session['func_content'], full=True, show_vis=True, val=session['disp_val'], der = session['disp_der'], show_table=False, func_select=False, constants=session['constant_fs'])
            except:
                errors += "Please enter numeric values for all of the inputs."
        
        # else:
        #     #global curr_idx
        #     # global rev_dyn_set

        #     if request.form["action"] == "prev":
        #         session['curr_idx'] = session['curr_idx']-1
        #     if request.form["action"] == "next":
        #         session['curr_idx'] = session['curr_idx']+1
        #     return render_template('graph2.html', visfunc=session['visfunc'], ins=session['master_ins'], outs=session['master_outs'], errors=errors, var_strs=session['var_strs'], flabels=session['flabels'], func_content=session['func_content'], full=True, val=session['disp_val'], der=session['disp_der'], func_select=True, table=table)

    
    return render_template('graph2.html', ins=session['master_ins'], outs=session['master_outs'], errors=errors, var_strs=session['var_strs'], flabels = session['flabels'], func_content=session['func_content'], full=False, show_vis = False, show_table=False, func_select=False, constants=session['constant_fs'])
예제 #10
0
def graphwindow():
    global show_table
    global visfunc
    errors = ""
    if request.method == "POST":
        action = request.form["action"]
        if request.form["action"] == "Set Input Values":
            show_table = False
            global varlist
            varlist = []
            try:
                global x
                x = [None] * master_outs
                for i in range(master_outs):
                    x[i] = ADnum(float(request.form["x"]),
                                 ins=master_ins,
                                 ind=0)
                var_strs['x'] = request.form["x"]
                varlist.append(x)
                if master_ins > 1:
                    global y
                    y = [None] * master_outs
                    for i in range(master_outs):
                        y[i] = ADnum(float(request.form["y"]),
                                     ins=master_ins,
                                     ind=1)
                    var_strs['y'] = request.form["y"]
                    varlist.append(y)
                if master_ins > 2:
                    global z
                    z = [None] * master_outs
                    for i in range(master_outs):
                        z[i] = ADnum(float(request.form["z"]),
                                     ins=master_ins,
                                     ind=2)
                    var_strs['z'] = request.form['z']
                    varlist.append(z)
                if master_ins > 3:
                    global u
                    u = [None] * master_outs
                    for i in range(master_outs):
                        u[i] = ADnum(float(request.form["u"]),
                                     ins=master_ins,
                                     ind=3)
                    var_strs['u'] = request.form["u"]
                    varlist.append(u)
                if master_ins > 4:
                    global v
                    v = [None] * master_outs
                    for i in range(master_outs):
                        v[i] = ADnum(float(request.form["v"]),
                                     ins=master_ins,
                                     ind=4)
                    var_strs['v'] = request.form["v"]
                    varlist.append(v)
                build_function()
                return render_template('graph2.html',
                                       ins=master_ins,
                                       outs=master_outs,
                                       errors=errors,
                                       var_strs=var_strs,
                                       flabels=flabels,
                                       func_content=func_content,
                                       full=True,
                                       val=disp_val,
                                       der=disp_der,
                                       show_table=False,
                                       func_select=False)
            except:
                errors += "Please enter numeric values for all of the inputs."

        else:
            global curr_idx
            global rev_dyn_set
            if request.form["action"] == "f1":
                visfunc = 0
                curr_idx = 0
                rev_dyn_set = []
            if request.form["action"] == "f2":
                visfunc = 1
                curr_idx = 0
                rev_dyn_set = []
            if request.form["action"] == "f3":
                visfunc = 2
                curr_idx = 0
                rev_dyn_set = []
            table = get_table()
            if request.form["action"][0] == 'd':
                curr_idx = 0
                action = request.form["action"]
                i = int(action[-2])
                var = varlist[int(action[-1])]
                rev_dyn_set = ADgraph.get_rev_dynamic_outs(
                    out_num[i], var[i].revder(out_num[i])[1], G[i],
                    edge_labs[i], pos[i], labs[i],
                    var[i].revder(out_num[i])[0])
            if request.form["action"] == "prev":
                curr_idx = curr_idx - 1
            if request.form["action"] == "next":
                curr_idx = curr_idx + 1
            return render_template('graph2.html',
                                   visfunc=visfunc,
                                   ins=master_ins,
                                   outs=master_outs,
                                   errors=errors,
                                   var_strs=var_strs,
                                   flabels=flabels,
                                   func_content=func_content,
                                   full=True,
                                   val=disp_val,
                                   der=disp_der,
                                   func_select=True,
                                   table=table)
        #if action[0]=="g":
        #if request.form["action"]=="Computational Graph":
        #   comp_graph(int(action[-1]))
        #  if show_table:
        #     df = ADgraph.gen_table(out_num[int(action[-1])])
        #    table = df.to_html(index=False)
        #ADgraph.draw_graph2(out_num[0], G[0], edge_labs[0], pos[0], labs[0])
        #else:
        #   table = 0
        #return render_template('graph.html', ins=master_ins, outs=master_outs, errors=errors, var_strs=var_strs, flabels=flabels, func_content=func_content, full=True, val=disp_val, der=disp_der, show_table=show_table, tables=table)
        #comp_graph(int(action[-1]))
        #if action[0]=="t":
        #   df = ADgraph.gen_table(out_num[int(action[-1])])
        #  table = df.to_html(index=False)
        # show_table = True
        #return render_template('graph.html', ins=master_ins, outs=master_outs, errors=errors, var_strs=var_strs, flabels=flabels, func_content=func_content, full=True, val=disp_val, der=disp_der, show_table = show_table, tables=table)
        #if action[0]=='r':
        #if request.form["action"]=="Reverse Graph":
        #   rev_graph(int(action[-1]))
        #ADgraph.draw_graph_rev2(out_num[0], G[0], edge_labs[0], pos[0], labs[0])
        #  return render_template('graph.html', ins=master_ins, outs=master_outs, errors=errors, var_strs=var_strs, flabels=flabels, func_content=func_content, full=True, val=disp_val, der=disp_der, show_table=show_table)
        #if action[0]=='d':
        #if request.form["action"]=="Rev Dynamic":
        #   rev_dynamic(int(action[-2]), varlist[int(action[-1])])
        #ADgraph.draw_graph_rev_dynamic(out_num[0], x[0].revder(out_num[0])[1], G[0], edge_labs[0], pos[0], labs[0], x[0].revder(out_num[0])[0])
        #return render_template('graph.html', ins=master_ins, outs=master_outs, errors=errors, var_strs=var_strs, flabels=flabels, func_content=func_content, full=True, val=disp_val, der=disp_der, show_table=show_table)
    return render_template('graph2.html',
                           ins=master_ins,
                           outs=master_outs,
                           errors=errors,
                           var_strs=var_strs,
                           flabels=flabels,
                           func_content=func_content,
                           full=False,
                           show_table=False,
                           func_select=False)