Я пытаюсь построить нейронную сеть, в которой структура моих выходных данных представляет собой список шаблонов, а не одно целые значения.
вот то, что я уже попробовал, я построил нейронную сеть, используя перцептрон и сигмоидальную функцию.
from math import exp
from random import seed
from random import random
import numpy as np # helps with the math
import matplotlib.pyplot as plt # to plot error during training
import os
def initialize_network(n_inputs, n_hidden, n_outputs):
network = list()
hidden_layer = [{'weights':[random() for i in range(n_inputs + 1)]} for i in range(n_hidden)]
network.append(hidden_layer)
output_layer = [{'weights':[random() for i in range(n_hidden + 1)]} for i in range(n_outputs)]
network.append(output_layer)
return network
def activate(weights, inputs):
activation = weights[-1]
for i in range(len(weights)-1):
activation += weights[i] * inputs[i]
return activation
no_class = 1
def transfer(activation):
return 1.0 / (1.0 + exp(-activation))
def forward_propagate(network, row):
inputs = row
for layer in network:
new_inputs = []
for neuron in layer:
activation = activate(neuron['weights'], inputs)
neuron['output'] = transfer(activation)
#print("neuron['output']", neuron['output'])
new_inputs.append(neuron['output'])
inputs = new_inputs
return inputs
def transfer_derivative(output):
return output * (1.0 - output)
#new_neuron['output'] = 0
# Backpropagate error and store in neurons
def backward_propagate_error(network, expected):
for i in reversed(range(len(network))):
layer = network[i]
errors = list()
if i != len(network)-1:
for j in range(len(layer)):
error = 0.0
for neuron in network[i + 1]:
error += (neuron['weights'][j] * neuron['delta'])
errors.append(error)
else:
for j in range(len(layer)):
neuron = layer[j]
#print("old neuron['output']", neuron['output'])
errors.append(expected[j] - neuron['output'])
#print("neuron['output'] 3", neuron['output'])
for j in range(len(layer)):
neuron = layer[j]
neuron['delta'] = errors[j] * transfer_derivative(neuron['output'])
def update_weights(network, row, l_rate):
for i in range(len(network)):
inputs = row[:-1]
if i != 0:
inputs = [neuron['output'] for neuron in network[i - 1]]
for neuron in network[i]:
for j in range(len(inputs)):
neuron['weights'][j] += l_rate * neuron['delta'] * inputs[j]
neuron['weights'][-1] += l_rate * neuron['delta']
def train_network(network, train, l_rate, n_epoch, n_outputs):
for epoch in range(n_epoch):
lattest_outputs = []
sum_error = 0
for row in train:
outputs = forward_propagate(network, row)
#print("old output", outputs)
##new_outputs = outputs*3
#print(" new outputs", new_outputs)
expected = [0 for i in range(n_outputs)]
expected[row[-1]] = 1
sum_error += sum([(expected[i]-outputs[i])**2 for i in range(len(expected))])
backward_propagate_error(network, expected)
update_weights(network, row, l_rate)
print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error))
seed(1)
dataset = [[[0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0],
[0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1]]
n_inputs = len(dataset[0]) - 1
n_outputs = len(set([row[-1] for row in dataset]))
print("N outputs", n_outputs)
network = initialize_network(n_inputs, 2, n_outputs)
#print("network", network)
train_network(network, dataset, 0.1, 200, n_outputs)
Так что здесь мои выводы являются целочисленными значениями, то есть 0 или 1
Но я хочу, чтобы мой персептрон работал для выходов в виде шаблонов, таких как [0,0,0,1,0,1,0] и [1,0,0,1,0,0,0] вместо 1 или 0.
Любая нейронная сеть может быть использована вместо персептрона. Как мне поступить?