omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Numpy issues

    Pythonista
    4
    6
    3368
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Drizzel
      Drizzel last edited by

      I currently am trying to get my head around some basic artificial “intelligence“, so I copied this code from the net:

      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(self.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):
              # application of the chain rule to find derivative of the loss function with respect to weights2 and weights1
              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)))
      
              # update the weights with the derivative (slope) of the loss function
              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([[0],[1],[1],[0]])
          nn = NeuralNetwork(X,y)
      
          for i in range(1500):
              nn.feedforward()
              nn.backprop()
      
          print(nn.output)
      

      When importing numpy I got some error, but I fixed it by installing numpy via stash. But if sou try to execute this code in pythonista, you will get heaps of errors. For example, numpy.array() apparently doesn’t exist. Does anyone know why this is the case?

      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        Please don't try to install numpy via StaSh, it's simply not possible, and it may break the version that is included with Pythonista. I just tried your script in a vanilla Pythonista, and it seemed to work fine.

        Drizzel 1 Reply Last reply Reply Quote 0
        • Drizzel
          Drizzel @omz last edited by

          @omz ok, no problem. I just reinstalled Pythonista on my iPad. Obviously it functions now, thanks a lot. I’m sure you hear this frequently, but Pythonista is a great app. I’m just 16yrs old, so it helped a lot at learning coding in general.

          Drizzel 1 Reply Last reply Reply Quote 0
          • Drizzel
            Drizzel @Drizzel last edited by

            @Drizzel said:

            @omz ok, no problem. I just reinstalled Pythonista on my iPad. Obviously it functions now, thanks a lot. I’m sure you hear this frequently, but Pythonista is a great app. I’m just 16yrs old, so it helped a lot at learning coding in general.

            Hopefully I didn’t agree that I’m over 18 when signing up this forum :D

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @Drizzel last edited by

              @Drizzel I've a lot of years too much, thus feel free to take some of mine 😂

              1 Reply Last reply Reply Quote 1
              • JonB
                JonB last edited by

                One thing that can happen sometimes it's if you hit X right after running a script. Since numpy or matplotlib are very large, importing them takes some time. If you cancel before the import is complete, you get a partial import, which breaks later imports.

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors