【发布时间】:2020-12-13 03:33:29
【问题描述】:
我想将self.weights 的特定元素(一个numpy 数组)设置为另一个numpy 数组,但出现这两个错误:
"TypeError: 只有 size-1 的数组可以转换为 Python 标量"
"ValueError: 设置一个带有序列的数组元素。".
例子:
self.weights=np.empty(some size)
self.weights[i]=np.random.randn(some size)
这是我的代码:
import numpy as np
import sys
np.set_printoptions(threshold=sys.maxsize)
class NeuralNetwork:
def __init__(self, inputs, hiddensSizes, outputs, lr, epochs):
self.inputs = inputs
self.inputsSize = self.inputs.shape[1]
self.outputs = outputs
self.outputsSize = self.outputs.shape[1]
self.lr = lr
self.epochs = epochs
self.hiddensSizes = hiddensSizes
self.weights = np.empty(len(self.hiddensSizes) + 1)
self.combinedLayers = np.hstack((self.inputsSize, self.hiddensSizes, self.outputsSize))
for i in range(len((self.weights))):
self.weights[i] = np.random.randn(self.combinedLayers[i], self.combinedLayers[i + 1]) #ERROR IS HERE
NN = NeuralNetwork(np.array([[0, 1, 0, 1, 1], [0, 0, 0, 0, 0]]), (3, 4), np.array([[0, 1, 0], [0, 0, 1]]), 0.1, 10000)
print(NN.weights)
【问题讨论】:
标签: python arrays numpy oop neural-network