import math
import random
def dot_product(v1, v2):
return sum(x*y for x, y in zip(v1, v2))
def vector_add(v1, v2):
return [x+y for x, y in zip(v1, v2)]
def scalar_vector_product(scalar, vector):
return [scalar * x for x in vector]
def matrix_vector_product(matrix, vector):
return [dot_product(row, vector) for row in matrix]
def transpose(matrix):
return list(map(list, zip(*matrix)))
def output(ans):
a=int(ans*100+0.5)
print('%.2f' % (a/100))
class NeuralNet(object):
def __init__(self):
self.synaptic_weights = [0.5] * 6
def __sigmoid(self, x):
return 1 / (1 + math.exp(-x))
def __sigmoid_derivative(self, x):
return x * (1 - x)
def train(self, inputs, outputs, training_iterations):
for iteration in range(training_iterations):
output = self.learn(inputs)
error = [o - out for o, out in zip(outputs, output)]
adjustments = []
for i, inp in enumerate(transpose(inputs)):
adjustments.append(sum(error[j] * self.__sigmoid_derivative(output[j]) * inp[j] for j in range(len(inp))))
self.synaptic_weights = vector_add(self.synaptic_weights, adjustments)
def learn(self, inputs):
result = matrix_vector_product(inputs, self.synaptic_weights)
return [self.__sigmoid(x) for x in result]
if __name__ == "__main__":
neural_network = NeuralNet()
inputs = [
[0.4, 0.3, 0.4, 0.6, 0.5, 0.3],
[0.3, 0.5, 0.8, 0.4, 0.2, 0.5],
[0.5, 0.6, 0.2, 0.8, 0.7, 0.5],
[0.4, 0.3, 0.4, 0.2, 0.3, 0.4],
[0.3, 0.2, 0.3, 0.2, 0.2, 0.4],
[0.4, 0.3, 0.4, 0.6, 0.5, 0.4],
[0.7, 0.6, 0.8, 0.7, 0.8, 0.6],
[0.3, 0.2, 0.3, 0.2, 0.4, 0.2]
]
outputs = [0.38, 0.45, 0.62, 0.38, 0.26, 0.47, 0.75, 0.24]
neural_network.train(inputs, outputs, 11000)
test_input = [float(x) for x in input().split()]
#test_input = [float(x) for x in input("").replace(',', ' ').split()]
#print(neural_network.learn([test_input]))
#test_input = [float(x) for x in input("").replace(',', ' ').split()]
result = neural_network.learn([test_input])[0]
#print(f"{result:.2f}")
# print(format( result, '.2f'))
#print(neural_network.learn([[0.5, 0.4, 0.5, 0.6, 0.4, 0.4]]))
output(result)
信息安全管理作业-L
最新推荐文章于 2025-12-05 17:02:52 发布
3104

被折叠的 条评论
为什么被折叠?



