列表中的for循环有什么问题?

时间:2019-04-14 18:09:26

标签: arrays python-3.x numpy

我正在尝试使用for循环来定义列表:

import numpy as np
# 2 input neurons , next 3 hidden , 5 hidden , 2 output neurons
layer_sizes = { 2,3,5,2 }
# for matrix shapes 3,2  5,3 and 2,5
weight_shapes = [{a,b} for a,b in zip(layer_sizes[1:],layer_sizes[:-1])]
#weight_shapes = [ {3,2},{5,3},{2,5}]
weights = [np.zeros(s) for s in weight_shapes]
print(weight_shapes)
print(weights)

但是我一直收到此错误:

Traceback (most recent call last):
  File "C:\Users\USER\NNe2.py", line 5, in <module>
    weight_shapes = [{a,b} for a,b in zip(layer_sizes[1:],layer_sizes[:-1])]
TypeError: 'set' object is not subscriptable

我希望打印语句能够提供列表的内容。

1 个答案:

答案 0 :(得分:0)

您的layer_sizesset {2, 3, 5}, 但是您的评论表明您希望长度为list,长度为4, 好像是这样声明的:

layer_sizes = [2, 3, 5, 2]