Tools: Understanding Multiple Inputs in Neural Networks - With Python Examples

Tools: Understanding Multiple Inputs in Neural Networks - With Python Examples

Source: Dev.to

Visualizing in 3D ## Step 1: Calculating the Hidden Node Coordinate ## Step 2: Generating the "Blue Dots" ## Step 3: Creating the Surface 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. 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. Since there are two inputs and one output, we need to draw a 3D graph of what’s going on. The inputs are scaled between 0 and 1 to keep things simple. 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: 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: 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. 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. … and you’re done! 🚀 🔗 Explore Installerpedia here Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse COMMAND_BLOCK: 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 COMMAND_BLOCK: 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}") COMMAND_BLOCK: 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}") CODE_BLOCK: At (0,0), the activation output is: 1.6 Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: At (0,0), the activation output is: 1.6 CODE_BLOCK: At (0,0), the activation output is: 1.6 COMMAND_BLOCK: 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 COMMAND_BLOCK: 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() COMMAND_BLOCK: 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() COMMAND_BLOCK: 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 COMMAND_BLOCK: 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() COMMAND_BLOCK: 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() CODE_BLOCK: ipm install repo-name Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: ipm install repo-name CODE_BLOCK: ipm install repo-name - Petal Width - Sepal Width - Petal Width and Sepal Width each get an axis (X and Z). - The output, Setosa, gets the vertical axis (Y).