计算机作业代做|STAT 4609 HW 5 Matrix Factorization Assignment 代写

矩阵分解是一种将矩阵还原成其组成部分的方法。它是一种可以简化更复杂的矩阵运算的方法,可以在分解后的矩阵上而不是在原矩阵本身上进行。

矩阵分解的一个常见类比是数字的因式分解,如10的因式分解为2×5的因式分解,因此,矩阵分解也被称为矩阵因式分解。与实数因式分解一样,有许多方法可以分解矩阵,因此有一系列不同的矩阵分解技术。

梯度下降

梯度下降(GD)是一种迭代的一阶优化算法,用于寻找一个给定函数的局部最小/最大值。这种方法通常用于机器学习(ML)和深度学习(DL),以最小化成本/损失函数(例如,在线性回归中)。由于其重要性和易于实现,这种算法通常在几乎所有机器学习课程的开始阶段就被教授。

它的使用不仅限于ML/DL,它也被广泛用于以下领域:

  • 控制工程(机器人、化学等)。
  • 计算机游戏
  • 机械工程

这种方法是在现代计算机时代之前提出的,并且有一个密集的发展时间,导致了许多改进的版本,但在这篇文章中,我们将使用一个在Python中实现的基本/香草梯度下降。

随机梯度下降(SGD)。

“随机”一词是指与随机概率有关的系统或过程。因此,在随机梯度下降法中,每次迭代都会随机选择几个样本,而不是整个数据集。在梯度下降法中,有一个术语叫做 “批次”,它表示每次迭代中用于计算梯度的数据集的总样本数。在典型的梯度下降优化中,如批量梯度下降,批次被认为是整个数据集。虽然,使用整个数据集对于以较少噪音和较少随机性的方式达到最小值确实很有用,但当我们的数据集变得很大时,问题就出现了。

假设你的数据集有一百万个样本,那么如果你使用典型的梯度下降优化技术,你将不得不使用所有的一百万个样本来完成一次迭代,同时进行梯度下降,而且每次迭代都必须这样做,直到达到最小值。因此,它的计算成本变得非常高。 这个问题是由随机梯度下降法解决的。在SGD中,它只使用一个样本,即批量大小为1,来进行每次迭代。该样本是随机洗牌并选择执行迭代的。

在这项CS代写案例中,将比较梯度下降、矩阵分解的随机梯度下降和交替最小二乘法对一个矩阵分解问题的影响。

Part 1: Short Essay Around 500 words. 1. Please summarize and compare the following optimization methods, Gradient Descent, Stochastic Gradient Descent, Alternating Least Square. 2. How would you deal with missing data during optimization.

Part 2 Coding

The skeleton of class MatrixFactor is given below. Please note that in this assignment, we use the modified objective function, which works with incomplete matrics, is computed only over the observed entries as following :

matlab代写

Subject to: No constraints on U and V

Details of input argument and return values.

Parameters:

A: ndarray

– m by n matrix to factorize

opt_method: str

-‘SGD’, ‘GD’, ‘ALS’,

learn_rt: float

the learning rate for SGD or GD optimization method.

k: int

integer specifying the column length of W / the row length of H

the resulting matrices W, H will have sizes of m by k and k by n, respectively

num_iter: int

number of iterations for the multiplicative updates algorithm

print_enabled: boolean

if ture, output print statements

Returns:

U: ndarray user-feature matrix

m by k matrix where k = dim VT: ndarray feature-item matrix

k by n matrix where k = dim



import numpy as np
import pandas as pd
class MatrixFactor:
def init(self, k, opt_method = 'SGD', learn_rt = 0.005, num_iter=1000
self.opt_method = opt_method
self.learn_rt = learn_rt
self.num_iter = num_iter
self.k = k
self.print_enabled = print_enabled
def _Squared_Frobenius_Loss(self):
"""
return
n*m np.ndarray: the difference between A and estimated value at eac
total loss, the sum of errors for all entries of A.
"""
A_est = np.matmul(self.U, self.VT)
diff_error = np.subtract(self.A, A_est)
diff_error[self.nan_cell_mask] = 0
sq_error = np.square(diff_error)
sq_error_sum = 0.5 * sq_error.sum() # the ignore missing data
return diff_error, sq_error_sum
def _SGD(self):
"""_SGD, should
update self.U, self.VT and
return a 1D np.ndarray which stores the loss after each iteration,
the length of the list should be the number of iteration,
because we include the loss before optimization, i.e. the loss when
num_iter = 0
"""
return Loss_list
pass
def _GD(self):
"""
Functionality is same as the _SGD method.
"""
pass
def _ALS(self):
"""
Functionality is same as the _SGD method.
"""
pass
def fit(self, A, init_U = None, init_VT = None):
## initialize U, V
self.A = A.copy()
self.nan_cell_mask = np.where(np.isnan(self.A))
self.A[self.nan_cell_mask] = -1 # empty entries are filled with arbi
if init_U is None:
self.U = np.random.rand(np.size(self.A, 0), self.k)
else:
self.U = init_U
if init_VT is None:
self.VT = np.random.rand(self.k, np.size(self.A, 1))
else:
self.VT = init_VT
"""for optimization"""
if self.opt_method == "SGD":
loss_list = self._SGD()
elif self.opt_method == "GD":
loss_list = self._GD()
elif self.opt_method == 'ALS':
loss_lsit = self._ALS()
else:
raise NotImplementedError("Cannot use provided optimization method
return self.U, self.VT, loss_list

The following code sample is for Gradient Descent.



def _GD(self):
loss_list = np.array([])
for n in range(self.num_iter):
# calcualte the squared Frobenius norm
diff, tol_error = self._Squared_Frobenius_Loss()
# calculate the gradient for each entry of U
grad_U = -np.matmul(diff, np.transpose(self.VT))
# calculate the gradient for each entry of VT
grad_VT = -np.matmul(np.transpose(self.U), diff)
# update U and VT
self.U = np.subtract(self.U, self.learn_rtgrad_U) self.VT = np.subtract(self.VT, self.learn_rtgrad_VT)
# print total loss if needed
if self.print_enabled:
print("iteration " + str(n) + ": " + str(tol_error))
loss_list = np.append(loss_list, tol_error)
return loss_list
MatrixFactor._GD = _GD

contact

Assignment Exmaple

Recent Case

Service Scope

C|C++|Java|Python|Matlab|Android|Jsp|Prolo
g|MIPS|Haskell|R|Linux|C#|PHP|SQL|.Net|Hand
oop|Processing|JS|Ruby|Scala|Rust|Data Mining|数据库|Oracle|Mysql|Sqlite|IOS|Data Mining|网络编程|多线程编程|Linux编程操作系统|计算机网络|留学生|编程|程序|代写|加急|个人代写|作业代写|Assignment

Wechat:maxxuezhang

wechat