Sale!

BME646 and ECE60146: Homework 1

$30.00 $18.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (3 votes)

1 Introduction The goal of this homework is to improve your understanding of the Python Object-Oriented (OO) code in general, especially with regard to how it is used in PyTorch. This is the only homework you will get on general Python OO programming. Future homework assignments will be specific to using PyTorch classes directly or your own extensions of those classes for creating your DL solutions. Note that you should use Python 3.x and not Python 2.x for this and all future programming assignments. For the Python-related knowledge required in this homework, refer to Prof. Kak’s tutorial on OO Python [1]. 2 Programming Tasks 1. Create a class named Sequence with an instance variable named array as shown below: 1 class Sequence ( object ): 2 def __init__ ( self , array ): 3 self . array = array The input parameter array is expected to be a list of numbers, e.g. [ 0, 1, 2]. This class will serve as the base class for the subclasses later in this assignment. 2. Now, extend your Sequence class into a subclass called Fibonacci, with its __init__ method taking in two input parameters: first_value and second_value. These two values will serve as the first two numbers in your Fibonacci sequence. 1 3. Further expand your Fibonacci class to make its instances callable. More specifically, after calling an instance of the Fibonacci class with an input parameter length, the instance variable array should store a Fibonacci sequence of that length and with the two aforementioned starting numbers. In addition, calling the instance should cause the computed Fibonacci sequence to be printed. Shown below is a demonstration of the expected behaviour described so far: 1 FS = Fibonacci ( first_value =1 , second_value =2 ) 2 FS ( length =5 ) # [1, 2, 3, 5, 8] 4. Modify your class definitions so that your Sequence instance can be used as an iterator. For example, when iterating through an instance of Fibonacci, the Fibonacci numbers should be returned one-by-one. The snippet below illustrates the expected behavior: 1 FS = Fibonacci ( first_value =1 , second_value =2 ) 2 FS ( length =5 ) # [1, 2, 3, 5, 8] 3 print (len( FS ) ) # 5 4 print ([n for n in FS]) # [1, 2, 3, 5, 8] 5. Make another subclass of the Sequence class named Prime. As the name suggests, the new class is identical to Fibonacci except that the array now stores consecutive prime numbers. Modify the class definition so that its instance is callable and can be used as an iterator. What is shown below illustrates the expected behavior: 1 PS = Prime () 2 PS ( length =8 ) # [2, 3, 5, 7, 11 , 13 , 17 , 19] 3 print (len( PS ) ) # 8 4 print ([n for n in PS]) # [2, 3, 5, 7, 11 , 13 , 17 , 19] 6. Finally, modify the base class Sequence such that two sequence instances of the same length can be compared by the operator > . Invoking (A > B) should compare element-wise the two arrays and return the number of elements in A that are greater than the corresponding elements in B. If the two arrays are not of the same size, your code should throw a ValueError exception. Shown below is an example: 1 FS = Fibonacci ( first_value =1 , second_value =2 ) 2 FS ( length =8 ) # [1, 2, 3, 5, 8, 13 , 21 , 34] 3 PS = Prime () 4 PS ( length =8 ) # [2, 3, 5, 7, 11 , 13 , 17 , 19] 5 print ( FS > PS ) # 2 2 6 7 PS ( length =5 ) # [2, 3, 5, 7, 11] 8 print ( FS > PS ) # will raise an error 9 # Traceback ( most recent call last ): 10 # … 11 # ValueError : Two arrays are not equal in length ! 3 Submission Instructions Include a typed report explaining how did you solve the given programming tasks. 1. Turn in a zipped file, it should include (a) a typed self-contained pdf report with source code and results and (b) source code files (only .py files are accepted). Rename your .zip file as hw1 .zip and follow the same file naming convention for your pdf report too. 2. For this homework, you are encouraged to use .ipynb for development and the report. If you use .ipynb, please convert it to .py and submit that as source code. 3. You can resubmit a homework assignment as many times as you want up to the deadline. Each submission will overwrite any previous submission. If you are submitting late, do it only once on BrightSpace. Otherwise, we cannot guarantee that your latest submission will be pulled for grading and will not accept related regrade requests. 4. The sample solutions from previous years are for reference only. Your code and final report must be your own work. 5. Your pdf must include a description of • Reproductions of the outputs for each of the provided snippets above with the given parameters. • Correct outputs for each of the provided snippets above with input parameters of your choice. • Your source code. Make sure that your source code files are adequately commented and cleaned up. 3 References [1] Python OO for DL. URL https://engineering.purdue.edu/ DeepLearn/pdf-kak/PythonOO.pdf. 4