In [1]:
# Import dependencies
import numpy as np

# Plotly tools
import plotly
import plotly.graph_objs as go
# Initiate Offlie Interactive Mode
plotly.offline.init_notebook_mode()

# Import IOA
import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path)
import IOA



In [18]:
# 2D Gradient Descent
def parabola():
    x = np.linspace(-5, 5, 100)
    y = x ** 2
    g = go.Scatter(x = x, y = y, line = dict(width = 6))
    lay = go.Layout(xaxis = dict(range= [-10, 10],
                                showgrid = False,
                                zeroline = False,
                                showline = False,
                                showticklabels = False),
                   yaxis = dict(showgrid = False,
                                zeroline = False,
                                showline = False,
                                showticklabels = False))
    fig = go.Figure(data = [g], layout = lay)
    plotly.offline.iplot(fig)
parabola()



In [3]:
def LinearRegression():    
    # Training Data
    # Target -> 2x + 3

    # Number of samples
    n = 100

    # Fuction and mapping
    def f(x): return 2 * x + 3
    x = [i for i in range(n)]
    y = [f(i) for i in x]

    # Make Loss Function 3D Surface

    # Make tweak-able prarameters
    mSpace = np.linspace(-10, 10, 200)
    bSpace = np.linspace(-10, 10, 200)
    m, b = np.meshgrid(mSpace, bSpace)

    # Loss function
    loss = (1 / (2 * n)) * sum([(((m * i) + b) - j) ** 2 for i, j in zip(x, y)])
    # Create the plot
    surface = go.Surface(x = m, y = b, z = loss)

    # Layout
    layout = go.Layout(title = "Loss Function in Linear Regression")

    # Create Figure
    fig = go.Figure(data = [surface], layout = layout)

    # Plot
    plotly.offline.iplot(fig)
LinearRegression()



In [22]:
# IOA
def rr(x, b, t):
    return (t - b)/(1 + np.exp(-x)) + b

def InputOpt(f, b, t, showNorm = True):
    # Make tweak-able prarameters
    xSpace = np.linspace(-30, 30, 200)
    ySpace = np.linspace(-30, 30, 200)
    x, y = np.meshgrid(xSpace, ySpace)
    # Create normal surface
    zNorm = f(x, y)
    surfaceNorm = go.Surface(x = x, y = y, z = zNorm, showscale = False,
                             colorscale = [[0, 'rgb(198,40,40)'], [1, 'rgb(21,101,192)'], [2, 'rgb(21,101,192)']])
    # Create the restricted plot
    xrr = rr(x, b, t)
    yrr = rr(y, b, t)
    zRR = f(xrr, yrr) + 1
    surfaceRR = go.Surface(x = xrr, y = yrr, z = zRR, showscale = False,
                          colorscale = [[0, 'rgb(198,40,40)'], [1, 'rgb(198,40,40)']])
    # Layout
    layout = go.Layout(scene = dict(xaxis = dict(title = "",
                                                showgrid = False,
                                                zeroline = False,
                                                showline = False,
                                                showticklabels = False),
                                   yaxis = dict(title = "",
                                                showgrid = False,
                                                zeroline = False,
                                                showline = False,
                                                showticklabels = False),
                                   zaxis = dict(title = "",
                                                showgrid = False,
                                                zeroline = False,
                                                showline = False,
                                                showticklabels = False)))
    # Create Figure
    if showNorm:
        fig = go.Figure(data = [surfaceRR, surfaceNorm], layout = layout)
    else:
        fig = go.Figure(data = [surfaceRR], layout = layout)
    # Plot
    plotly.offline.iplot(fig)
    
def f1(x, y): return -(x**2 + y**2) ** (1 / 2)
def f2(x, y): return np.sin(.003 * (x ** 2 + y **2))
def f3(x, y): return -(x ** 2 + y ** 2)
InputOpt(f3, 4, 20, showNorm = True)



In [ ]: