0
$\begingroup$

When hovering over the contour graph, I got a negative value for the height. However the height is actually the cost function so it will always be positive. Here's the screenshot:

enter image description here

Clearly I can see the scales for the height are all positive for 3 plots, but the z value is not always. Could anyone help to explain why this happened?

Just for your reference, here's the code I used to plot:

import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
import numpy as np
import math

def pred_fn(w, x):
    return np.matmul(x, w)

def cost_fn(w, Y):
    Y_hat = pred_fn( w, X )
    return np.sum( ( Y_hat - Y ) ** 2 )

# **Warning:** Don't use broadcast operator, it's very complicated.
# W = np.hstack((W1.reshape(-1, 1),W2.reshape(-1, 1))).reshape(w2.size, w1.size, 2)
def compute_cost(w1, w2, Y, J):
    for i, w2_v in enumerate(w2):
        for j, w1_v in enumerate(w1):
            J[i, j] = cost_fn(np.array( [w1_v, w2_v] ), Y)
    return J

def add_subplot(row_index, col_index):
    ax = fig.add_subplot(row_index, col, col_index, projection='3d')
    W1, W2 = np.meshgrid(w1, w2)
    J = np.zeros((w2.size, w1.size))
    ax.plot_surface(W1, W2, compute_cost(w1, w2, Y, J), rstride=1, cstride=1,
                    cmap='viridis', edgecolor='none')
    ax.set_title('%d.%d' %(row_index, col_index));

# -------
fig = plt.figure()
col = 3

# 1st
x1 = np.linspace(-10, 10, 20)
x2 = np.linspace(-500, 1000, 20)
X = np.hstack((x1.reshape(-1, 1), x2.reshape(-1, 1)))
Y = 3*x1 + 3*x2
w1 = np.linspace(0, 16, 20)
w2 = np.linspace(-30, 28, 30)
add_subplot(1, 1)

x1 = np.linspace(-1, 1, 20)
x2 = np.linspace(-1, 1, 20)
X = np.hstack((x1.reshape(-1, 1), x2.reshape(-1, 1)))
Y = 3*x1 + 3*x2
w1 = np.linspace(2.5, 3.5, 100)
w2 = np.linspace(2.5, 3.5, 100)
add_subplot(1, 2)

x1 = np.linspace(-10, 10, 50)
x2 = np.linspace(-20, 10, 50)
X = np.hstack((x1.reshape(-1, 1), x2.reshape(-1, 1)))
Y = 3*x1 + 3*x2
w1 = np.linspace(-140, 106, 120)
w2 = np.linspace(-50, 58, 230)
add_subplot(1, 3)

plt.show()
$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.