如何简化神经网络代码?拟构建更大规模网络避免冗余代码
问题描述
我已完成一个神经网络的编写,接下来计划构建规模更大的神经网络,但希望避免编写过于冗长的代码,请问该如何简化代码?以下是我当前的神经网络代码:
input[0] = (player_x - Game_logic.block_1x) input[1] = (player_y - Game_logic.block_1y) input[2] = (player_x - Game_logic.block_2x) input[3] = (player_y - Game_logic.block_2y) input[4] = (player_x - Game_logic.block_3x) input[5] = (player_y - Game_logic.block_3y) snk[0] = (((input[0] * weights[0])/10) + ((input[1] * weights[1])/10) + ((input[2] * weights[2])/10) + ((input[3] * weights[3])/10) + ((input[4] * weights[4])/10) + ((input[5] * weights[5])/10) +1) snk[1] = (((input[0] * weights[6])/10) + ((input[1] * weights[7])/10) + ((input[2] * weights[8])/10) + ((input[3] * weights[9])/10) + ((input[4] * weights[10])/10) + ((input[5] * weights[11])/10) +1) snk[2] = (((input[0] * weights[12])/10) + ((input[1] * weights[13])/10) + ((input[2] * weights[14])/10) + ((input[3] * weights[15])/10) + ((input[4] * weights[16])/10) + ((input[5] * weights[17])/10) +1) snk[3] = (((input[0] * weights[18])/10) + ((input[1] * weights[19])/10) + ((input[2] * weights[20])/10) + ((input[3] * weights[21])/10) + ((input[4] * weights[22])/10) + ((input[5] * weights[23])/10) +1) snk[4] = (((input[0] * weights[24])/10) + ((input[1] * weights[25])/10) + ((input[2] * weights[26])/10) + ((input[3] * weights[27])/10) + ((input[4] * weights[28])/10) + ((input[5] * weights[29])/10) +1) output[0] = ((snake_layer_1[0] * weights[30])/10) + ((snake_layer_1[1] * weights[31])/10) + ((snake_layer_1[2] * weights[32])/10) + ((snake_layer_1[3] * weights[33])/10) + ((snake_layer_1[4] * weights[34])/10) output[1] = ((snake_layer_1[0] * weights[35])/10) + ((snake_layer_1[1] * weights[36])/10) + ((snake_layer_1[2] * weights[37])/10) + ((snake_layer_1[3] * weights[38])/10) + ((snake_layer_1[4] * weights[39])/10) output[3] = ((snake_layer_1[0] * weights[40])/10) + ((snake_layer_1[1] * weights[41])/10) + ((snake_layer_1[2] * weights[42])/10) + ((snake_layer_1[3] * weights[43])/10) + ((snake_layer_1[4] * weights[44])/10)
回答
1. 用循环替代重复的输入赋值
输入部分是重复计算玩家与多个block的坐标差,可通过遍历block列表批量生成输入,后续新增block只需扩展列表即可:
# 将所有block坐标整理为列表 blocks = [(Game_logic.block_1x, Game_logic.block_1y), (Game_logic.block_2x, Game_logic.block_2y), (Game_logic.block_3x, Game_logic.block_3y)] input = [] for bx, by in blocks: input.append(player_x - bx) input.append(player_y - by)
2. 权重矩阵化+循环简化层计算
当前隐藏层(snk)和输出层的计算都是重复的加权求和,可将一维权重数组重构为二维矩阵,用循环批量计算每个神经元的输出:
处理隐藏层(snk)
把前30个权重(5个神经元×6个输入)拆分为5行6列的矩阵:
# 重构隐藏层权重矩阵 hidden_weights = [weights[i*6 : (i+1)*6] for i in range(5)] snk = [] for neuron_weights in hidden_weights: total = sum((in_val * w) / 10 for in_val, w in zip(input, neuron_weights)) total += 1 # 加上偏置项 snk.append(total)
处理输出层
把权重中30-44号元素(3个神经元×5个隐藏层节点)拆分为3行5列的矩阵:
# 重构输出层权重矩阵 output_weights = [weights[30 + i*5 : 30 + (i+1)*5] for i in range(3)] output = [] for neuron_weights in output_weights: total = sum((h_val * w) / 10 for h_val, w in zip(snk, neuron_weights)) output.append(total)
3. 抽象为函数复用逻辑
如果后续要扩展更多层,可将层计算逻辑封装成通用函数,避免重复编写循环:
def calculate_layer(inputs, weights_matrix, has_bias=False, bias_val=1, scale=10): """通用层计算函数""" layer_output = [] for neuron_weights in weights_matrix: total = sum((val * w) / scale for val, w in zip(inputs, neuron_weights)) if has_bias: total += bias_val layer_output.append(total) return layer_output # 调用示例 snk = calculate_layer(input, hidden_weights, has_bias=True) output = calculate_layer(snk, output_weights)
4. 用列表推导式进一步精简(可选)
若追求更紧凑的写法,可将外层循环替换为列表推导式,适合对代码逻辑熟悉后使用:
snk = [sum((in_val * w)/10 for in_val, w in zip(input, nw)) + 1 for nw in hidden_weights] output = [sum((h_val * w)/10 for h_val, w in zip(snk, nw)) for nw in output_weights]
内容的提问来源于stack exchange,提问作者FundLikVis
相关产品推荐
相关产品推荐

