DEV Community

Cover image for Understanding Multiple Inputs in Neural Networks - With Python Examples
Rijul Rajesh
Rijul Rajesh

Posted on

Understanding Multiple Inputs in Neural Networks - With Python Examples

In our earlier examples, we only had one input. But in reality, one input wouldn't suffice as problems become more complex.

We can have a more complicated example with more than one input node and more than one output node. This example is about an Iris flower.

The two inputs are:

  1. Petal Width
  2. Sepal Width

With these inputs, we need to predict the species: Setosa, Versicolor, or Virginica. For now, we will keep it simple by using one output node: Setosa.

Visualizing in 3D

Since there are two inputs and one output, we need to draw a 3D graph of what’s going on.

  • Petal Width and Sepal Width each get an axis (X and Z).
  • The output, Setosa, gets the vertical axis (Y).

The inputs are scaled between 0 and 1 to keep things simple.

Step 1: Calculating the Hidden Node Coordinate

Let’s start where Petal and Sepal Width are 0 and plug them into the neural network. Starting with the top node, let's determine the coordinate for the hidden layer.

Using a weight of -2.5 for Petal, -0.6 for Sepal, and a bias of 1.6:

We then pass this through the activation function:


import numpy as np

# Initial point calculation
petal_w, sepal_w = 0, 0
weight_p, weight_s, bias = -2.5, -0.6, 1.6

x_coord = (petal_w * weight_p) + (sepal_w * weight_s) + bias
y_coord = max(0, x_coord)

print(f"At (0,0), the activation output is: {y_coord}")

Enter fullscreen mode Exit fullscreen mode
At (0,0), the activation output is: 1.6
Enter fullscreen mode Exit fullscreen mode

Step 2: Generating the "Blue Dots"

Now let's set Petal Width to 0.2 but keep Sepal Width at 0. We get 1.1 for the Y-axis coordinate. Similarly, if we repeat this until Petal Width is 1 while keeping Sepal Width at 0, we get the following blue dots:

import matplotlib.pyplot as plt

petal_range = np.linspace(0, 1, 6) # 0, 0.2, 0.4, 0.6, 0.8, 1.0
sepal_fixed = 0

# Calculate Y for these specific dots
y_dots = np.maximum(0, (petal_range * -2.5) + (sepal_fixed * -0.6) + 1.6)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(petal_range, [sepal_fixed]*6, y_dots, color='blue', label='Dots at Sepal=0')
ax.set_xlabel('Petal Width')
ax.set_zlabel('Sepal Width')
ax.set_ylabel('Output')
plt.show()

Enter fullscreen mode Exit fullscreen mode

Step 3: Creating the Surface

Now let's do the same for Sepal Width = 0.2, and so on. If we do this until Sepal Width is 1, we will get this blue bent surface.

import numpy as np
import matplotlib.pyplot as plt

# Create a grid of dots across the entire bottom plane
p_dots = np.linspace(0, 1, 6)
s_dots = np.linspace(0, 1, 6)
P_grid, S_grid = np.meshgrid(p_dots, s_dots)

# Calculate Y for all dots in the grid
Y_grid_dots = np.maximum(
    0,
    (P_grid * -2.5) + (S_grid * -0.6) + 1.6
)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# COVER the dots with a surface (same grid, same values)
ax.plot_surface(
    P_grid,
    S_grid,
    Y_grid_dots,
    color='blue',
    alpha=0.7
)

ax.set_xlabel('Petal Width')
ax.set_ylabel('Sepal Width')
ax.set_zlabel('Setosa Output')

plt.show()


Enter fullscreen mode Exit fullscreen mode

Hope you understood until here! In the next article, we will apply some weights and also do the same for the bottom nodes and complete the further steps.

You can try the examples out in the Colab notebook.

Looking for an easier way to install tools, libraries, or entire repositories?
Try Installerpedia: a community-driven, structured installation platform that lets you install almost anything with minimal hassle and clear, reliable guidance.

Just run:

ipm install repo-name
Enter fullscreen mode Exit fullscreen mode

… and you’re done! 🚀

Installerpedia Screenshot

🔗 Explore Installerpedia here

Top comments (0)