Implement the following optimization classes in the /app/ directory for the smartnet library, ensuring they are compatible with the LinearLayer and MSELayer classes. Each optimization class should handle the parameters of the given layer, support the necessary methods, and perform gradient updates according to the respective optimization algorithm.
SGDOptim:
Implement a stochastic gradient descent optimizer that includes methods:
__init__(self, parameters): Initializes with parameter list.
zero_grad(self): Sets the gradients of the parameters to zero.
step(self): Updates the parameters based on their gradients.
MomentumOptim:
Implement a momentum optimizer that builds on SGD and includes a momentum term. It should have:
__init__(self, parameters, lr=0.001): Initializes with parameter list and learning rate.
zero_grad(self): As in SGD.
step(self): Updates parameters using weighted average of past gradients.
RMSPropOptim:
Implement an RMSProp optimizer that adapts the learning rate for each parameter. It should have:
__init__(self, parameters, lr=0.001, alpha=0.99): Initializes with parameter list, learning rate, and decay factor.
zero_grad(self): As in SGD.
step(self): Updates parameters based on the moving average of squared gradients.
AdamOptim:
Implement an Adam optimizer that combines momentum and RMSProp. It should include:
__init__(self, parameters, lr=0.001, beta1=0.9, beta2=0.999): Initializes with parameter list, learning rate, and beta values.
zero_grad(self): As in SGD.
step(self): Updates parameters based on gradients, moving averages, and bias correction.
Each optimization class must ensure correct handling of parameter updates over multiple iterations as demonstrated in the provided test cases.
Ensure that all classes are thoroughly tested, and that the implementation respects the existing interface of LinearLayer and MSELayer. Once implemented, the test cases provided should pass without modification.