Reputation: 47
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.
Upvotes: 1
Views: 190
Reputation: 231395
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]])
Upvotes: 2