具有多个元素的数组的真值是不明确的。使用 a.any() 或 a.all() python numpy 使用 ReLu 函数
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() python numpy using ReLu function
import numpy as np
class NeuralNetwork():
def __init__(self):
np.random.seed(1)
self.synaptic_weights = np.random.random((8, 5))
def rectified(self, x):
return max(0, x)
def rectified_derivative(x):
x[x<=0] = 0
x[x>0] = 1
return x
def train(self, training_inputs, training_outputs, training_iterations):
for iteration in range(training_iterations):
output = self.think(training_inputs)
error = training_outputs - output
adjustments = np.dot(training_inputs.T, error * self.rectified_derivative(output))
self.synaptic_weights += adjustments
def think(self, inputs):
inputs = inputs.astype(float)
output = self.rectified(np.dot(inputs, self.synaptic_weights))
return output
不确定为什么会收到此错误。有人可以指出我正确的方向吗?错误在此行:
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
return max(0, x)
如果您试图将所有值限制为非负值,请使用 numpy.clip
,如下所示:
x.clip(0)
Python 的内置 max
运算符不能很好地处理 numpy 数组
import numpy as np
class NeuralNetwork():
def __init__(self):
np.random.seed(1)
self.synaptic_weights = np.random.random((8, 5))
def rectified(self, x):
return max(0, x)
def rectified_derivative(x):
x[x<=0] = 0
x[x>0] = 1
return x
def train(self, training_inputs, training_outputs, training_iterations):
for iteration in range(training_iterations):
output = self.think(training_inputs)
error = training_outputs - output
adjustments = np.dot(training_inputs.T, error * self.rectified_derivative(output))
self.synaptic_weights += adjustments
def think(self, inputs):
inputs = inputs.astype(float)
output = self.rectified(np.dot(inputs, self.synaptic_weights))
return output
不确定为什么会收到此错误。有人可以指出我正确的方向吗?错误在此行:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
return max(0, x)
如果您试图将所有值限制为非负值,请使用 numpy.clip
,如下所示:
x.clip(0)
Python 的内置 max
运算符不能很好地处理 numpy 数组