Search code examples
pythonarraysneural-networkartificial-intelligence

Cannot figure out inputting floats or integers into my neural-network


Sorry if this is a trivial question, or just plain stupid, I'm just getting started with python and neural-networks... So I made this simple neural network off a tutorial and everything's working fine, my question is how I would go about changing my input and output goal, because at the moment I don't understand why the input and output is in an array? I'm looking to do something like getting it to learn to return a 1 when a decreasing value gets to a certain point, like 0.25, and return 0 otherwise? An example would be the input being the X distance to an obstacle and it could learn to jump when it gets close enough (output of 1 = jump, output of 0 = do nothing)? (To summarize, my problem is I'm trying to find a way to input things like floats and output things like floats or integers but it only seems to take in and output np.arrays) Here's the code for reference (this works fine but I'm not sure how I should change the input and output goal...):

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)

Solution

  • You need to know what batch size is, generally speaking, we usually input multiple samples into the NN, batch size is the num of samples, see, the input has at list 2 dim [batch_size, fea_size], so we use ndarray to contain the input.

    If you want input a single (mean batch size is 1) float(means fea_size is 1), you need to wrap you intput using ndarray in shape (1,1), like np.array([[0.5]]).

    Next, the output of NN's is also in shape [batch_size, output_size], it gives results for all input samples, and the output_size id determined by last layer's weight (in your code self.weights2 = np.random.rand(4,1)), this means the output_size is 1. So if you want a float output, you can get it from np.output[:, 0] for all samples.