赞
踩
# 导入所需的库 import numpy as np # 定义一个人工智能类 class AI: def __init__(self): self.weights = np.random.rand(3) # 随机初始化权重 def predict(self, inputs): summation = np.dot(inputs, self.weights) output = self.activation(summation) return output def activation(self, x): return 1 / (1 + np.exp(-x)) def train(self, training_inputs, labels, iterations): for iteration in range(iterations): output = self.predict(training_inputs) error = labels - output adjustment = np.dot(training_inputs.T, error * output * (1 - output)) self.weights += adjustment # 创建一个训练数据集 training_inputs = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]]) labels = np.array([[0, 1, 1, 0]]).T # 初始化人工智能对象 ai = AI() # 训练人工智能 ai.train(training_inputs, labels, iterations=10000) # 使用训练好的人工智能进行预测 print(ai.predict(np.array([1, 0, 0])))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。