def fit(self, x: np.ndarray, y: np.ndarray) -> None: M, N = x.shape self.v = np.random.normal(0, 0.1, [self.k, N]) self.w = np.random.normal(0, 0.1, N) for _iter in range(self.max_iter): dv = np.zeros([self.k, N]) dw = np.zeros(N) db = 0 y_hat = self.predict(x) if self.verbose: print("################################") print("iter{}, 训练误差:".format(_iter), compute_mse(y, y_hat)) for m in range(M): diff = y[m] - y_hat[m] dw += -0.5 * diff * x[m] db += -0.5 * diff dv += -0.5 * diff * (np.matmul( np.matmul(self.v, x[m]).reshape(-1, 1), x[m].reshape( 1, -1)) - x[m]**2 * self.v) self.w -= self.learning_rate * dw / m self.b -= self.learning_rate * db / m self.v -= self.learning_rate * dv / m
from algs.nn_algoritms.common.model import Model from algs.nn_algoritms.common.layer import Dense import numpy as np from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from algs.utils import compute_mse x, y = load_boston(return_X_y=1) x = (x - np.min(x, axis=0)) / (np.max(x, axis=0) - np.min(x, axis=0)) y = y.reshape(-1, 1) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=50) model = Model(lr=0.01, epoch=1500, loss="Mse") model.add(Dense(activation='leakyrelu', units=64)) model.add(Dense(activation='tanh', units=32)) model.add(Dense(activation='linear', units=1)) model.fit(x_train, y_train) y_predict = model.predict(x_test).reshape(-1) print(compute_mse(y_predict, y_test)) import matplotlib.pyplot as plt plt.plot(model.history['loss'])
from sklearn.model_selection import train_test_split from algs.ml_algorithms.fm import FM from algs.utils import compute_mse x, y = load_boston(return_X_y=1) # 分桶为稀疏矩阵 ###################################################################### x_new = [] for col in range(x.shape[1]): tmp = np.zeros([len(x), 16]) _max = max(x[:, col]) _min = min(x[:, col]) for row in range(x.shape[0]): _ = int(round((x[row][col] - _min) / ((_max - _min) / 15), 0)) tmp[row, _] = 1 x_new.append(tmp) x = np.concatenate(x_new, axis=1) ###################################################################### x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=50) fm = FM(k=15, learning_rate=0.03) fm.fit(x_train, y_train) y_predict = fm.predict(x_test.astype(np.float32)) print("验证集误差:", compute_mse(y_predict, y_test))