【问题标题】:Numpy function type error: only size-1 arrays can be converted to Python scalarsNumpy 函数类型错误:只能将 size-1 数组转换为 Python 标量
【发布时间】:2021-08-13 16:20:17
【问题描述】:
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 函数,它会抛出上述错误。 按照先前对有些类似问题的回答,我尝试将其矢量化并将其转换为浮点数。但这似乎不起作用。

当我对它进行矢量化时,我得到了这个错误:

TypeError: loop of ufunc does not support argument 0 of type vectorize which has no callable exp method.

【问题讨论】:

  • 输入的维度是多少?您能否添加一个示例和预期的输出?如果我理解正确,您只询问函数'softmax'。发布所有代码会使您感到困惑并且更难以为您提供帮助。我建议,下次,您将问题限制在代码的相关部分。如果您告诉我们代码中的哪一行引发了您所询问的错误,这也会有所帮助
  • 显示回溯!我怀疑使用float()。为什么在那儿?为什么是np.vectorize?您没有阅读该函数的文档。
  • 在链接中,vectorize 采用仅适用于标量的function,并返回适用于数组的function。你不这样做!您还没有显示inputs 是什么,但astype 方法暗示它是一个数组。 np.vectorize 这样没有意义。停止尝试使用np.vectorize;这对你没有帮助。
  • @hpaulj 我会记下这条评论。非常感谢你的这个提示,这是非常明智的。我通常在 stackoverflow 或其他类似论坛中寻找答案。我将尝试将其应用于当前问题。

标签: python numpy matrix deep-learning softmax


【解决方案1】:

对于作为二维数值数组的inputs,您不需要所有矢量化或浮点转换。

考虑一个小的二维数组(整数 dtype,但没关系):

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 函数,它执行一维求和 - 注意结果是 (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]])

【讨论】:

  • 非常感谢。我学到了两件重要的事情。谢谢:)
猜你喜欢
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
  • 2020-12-29
  • 1970-01-01
  • 2019-11-11
相关资源
最近更新 更多