65.9K
CodeProject 正在变化。 阅读更多。
Home

TensorFlow 教程 5:运行神经网络

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2投票s)

2018 年 10 月 16 日

CPOL

2分钟阅读

viewsIcon

7295

作者:Dante Sblendorio

在本次竞赛的前几部分,我们已经完成了安装 TensorFlow、设置 Jupyter Notebook 以及创建神经网络的所有步骤。现在是真正有趣的部分:运行神经网络并查看我们的结果。请继续阅读,完成我们旅程的最后一步。

现在我们已经定义了所有必需的函数,就可以构建神经网络了。我们首先根据训练集的形状初始化所有变量和参数,然后定义学习率、epoch 数和 batch size。学习率决定了数学运算收敛到最小成本值的速度。epoch 数是训练数据通过网络循环的次数。batch size 是每个随机子样本的大小。每个参数在最终测试准确率以及网络收敛速度方面都发挥着作用。

from tensorflow.python.framework import ops
import math

def nn(train_x, train_y, test_x, test_y, learning_rate ,num_epochs, batch_size, print_cost = True):
       ops.reset_default_graph()
       tf.set_random_seed(1)
       (n_x, m) = train_x.shape
       n_y = train_y.shape[0]
       
       # Initialize
       costs = []
       X = tf.placeholder(tf.float32, [n_x, None], name="X")
       Y = tf.placeholder(tf.float32, [n_y, None], name="Y")
       parameters = init_parameters(13)
       Z3 = for_prop(X, parameters)
       cost = c(Z3, Y)
       optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
       init = tf.global_variables_initializer()

       # Forward propagation
       with tf.Session() as sess:
           sess.run(init)

           #training loop
           for epoch in range(num_epochs):
               epoch_cost = 0.
               num_batches = int(m / batch_size)
               batches = rand_batches(train_x, train_y, batch_size, 13)
               for batch in batches:
                   (batch_X, batch_Y) = batch
                   _ , batch_cost = sess.run([optimizer, cost], feed_dict={X: batch_X, Y: batch_Y})
                   epoch_cost += batch_cost / num_batches

               # print the cost at every 50 epochs
               if print_cost == True and epoch % 50 == 0:
                   print ("Epoch %i cost: %f" % (epoch, epoch_cost))
               if print_cost == True and epoch % 5 == 0:
                   costs.append(epoch_cost)

           # Save the parameters
           parameters = sess.run(parameters)
           print("Parameters trained...")

           # Calculate the correct predictions
           correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

           # Accuracy of the test set
           accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

           print("Train Accuracy:", accuracy.eval({X: train_x, Y: train_y}))
           print("Test Accuracy:", accuracy.eval({X: test_x, Y: test_y}))

           return parameters

现在我们已经定义了神经网络函数,就可以为每个参数选择值并运行了。

learning_rate = 0.001 #change this to change learning rate
num_epochs = 1000     #change this to change number of epochs
batch_size = 15       #change this to change batch size
parameters = nn(train_x, train_y, test_x, test_y, learning_rate ,num_epochs, batch_size)

在学习率、epoch 数和 batch size 定义为当前值的情况下,神经网络在测试集上表现良好,准确预测 Class 的次数超过 96%。尝试更改每个参数的值,并观察准确率的变化(这也是一个很好的练习,可以更好地理解神经网络的工作原理)。关于学习率、epoch 和 batch size,有一篇很棒的文章可以阅读 这里

要生成挑战 5 的参赛代码,请在 Jupyter notebook 中创建一个新的代码单元格,并将以下代码粘贴到其中

member_number = 12345678

one = [member_number, int(member_number/5), int(member_number/25), int(member_number/50), int(member_number/100)]

two = [0.02, 0.05, 0.08, 0.11, 0.14]

a = tf.placeholder(tf.float32, shape=(5))

b = tf.placeholder(tf.float32, shape=(5))

result = tf.tensordot(a, b, 1)

with tf.Session() as sess:

   print(int(result.eval(feed_dict={a: one, b: two})))

接下来,将 12345678 替换为您的 CodeProject 会员号码。运行代码,打印出来的数字将是您参加 AI 竞赛最终挑战的参赛代码。请点击 这里 输入竞赛参赛代码。

© . All rights reserved.