使用Python和NumPy实现回归神经网络

我想用PythonNumpy创建一个回归神经网络。我已经为分类问题创建了一个网络,其中使用了sigmoid函数,但我不清楚如何将我的函数改为用于回归。我知道线性函数的形式应该是y=k*x + n,但我不知道如何在Python中表示这个公式。我的nk值是什么,又该如何将它们传递给我的函数?

当然,我这里提到的只是一个简单的线性函数,我的目标是创建类似RBF函数的东西。

这是我目前的代码,但其中使用的是sigmoid函数,我想将其改为回归函数。

我该如何用回归函数及其导数函数替换sigmoid函数?

import numpy as npimport pandas as pddf = pd.DataFrame({'input 1':[0.5, 0.3, 0, 0.1, 0.4, -0.4, 0.4, 0.1, -0.6, 0.2, 0.6, 0, 0.2, 0.2, -0.1, -0.1, 0, 0.4, -0.2, -0.4],                   'input 2':[0.3, 0.6, -0.4, -0.2, 0.9, 0, 0.35, -0.4, -0.9, 0.4, 0.3, -0.1, 0.1, 0.3, 0.1, 0.1, 0.3, 0.1, 0.3, 0.3],                   'input 3':[0, 0.4, 0, -0.1, 0.4, -0.2, 0.7, -0.3, -0.1, 0.1, 0.3, 0, 0.5, 0.4, -0.31, 0.1, 0.3, 0.1, 0.1, 0.2],                   'result':[21, 31, 10, 6, 53, 31, 13, 15,16, 15, 26, 36, 52, 41, 14, 8, 14, 13, 11, 4]})print(df)def sigmoid(x):    return 1 / (1 + np.exp(-x))def sigmoid_derivate(x):    return x * (1 - x)features = df.iloc[:,:-1].to_numpy()results =  df.iloc[:,-1:].to_numpy()np.random.seed(1)weights = 2 * np.random.random((3,1)) - 1print('These are my random weights:\n')print(weights)for iteration in range(100000):    input_layer = features    outputs = sigmoid(np.dot(input_layer, weights))    error = results - outputs    adjustments = error * sigmoid_derivate(outputs)    weights += np.dot(input_layer.T, adjustments)df['output prediction'] = outputs.round(0)print(df)

回答:

你已经实现了一个使用sigmoid函数的分类系统,该系统会产生0到1之间的值。

对于线性回归类型的问题,你可以直接创建一个没有激活函数的输出层,因为我们需要的是未经转换的数值结果。

所以,实际上要预测值,你只需要一个线性回归模型即可。


一个使用梯度下降优化的小型线性回归模型示例。

X = df.iloc[:,:-1].to_numpy()y =  df.iloc[:,-1:].to_numpy()def compute_cost(X, y, params):    n_samples = len(y)    h = X @ params  # dot product    return (1/(2*n_samples))*np.sum((h-y)**2)def gradient_descent(X, y, params, learning_rate, n_iters):    n_samples = len(y)    J_history = np.zeros((n_iters,1))    for i in range(n_iters):        params = params - (learning_rate/n_samples) * X.T @ (X @ params - y)         J_history[i] = compute_cost(X, y, params)    return (J_history, params)n_samples = len(y)mu = np.mean(X, 0)sigma = np.std(X, 0)X = (X-mu) / sigma # normalizeX = np.hstack((np.ones((n_samples,1)),X))  # add bias termn_features = np.size(X,1)params = np.zeros((n_features,1))  # initial guessn_iters = 1500learning_rate = 0.01initial_cost = compute_cost(X, y, params)print("Initial cost is: ", initial_cost, "\n")(J_history, optimal_params) = gradient_descent(X, y, params, learning_rate, n_iters)print("Optimal parameters are: \n", optimal_params, "\n")print("Final cost is: ", J_history[-1])plt.plot(range(len(J_history)), J_history, 'r')plt.title("Convergence Graph of Cost Function")plt.xlabel("Number of Iterations")plt.ylabel("Cost")plt.show()

请记住,OLS可以用矩阵的闭合形式表达,因此不需要使用梯度下降法。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注