1
class GCN:
  def __init__(self,alpha,adj,feature,hiddenlayer_neurons,output_layer_neurons):
    self.alpha=alpha
    self.adj=adj
    self.feature=feature
    self.hiddenlayer_neurons=hiddenlayer_neurons
    self.output_layer_neurons=output_layer_neurons
  
  def weightlayers(self):
    self.weights1= np.random.normal(loc=0,scale=0.5,size=(features.shape[1],self.hiddenlayer_neurons))
    print(features.shape)
    print(adj.shape)
    self.weights2= np.random.normal(loc=0,scale=0.5,size=(self.hiddenlayer_neurons,self.output_layer_neurons))
    self.bias1= np.random.normal(loc=0, scale=0.05, size=self.hiddenlayer_neurons)
    self.bias2=np.random.normal(loc=0, scale=0.05, size= self.output_layer_neurons)
    return self.weights1,self.weights2,self.bias1,self.bias2

  def sigmoid(self,x):
    sigma=1/(1+np.exp(-x))
    return sigma
  
  def softmax(self,inputs):
    inputs=inputs.astype(np.float)
    inputs=np.vectorize(inputs)
    f=np.exp(inputs) / float(sum(np.exp(inputs)))
    #f2 = np.vectorize(f)
    return f

  def forwardpropagation(self):
    self.weights1,self.weights2,self.bias1,self.bias2=self.weightlayers()

    self.bias1=(np.reshape(self.bias1,(-1,1))).T
    self.bias2=(np.reshape(self.bias2,(-1,1))).T
    print(self.bias1.ndim)
    #self.sigmoid=self.sigmoid()
    self.adj=self.adj.T
    self.input= self.adj.dot(self.feature).dot(self.weights1) + (self.bias1)
    print(self.input.shape)
    self.sigmaactivation= self.sigmoid(self.input)
    self.hiddeninput=(self.sigmaactivation @ self.weights2 ) + (self.bias2)
    self.output=self.softmax(self.hiddeninput)
    return self.output

For the softmax function it is throwing the above mentioned error. Following previous answers for somewhat similar question I tried to vectorize and convert it to float.But that does't seen to work.

When I vectorize it, I get this error :

TypeError: loop of ufunc does not support argument 0 of type vectorize which has no callable exp method.
10
  • 1
    what is the dimension of the input? can you please add an example of it and an expected output? If I understand correctly, you're asking only about the function 'softmax'. posting all of your code makes it confusing and more difficult to help you. I suggest, next time, you limit your question to the relevant part of your code. It would also help if you tell us which line in your code throws the error you're asking about Commented May 25, 2021 at 12:38
  • 1
    Show the traceback! I suspect the use of float(). Why's it there? Why the np.vectorize? You didn't read that function''s docs. Commented May 25, 2021 at 14:28
  • @hpaulj stackoverflow.com/questions/36680402/… Commented May 25, 2021 at 16:13
  • In the link, vectorize takes a function that only works with a scalar, and returns one that works with an array. You aren't doing that!. You haven't shown what inputs is, but the astype method implies that it's an array. np.vectorize doesn't make sense that way. Stop trying to use np.vectorize; it's not helping you. Commented May 25, 2021 at 16:18
  • 1
    @hpaulj I will keep a note of this comment.Thank you so much for this tip, that is very sensible. I am usually looking up for answers in stackoverflow or orther similar forums.I will try to apply this in the current problem. Commented May 25, 2021 at 16:31

1 Answer 1

2

For inputs as 2d numeric array, you don't need all that vectorize or float conversion.

Consider a small 2d array (integer dtype, but doesn't matter):

In [110]: arr = np.arange(6).reshape(2,3)
In [111]: np.exp(arr)
Out[111]: 
array([[  1.        ,   2.71828183,   7.3890561 ],
       [ 20.08553692,  54.59815003, 148.4131591 ]])

sum is a python function, that does 1d summation - note the result is (3,) shape array. Trying to do a scalar float conversion on that produces your error:

In [112]: sum(np.exp(arr))
Out[112]: array([ 21.08553692,  57.31643186, 155.8022152 ])
In [113]: float(sum(np.exp(arr)))
Traceback (most recent call last):
  File "<ipython-input-113-0972ef0e1a76>", line 1, in <module>
    float(sum(np.exp(arr)))
TypeError: only size-1 arrays can be converted to Python scalars

np.sum does the sum on all values, returning one value. That's float, but that isn't important.

In [114]: np.sum(np.exp(arr))
Out[114]: 234.2041839862982

That can be used to scale the individual values:

In [115]: f=np.exp(arr)
     ...: f/np.sum(f)
Out[115]: 
array([[0.00426978, 0.01160646, 0.03154963],
       [0.08576079, 0.23312201, 0.63369132]])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much.I learnt two important things.Thank you:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.