Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Visual_NN.py 3.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  1. import vpython as vp
  2. import numpy as np
  3. neuron_color = vp.vector(0.00, 0.49, 0.73)
  4. weight_color = vp.vector(0.80, 0.80, 0.80)
  5. text_color = vp.vector(1.0, 1.0, 1.0)
  6. class Network:
  7. def __init__(self, architecture, act_funcs, title,
  8. x_compress=2.0, y_compress=1.0):
  9. neuron_storage = []
  10. weight_storage = []
  11. net_amp = (len(architecture) - 1) / 2
  12. max_amp = (max(architecture) - 1) / 2
  13. x_text = net_amp * x_compress
  14. y_title = max_amp * y_compress + 0.5
  15. if act_funcs:
  16. y_text = max_amp * y_compress + 1.0
  17. y_funcs_text = y_text - 0.5
  18. else:
  19. y_text = max_amp * y_compress + 0.5
  20. vp.text(text=title, pos=vp.vector(0, y_title, 0),
  21. align="center", color=text_color, height=0.2)
  22. # Descriptive Text Below the Network
  23. vp.text(text="inputs", pos=vp.vector(-x_text, -y_text, 0),
  24. align="right", color=text_color, height=0.2)
  25. vp.text(text="|", pos=vp.vector(-x_text + 0.5, -y_text, 0),
  26. align="center", color=text_color, height=0.2)
  27. vp.text(text="hidden\nlayers", pos=vp.vector(0, -y_text, 0),
  28. align="center", color=text_color, height=0.2)
  29. vp.text(text="|", pos=vp.vector(x_text - 0.5, -y_text, 0),
  30. align="center", color=text_color, height=0.2)
  31. vp.text(text="outputs", pos=vp.vector(x_text, -y_text, 0),
  32. align="left", color=text_color, height=0.2)
  33. # Initialize the Neuron Storage
  34. for n in architecture:
  35. layer = [0 for i in range(n)]
  36. neuron_storage.append(layer)
  37. # Create and Store the Neuron Spheres
  38. for layer_num, num_neurons in enumerate(architecture):
  39. layer_amp = (num_neurons - 1) / 2
  40. x = (layer_num - net_amp) * x_compress
  41. if act_funcs:
  42. vp.text(text=act_funcs[layer_num],
  43. pos=vp.vector(x, -y_funcs_text, 0),
  44. align="center", color=text_color, height=0.2)
  45. for neuron_num in range(num_neurons):
  46. y = (neuron_num - layer_amp) * y_compress
  47. neuron_pos = vp.vector(x, y, 0)
  48. neuron_storage[layer_num][neuron_num] = vp.sphere(
  49. pos=neuron_pos, radius=0.1, color=neuron_color)
  50. # Create and Store the Weight Bonds
  51. for layer_num, current_n in enumerate(architecture[:-1]):
  52. next_n = architecture[layer_num+1]
  53. weight_storage.append([])
  54. for i in range(current_n):
  55. weight_storage[-1].append([])
  56. x_start = (layer_num - net_amp) * x_compress
  57. x_end = x_start + 1 * x_compress
  58. for j in range(next_n):
  59. y_start = (i - (current_n - 1) / 2) * y_compress
  60. y_end = (j - (next_n - 1) / 2) * y_compress
  61. start = vp.vector(x_start, y_start, 0)
  62. end = vp.vector(x_end, y_end, 0)
  63. weight_storage[-1][-1].append(vp.cylinder(
  64. pos=start, axis=end-start, radius=0.01,
  65. color=weight_color))
  66. # Make Neuron and Weight Bond Storage Class Attributes
  67. self.neuron_storage = neuron_storage
  68. self.weight_storage = weight_storage
  69. arch = [1, 13, 1]
  70. funcs = ['linear', 'tanh', 'linear']
  71. network = Network(
  72. arch, funcs, "Neural Network", x_compress=2.0, y_compress=0.33)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...