Numpy function type error: only size-1 arrays can be converted to Python scalars
Numpy function type error: only size-1 arrays can be converted to Python scalars
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
对于 softmax 函数,它抛出上述错误。
按照之前对类似问题的回答,我尝试将其矢量化并将其转换为 float.But,但它看起来不起作用。
当我对它进行矢量化时,我得到了这个错误:
TypeError: loop of ufunc does not support argument 0 of type vectorize which has no callable exp method.
对于作为二维数值数组的 inputs
,您不需要所有矢量化或浮点转换。
考虑一个小的二维数组(整数数据类型,但没关系):
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
是一个 python 函数,它执行 1d 求和 - 注意结果是 (3,) 形状数组。尝试对其进行标量 float
转换会产生错误:
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
对所有值求和,返回一个值。那是浮点数,但这并不重要。
In [114]: np.sum(np.exp(arr))
Out[114]: 234.2041839862982
可用于缩放单个值:
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]])
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
对于 softmax 函数,它抛出上述错误。 按照之前对类似问题的回答,我尝试将其矢量化并将其转换为 float.But,但它看起来不起作用。
当我对它进行矢量化时,我得到了这个错误:
TypeError: loop of ufunc does not support argument 0 of type vectorize which has no callable exp method.
对于作为二维数值数组的 inputs
,您不需要所有矢量化或浮点转换。
考虑一个小的二维数组(整数数据类型,但没关系):
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
是一个 python 函数,它执行 1d 求和 - 注意结果是 (3,) 形状数组。尝试对其进行标量 float
转换会产生错误:
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
对所有值求和,返回一个值。那是浮点数,但这并不重要。
In [114]: np.sum(np.exp(arr))
Out[114]: 234.2041839862982
可用于缩放单个值:
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]])