4

How can i find the dimensions(.shape) of x outside the function without "global"

import numpy as np

def f():
    x=np.array(([1.2],[3,4],[5,6]),dtype=float)

i would also like to know the same thing in this code.For example the dimensions of self.o_error.And generally anything inside a class

import numpy as np

# X = (hours studying, hours sleeping), y = score on test, xPredicted = 4 hours studying & 8 hours sleeping (input data for prediction)
X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)
y = np.array(([92], [86], [89]), dtype=float)
xPredicted = np.array(([4,8]), dtype=float)

# scale units
X = X/np.amax(X, axis=0) # maximum of X array
xPredicted = xPredicted/np.amax(xPredicted, axis=0) # maximum of xPredicted (our input data for the prediction)
y = y/100 # max test score is 100

class Neural_Network(object):
  def __init__(self):
  #parameters
    self.inputSize = 2
    self.outputSize = 1
    self.hiddenSize = 3

  #weights
    self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # (3x2) weight matrix from input to hidden layer
    self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # (3x1) weight matrix from hidden to output layer

  def forward(self, X):
    #forward propagation through our network
    self.z = np.dot(X, self.W1) # dot product of X (input) and first set of 3x2 weights
    self.z2 = self.sigmoid(self.z) # activation function
    self.z3 = np.dot(self.z2, self.W2) # dot product of hidden layer (z2) and second set of 3x1 weights
    o = self.sigmoid(self.z3) # final activation function
    return o

  def sigmoid(self, s):
    # activation function
    return 1/(1+np.exp(-s))

  def sigmoidPrime(self, s):
    #derivative of sigmoid
    return s * (1 - s)

  def backward(self, X, y, o):
    # backward propagate through the network
    self.o_error = y - o # error in output
    self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to error

    self.z2_error = self.o_delta.dot(self.W2.T) # z2 error: how much our hidden layer weights contributed to output error
    self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error

    self.W1 += X.T.dot(self.z2_delta) # adjusting first set (input --> hidden) weights
    self.W2 += self.z2.T.dot(self.o_delta) # adjusting second set (hidden --> output) weights
  • 2
    How about beginning with `return x`? [related](https://stackoverflow.com/questions/1360721/how-to-get-set-local-variables-of-a-function-from-outside-in-python) haven't gone as far as calling it a dupe. – Paul Rooney Aug 16 '18 at 06:44
  • But your class `Neural_Network` never exposes `self.o_error`. If you want to expose dimensions like that, you should [add properties](https://stackoverflow.com/questions/6618002/using-property-versus-getters-and-setters/6618176#6618176) or accessors to the class. – smci Aug 16 '18 at 06:55

2 Answers2

2

You can initialize your variables in __init__ function, then you will be able to access them outside the function and even outside the class

class Bar():
    def __init__(self):
        self.var = 4
    def addone(self):
        self.var +=1
    def showvar(self):
        print(self.var)

foo = Bar()
foo.showvar()   # 4

# you can access variable directly
print(foo.var)  # 4

# change variable using other functions
foo.addone()
foo.showvar()   # 5
Ali Kazemkhanloo
  • 1,017
  • 1
  • 10
  • 16
  • This is very useful in your example and it works.But in my Neural Network must i initialize self.o_error.But how is this posible ?? – THEODOROPOULOS DIMITRIS Aug 16 '18 at 07:50
  • @THEODOROPOULOSDIMITRIS This is [a 20 minutes reading](https://docs.python.org/3/tutorial/classes.html#classes). Moreover, it has [a section](https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces) addressing in a very specific way the topic that, seemingly, you cannot entirely grasp. Moreover, it was written by [GvR](https://en.wikipedia.org/wiki/Guido_van_Rossum)... – gboffi Aug 16 '18 at 08:14
  • @THEODOROPOULOSDIMITRIS I have little experience in neural network, But I think you can initialize it with `None` – Ali Kazemkhanloo Aug 16 '18 at 09:52
1

The easiest solutions are:

def f():
    x=np.array(([1.2],[3,4],[5,6]))
    return x
x = f()
x_shape = x.shape

or

def f():
    x=np.array(([1.2],[3,4],[5,6]))
    x_shape = x.shape
    return x, x_shape

x, x_shape = f()

or, if you do not want to modify the function, AND get both on the same line you can do:

def f():
    x=np.array(([1.2],[3,4],[5,6]))
    return x

x, x_shape = f(), f().shape
VegardKT
  • 1,226
  • 10
  • 21