我正在编码一个拟进行模式识别的ANN。代码是从强制转换数组的模型复制而来的,我做了一点点更改,因此它从 tabulate 强制转换了表,表中的1是模式中的黑色像素,而0是白色像素。这是一种简单的模式,而图纸是B,C和D。但是现在,我被卡住了,因为当我运行它时,它会向我返回此错误:
TypeError:无法根据规则“安全”将数组数据从dtype('float64')转换为dtype('
我试图通过关注其他用户的问题来解决它,但是那没有用。 这是代码:
from numpy import exp, array, random, dot
import numpy as np
from tabulate import tabulate
class NeuralNetwork():
def __init__(self):
random.seed(1)
self.synaptic_weights = 2 * random.random((5, 1)) - 1
def __sigmoid(self, x):
return 1 / (1 + exp(-x))
def __sigmoid_derivate(self, x):
return x * (1 - x)
def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
for iteration in range(number_of_training_iterations):
output = self.think(training_set_inputs)
error = training_set_outputs - output
adjustement = dot(training_set_inputs.T, error * self.__sigmoid_derivate(output))
self.synaptic_weights += adjustement
def think(self, inputs):
return self.__sigmoid(dot(inputs, self.synaptic_weights))
if __name__ == "__main__":
neural_network = NeuralNetwork()
input1 = [[1, 1, 1, 1, 0],
[1, 0, 0, 0, 1],
[1, 0, 1, 1, 0],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 0]]
input2 = [[0, 1, 1, 1, 1],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[0, 1, 1, 1, 1]]
input3 = [[1, 1, 1, 1, 0],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 0]]
input_n = [[0, 1, 1, 1, 0],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 1],
[0, 1, 1, 1, 0]]
print ("Random starting synaptic weights: ")
print (neural_network.synaptic_weights)
training_set_inputs = array([[tabulate(input1)], [tabulate(input2)], [tabulate(input3)]])
training_set_outputs = array([[1, 2, 3]])
#1 = B || 2 = C || 3 = D
print ("New synaptic weights after training: ")
print (neural_network.synaptic_weights)
print ("Considering new situation inputN -> ?: ")
print (neural_network.think(tabulate(input_n)))
我想请你帮忙。谢谢!