如何在用Python3编写的程序中循环隐藏层?

我已经编写了代码来执行深度学习的隐藏层方法。每个隐藏层都会分析输入数据,并将其传递给其他隐藏层,直到分析后的数据出现。

我可以创建任意数量的隐藏层。但是,如果我想创建50个隐藏层,那将需要很长时间和精力。因此,我考虑使用循环功能来节省时间和精力。然而,由于我是编程新手,这对我来说很困难。

这是程序代码:

import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("/tmp/data/", one_hot=True)n_nodes_hl1 = 500n_nodes_hl2 = 500n_nodes_hl3 = 500n_classes = 10batch_size = 100# height * widthx = tf.placeholder('float',[None, 784])y = tf.placeholder('float')def neural_network_model(data):   # (input_data * wehights) + biases   hidden_1_layer = {'weight' :tf.Variable(tf.random_normal([784, n_nodes_hl1])),                     'biases' :tf.Variable(tf.random_normal([n_nodes_hl1]))}   hidden_2_layer = {'weight' :tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),                     'biases' :tf.Variable(tf.random_normal([n_nodes_hl2]))}   hidden_3_layer = {'weight' :tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),                     'biases' :tf.Variable(tf.random_normal([n_nodes_hl3]))}   output_layer = {'weight' :tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),                     'biases' :tf.Variable(tf.random_normal([n_classes]))}   #    # (input_data * wehights) + biases   l1 = tf.add(tf.matmul(data, hidden_1_layer['weight']), hidden_1_layer['biases'])   l1 = tf.nn.relu(l1)   l2 = tf.add(tf.matmul(l1, hidden_2_layer['weight']), hidden_2_layer['biases'])   l2 = tf.nn.relu(l2)   l3 = tf.add(tf.matmul(l2, hidden_3_layer['weight']), hidden_3_layer['biases'])   l3 = tf.nn.relu(l3)   output = tf.matmul(l3, output_layer['weight']) + output_layer['biases']   return outputdef train_neural_network(x):   prediction = neural_network_model(x)   cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )   optimizer = tf.train.AdamOptimizer().minimize(cost)   hm_epochs = 20   with tf.Session() as sess:      sess.run(tf.initialize_all_variables())      for epoch in range(hm_epochs):      epoch_loss = 0      for _ in range(int(mnist.train.num_examples/batch_size)):         epoch_x, epoch_y = mnist.train.next_batch(batch_size)         _, c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y})         epoch_loss += c      print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss)      correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))      accuracy = tf.reduce_mean(tf.cast(correct, 'float'))      print('accuracy:', accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))train_neural_network(x)

以下是我需要循环的部分:

def neural_network_model(data):   # (input_data * wehights) + biases   hidden_1_layer = {'weight' :tf.Variable(tf.random_normal([784, n_nodes_hl1])),                     'biases' :tf.Variable(tf.random_normal([n_nodes_hl1]))}   hidden_2_layer = {'weight' :tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),                     'biases' :tf.Variable(tf.random_normal([n_nodes_hl2]))}   hidden_3_layer = {'weight' :tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),                     'biases' :tf.Variable(tf.random_normal([n_nodes_hl3]))}   output_layer = {'weight' :tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),                     'biases' :tf.Variable(tf.random_normal([n_classes]))}   #    # (input_data * wehights) + biases   l1 = tf.add(tf.matmul(data, hidden_1_layer['weight']), hidden_1_layer['biases'])   l1 = tf.nn.relu(l1)   l2 = tf.add(tf.matmul(l1, hidden_2_layer['weight']), hidden_2_layer['biases'])   l2 = tf.nn.relu(l2)   l3 = tf.add(tf.matmul(l2, hidden_3_layer['weight']), hidden_3_layer['biases'])   l3 = tf.nn.relu(l3)   output = tf.matmul(l3, output_layer['weight']) + output_layer['biases']   return output

回答:

我想你想要一个类似下面的方法来创建单个隐藏层:

def make_hidden(input_num, hidden_num):  return {'weight' :tf.Variable(tf.random_normal([input_num,                                                   hidden_num])),          'biases' :tf.Variable(tf.random_normal([hidden_num]))}

输出层可以像你之前那样创建。

def make_output(hidden_num, output_classes):  return {'weight' :tf.Variable(tf.random_normal([hidden_num,                                                   n_classes])),          'biases' :tf.Variable(tf.random_normal([n_classes]))}

然后,你有一个列表来存储每一层的节点数,从输入层开始,到最后一个隐藏层结束:

n_nodes = [0, 784, 500, 500, 500]     #     |___ 虚拟值,以便n_nodes[i]和n_nodes[i+1]存储第i个隐藏层的输入和隐藏节点数     #          (基于1的索引),因为layers[0]是输入层。

然后你的neural_network_model可以简化如下:

def neural_network_model(data, n_nodes):   layers = []*len(n_nodes)   layers[0] = data   for i in range(1, len(n_nodes)-1):     hidden_i = make_hidden(n_nodes[i], n_nodes[i+1])     layers[i] = tf.add(tf.matmul(layers[i-1], hidden_i['weight']), hidden_i['biases'])     layers[i] = tf.nn.relu(layers[i])   output_layer = make_output(n_nodes[-1], n_classes)   output = tf.matmul(layers[-1], output_layer['weight']) + output_layer['biases']   return output

你可能还需要一些小的修改来使代码正常工作。我希望你能理解循环隐藏层的要点。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注