抱歉,如果这是一个琐碎的问题,或者只是个愚蠢的问题,我才刚开始使用python和神经网络...所以我从一个教程中制作了这个简单的神经网络,并且一切正常,我的问题是我要怎么去关于更改输入和输出目标的信息,因为目前我还不知道为什么输入和输出位于数组中?我正在尝试做一些事情,例如让它学习到当减小的值到达某个点(例如0.25)时返回1,否则返回0?一个例子是输入是到障碍物的X距离,当距离足够近时它可以学习跳跃(输出1 =跳跃,输出0 =不执行任何操作)?(总而言之,我的问题是我试图找到一种方法来输入浮点数并输出诸如浮点数或整数之类的东西,但这似乎只能接受并输出np。
import numpy as np
def sigmoid(x):
return 1.0/(1+ np.exp(-x))
def sigmoid_derivative(x):
return x * (1.0 - x)
class NeuralNetwork:
def __init__(self, x, y):
self.input = x
self.weights1 = np.random.rand(self.input.shape[1],4)
self.weights2 = np.random.rand(4,1)
self.y = y
self.output = np.zeros(y.shape)
def feedforward(self):
self.layer1 = sigmoid(np.dot(self.input, self.weights1))
self.output = sigmoid(np.dot(self.layer1, self.weights2))
def backprop(self):
d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_derivative(self.output)))
d_weights1 = np.dot(self.input.T, (np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights2.T) * sigmoid_derivative(self.layer1)))
self.weights1 += d_weights1
self.weights2 += d_weights2
if __name__ == "__main__":
X = np.array([[0,0,1],
[0,1,1],
[1,0,1],
[1,1,1]])
y = np.array([[1],[0],[1],[0]])
nn = NeuralNetwork(X,y)
for i in range(10000):
nn.feedforward()
nn.backprop()
print(nn.output)
您需要知道什么batch size
,通常来说,我们通常将多个样本输入到NN中,batch size
即样本数,请参见,输入在列表2中dim [batch_size, fea_size]
,因此我们使用ndarray来包含输入。
如果要输入单个(平均数batch size
为1)float(平均数fea_size
为1),则需要使用ndarray以形状(1,1)封装输入,例如np.array([[0.5]])
。
接下来,NN的输出也为shape [batch_size, output_size]
,它给出所有输入样本的结果,并且output_size
id由上一层的权重确定(在您的代码中self.weights2 = np.random.rand(4,1)
),这意味着aoutput_size
为1。因此,如果要浮点输出,可以得到它来自np.output[:, 0]
所有样本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句