如何在用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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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