<jupyter_start><jupyter_text>Artificial Neuron Layer In this notebook, we use PyTorch tensors to create a layer of artificial neurons that could be used within a deep learning model architecture.<jupyter_code>import torch
import matplotlib.pyplot as plt
_ = torch.manual_seed(42)<jupyter_output><empty_output><jupyter_text>Set number of neurons:<jupyter_code>n_input = 784 # Flattened 28x28-pixel image
n_dense = 128<jupyter_output><empty_output><jupyter_text>Simulate an "input image" with a vector tensor `x`:<jupyter_code>x = torch.rand(n_input) # Samples float values from [0,1) uniform distribution (interval doesn't include 1)
x.shape
x[0:6]
_ = plt.hist(x)<jupyter_output><empty_output><jupyter_text>Create tensors to store neuron parameters (i.e., weight matrix `W`, bias vector `b`) and initialize them with starting values:<jupyter_code>b = torch.zeros(n_dense)
b.shape
b[0:6]
W = torch.empty([n_input, n_dense])
W = torch.nn.init.xavier_normal_(W)
W.shape
W[0:4, 0:4]<jupyter_output><empty_output><jupyter_text>Pass the "input image" `x` through a *dense* neuron layer with a *sigmoid activation function* to output the vector tensor `a`, which contains one element per neuron:<jupyter_code>z = torch.add(torch.matmul(x, W), b)
a = torch.sigmoid(z)
a.shape
_ = plt.hist(a)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup