【问题标题】:ValueError: could not broadcast input array from shape (15,15) into shape (15)ValueError:无法将输入数组从形状(15,15)广播到形状(15)
【发布时间】:2020-04-09 13:40:37
【问题描述】:
import numpy 
HL1_neurons = 15
input_HL1_weights = numpy.random.uniform(low=-0.1, high=0.1,size=(15, HL1_neurons))
output_neurons = 1
HL2_output_weights = numpy.random.uniform(low=-0.1, high=0.1,size=(HL1_neurons, 1))
weights = numpy.array([input_HL1_weights,HL2_output_weights])

在执行代码时 HL1_neurons 接受 15 以外的任何数字

如果是 15 则显示以下错误,请在这方面帮助我


ValueError                                Traceback (most recent call last)
<ipython-input-15-97fe596c0407> in <module>
      4 output_neurons = 1
      5 HL2_output_weights = numpy.random.uniform(low=-0.1, high=0.1,size=(HL1_neurons, 1))
----> 6 weights = numpy.array([input_HL1_weights.astype(object),HL2_output_weights.astype(object)])

ValueError: could not broadcast input array from shape (15,15) into shape (15)

【问题讨论】:

  • 你认为numpy.array([input_HL1_weights,HL2_output_weights]) 会做什么?

标签: python numpy neural-network


【解决方案1】:

input_HL1_weights 是 (15,15) 形状。 HL2_output_weights 是 (15,1)。

试图从这两个中创建一个对象 dtype 数组,会导致这种错误。如果它们的形状在第一个维度上不同(例如它们的转置),则结果将是 (2,) 对象 dtype 数组。如果形状匹配,则形状将是 (2,n,m)。这是使用 np.array(...) 创建对象 dtype 数组的已知问题。

你到底想生产什么?

产生 (2,) 对象数组的更可靠的方法是:

weights = np.empty(2, object)
weights[0] = input_HL1_weights
weights[1] = HL2_output_weights

这消除了形状匹配或部分匹配时如何处理的歧义。

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 2018-06-08
    • 2020-01-21
    • 2020-10-18
    • 2018-09-25
    • 2018-04-21
    • 2020-10-09
    • 2021-03-30
    • 2021-08-24
    相关资源
    最近更新 更多