Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We will define a class to model the fully connected multi layer network. The network parameters will be a dictionary accesssed with keys W1, b1,

We will define a class to model the fully connected multi layer network. The network parameters will be a dictionary accesssed with keys "W1", "b1", "W2", "b2", etc.\ ensure very small values close to zero for faster training. The requires_grad will be needed for tracking gradient. We limit the precision to torch.float 32 (optional).\ Bias values are initialized with 0 . For example a bias vector of dimensions

3\\\\times 1

is initialized as , requires_grad=True, dtype=torch.float32)\ class MultiLayerNetwork():\ def_init_(self, net_dims, activation_list=None):\ # Activation functions can be provided through activation_list\ # Default activations are relu for all layers\ # except for the last layer which is Softmax\ self.allowed_activations = ['relu', 'sigmoid', 'tanh']\ self.final_activation = 'softmax'\ self.num_layers

=

len(net_dims)-1\ if activation_list is None:\ self.activation_list self.num_layers-1) 'softmax'

image text in transcribed
e will define a class to model the fully connected multi layer network. The network parameters will be a dictionary accesssed with keys "W1", "b1", "W2", "b2", etc. ensure very small values close to zero for faster training. The requires_grad will be needed for tracking gradient. We limit the precision to torch.float 32 (optional). 2. Bias values are initialized with 0 . For example a bias vector of dimensions 31 is initialized as b=torch.zeros((3,4), requires_grad=True, dtype=torch.float 32 ) class MultiLayerNetwork(): def_init_(self, net_dims, activation_list=None): \#H Activation functions can be provided through activation_list \# Default activations are relu for all layers which is Softmax self.allowed_activations =[ 'relu', 'sigmoid', 'tanh'] self.final_activation = 'softmax' self.num_layers = len(net_dims)-1 if activation_list is None: self.activation_list =[ 'relu'] ( self.num_layers-1) + ['softmax'] elif (len(activation_list)==self.num_layers and all(act in self.allowed_activations for act in activation_list[:-1]) and ( activation_list [1]== 'softmax'): self.activation_list = activation_list else: raise ValueError('activation_list is incorrect!') \#\# Intitialize parameters as a dictionary self. parameters ={} for ly in range(self.num_layers): \# Hint: \# parameters ["W"+tr(1y+1)]=0.01torch. randn( .., ..) \# parameters ["b"+str(1y+1)] = torch.zeros((.., ..)) \# Use requires_grad=True, \#H\#\# BEGIN SOLUTION \#\#\#\# \#\#\#\# END SOLUTION ### \#\# Forward propagation of input data through the network def forward(self, x ): for ly in range(self.num_layers): \#H Apply the dot product followed by activation \#\# Use torch. mm() \#\# Use torch.mm() \#\#\# BEGIN SOLUTION \#\#\# \#\#HI END SOLUTION ### return x WH Function to Display the Network def display(self): print('\%d Layer Fully Connected Neural Network' z self.num_layers) dims = list(self. parameters.values()) for ly in range(self.num_layers): str(1y+1), dims [2*1y+1].shape [0], dims [2*1y+1].shape[1], self.activation_list[ly]), end='') print('[Cross_entropy_loss]')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

How are cost of goods sold and gross profit computed?

Answered: 1 week ago