Facebook
From Burly Partdridge, 1 Year ago, written in Python.
Embed
Download Paste or View Raw
Hits: 113
  1. def train_neural_network(input_layer, hidden_layer, output_layer, actual_move):
  2.     layer_1_weights, layer_2_weights, bias = get_neural_network()
  3.     new_l1_weights = {}
  4.     new_l2_weights = {}
  5.  
  6.     # calculates total error
  7.     total_error = 0
  8.     for i in range(len(output_layer)):
  9.         if actual_move[0] * 3 + actual_move[1] == i:
  10.             total_error += ((1 - output_layer[i])**2)/2
  11.         else:
  12.             total_error += 0.5*((output_layer[i])**2)
  13.     print(total_error)
  14.  
  15.     # adjusts second layer weights
  16.     for i in range((Global_variables.hidden_layer_size + 1)*9):
  17.         if actual_move[0] * 3 + actual_move[1] == i % 9:
  18.             d_error_by_d_output_node = output_layer[i % 9] - 1
  19.         else:
  20.             d_error_by_d_output_node = output_layer[i % 9]
  21.         d_output_node_by_d_node_net_value = output_layer[i % 9]*(1 - output_layer[i % 9])
  22.         if i // 9 != Global_variables.hidden_layer_size:
  23.             d_node_net_value_by_d_weight = hidden_layer[i // 9]
  24.         else:
  25.             d_node_net_value_by_d_weight = bias
  26.         d_error_by_d_weight = d_error_by_d_output_node*d_output_node_by_d_node_net_value*d_node_net_value_by_d_weight
  27.         new_l2_weights[str(i // 9) + str(i % 9)] = \
  28.             layer_2_weights[str(i // 9) + str(i % 9)] - Global_variables.learning_rate*d_error_by_d_weight
  29.  
  30.     # adjusts bias
  31.     d_error_by_d_bias = 0
  32.     for i in range(9):
  33.         d_node_net_value_by_d_bias = layer_2_weights[str(Global_variables.hidden_layer_size) + str(i)]
  34.         d_output_node_by_d_node_net_value = output_layer[i]*(1 - output_layer[i])
  35.         if actual_move[0] * 3 + actual_move[1] == i:
  36.             d_error_by_d_output_node = output_layer[i] - 1
  37.         else:
  38.             d_error_by_d_output_node = output_layer[i]
  39.         d_error_by_d_bias += d_node_net_value_by_d_bias * d_output_node_by_d_node_net_value * d_error_by_d_output_node
  40.     bias = bias - Global_variables.learning_rate * d_error_by_d_bias
  41.  
  42.     # adjusts first layer weights
  43.     for i in range(Global_variables.hidden_layer_size*9):
  44.         d_error_by_d_weight = 0
  45.         if i // 9 != Global_variables.hidden_layer_size:
  46.             d_output_of_node_by_d_node_net_value = \
  47.                 hidden_layer[i % Global_variables.hidden_layer_size]*(1 - hidden_layer[i % Global_variables.hidden_layer_size])
  48.         else:
  49.             d_output_of_node_by_d_node_net_value = \
  50.                 bias * (1 - bias)
  51.         d_node_net_value_by_d_weight = input_layer[i // Global_variables.hidden_layer_size]
  52.         for j in range(9):
  53.             d_output_node_net_value_by_d_output_of_node = layer_2_weights[str(i // 9) + str(j)]
  54.             d_output_node_by_d_output_node_net_value = output_layer[j]*(1 - output_layer[j])
  55.             if actual_move[0] * 3 + actual_move[1] == i:
  56.                 d_error_by_d_output_node = output_layer[j] - 1
  57.             else:
  58.                 d_error_by_d_output_node = output_layer[j]
  59.             d_error_by_d_weight += d_output_of_node_by_d_node_net_value * d_node_net_value_by_d_weight * \
  60.                 d_output_node_net_value_by_d_output_of_node * d_output_node_by_d_output_node_net_value * \
  61.                 d_error_by_d_output_node
  62.         new_l1_weights[str(i // Global_variables.hidden_layer_size) + str(i % Global_variables.hidden_layer_size)] = \
  63.             layer_1_weights[str(i // Global_variables.hidden_layer_size) + str(i % Global_variables.hidden_layer_size)] - \
  64.             d_error_by_d_weight * Global_variables.learning_rate
  65.  
  66.     network_file = open("neural network", "w")
  67.     line = ""
  68.     for i in range(9 * Global_variables.hidden_layer_size):
  69.         line += str(new_l1_weights[str(i // Global_variables.hidden_layer_size) + str(i % Global_variables.hidden_layer_size)]) + " "
  70.     network_file.write(line + "\n")
  71.     line = ""
  72.     for i in range(9 * (Global_variables.hidden_layer_size + 1)):
  73.         line += str(new_l2_weights[str(i // 9) + str(i % 9)]) + " "
  74.     network_file.write(line + "\n")
  75.     network_file.write(str(bias))
  76.     network_file.close()