How to make curved sections in matplotlib

I try to imagine how two-dimensional data is transformed and “bent” when they pass through layers of a neural network. Affine transformations and translations are simple, but visualizing how the activation function (like tanh or the logistic function) bends two-dimensional space into a curved grid poses a big problem.

To understand what I mean, Chris Olah did just that in his post Neural Networks, Manifolds, and Topology.

enter image description here

Do any of you know how to do this?

+4
source share
1 answer

I got the following solution:

, NumPy linspace:

x_range = range(-5,6)
y_range = range(-5,6)

lines = np.empty((len(x_range)+len(y_range), 2, 100))

for i in x_range: # vertical lines
    linspace_x = np.linspace(x_range[i], x_range[i], 100)
    linspace_y = np.linspace(min(y_range), max(y_range), 100)
    lines[i] = (linspace_x, linspace_y)
for i in y_range: # horizontal lines
    linspace_x = np.linspace(min(x_range), max(x_range), 100)
    linspace_y = np.linspace(y_range[i], y_range[i], 100)
    lines[i+len(x_range)] = (linspace_x, linspace_y)

. ( - .)

def affine(z):
    z[:, 0] = z[:, 0] + z[:,1] * 0.3 # transforming the x coordinates
    z[:, 1] = 0.5 * z[:, 1] - z[:, 0] * 0.8 # transforming the y coordinates
    return z

transformed_lines = affine(lines)

, : ( ) , , ( ):

def sigmoid(z):
    return 1.0/(1.0+np.exp(-z))

bent_lines = sigmoid(transformed_lines)

matplotlib:

plt.figure(figsize=(8,8))
plt.axis("off")
for line in bent_lines:
    plt.plot(line[0], line[1], linewidth=0.5, color="k")
plt.show()

:

enter image description here

+1

Source: https://habr.com/ru/post/1668714/


All Articles