Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中带NumPy的回归神经网络_Python_Numpy_Machine Learning - Fatal编程技术网

Python中带NumPy的回归神经网络

Python中带NumPy的回归神经网络,python,numpy,machine-learning,Python,Numpy,Machine Learning,我想用Python和Numpy制作回归神经网络。我做了一个分类,我使用了sigmoid函数,但我不知道如何将我的函数改为回归。 我知道线性函数应该看起来像y=k*x+n,但我不知道如何在python中表示它。我的n和k值是什么,如何将它们传递给我的函数 import numpy as np import pandas as pd df = pd.DataFrame({'input 1':[0.5, 0.3, 0, 0.1, 0.4, -0.4, 0.4, 0.1, -0.6, 0.2, 0.

我想用
Python
Numpy
制作回归神经网络。我做了一个分类,我使用了
sigmoid
函数,但我不知道如何将我的函数改为回归。 我知道线性函数应该看起来像
y=k*x+n
,但我不知道如何在python中表示它。我的
n
k
值是什么,如何将它们传递给我的函数

import numpy as np
import pandas as pd


df = 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)) - 1

print('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)
Ofc,我使用简单的线性函数,但我的目标是创建类似于
RBF
函数的函数

这是我的代码,但在他的代码中我有一个sigmoid函数,我想有一个回归函数

如何用回归函数及其导数函数更改sigmoid函数

import numpy as np
import pandas as pd


df = 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)) - 1

print('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)

我认为你应该修改你对神经网络功能的理解。实际上,您应该按顺序初始化网络,然后添加 输入图层,其中应包含您将使用的数据中的要素数量。此外,您应该添加将要使用的隐藏层,并通过添加输出层来完成,在输出层中,您应该提到输出的数量为1,激活函数为“线性”。 我给你举个例子

初始化网络 NN=顺序()

输入层 NN.add(密集(N0,输入尺寸=特征.shape[1],激活=relu'))

隐藏层 NN.add(密集(N1,activation='relu')) NN.add(稠密(N2,活化='relu')) . . .

输出层
NN.add(稠密(1,activation='linear'))

您已经实现了一个分类系统,使用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 # normalize

X = np.hstack((np.ones((n_samples,1)),X))  # add bias term
n_features = np.size(X,1)

params = np.zeros((n_features,1))  # initial guess

n_iters = 1500
learning_rate = 0.01

initial_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可以表示为矩阵闭合形式,因此,不需要梯度下降。

我知道这一点,但我不知道如何做到这一点。我不理解您发布的代码。是否可以对我的代码实施您的解决方案?