在笔记7中,笔者和大家一起入门了 tensorflow的基本语法,并举了一些实际的例子进行了说明,终于告别了使用numpy手动搭建的日子。所以我们将继续往下走,看看如何利用 tensorflow搭建神经网络模型。
尽管对于初学者而言使用tensorflow看起来并不那么习惯,需要各种步骤,但简单来说,tensorflow搭建模型实际就是两个过程:创建计算图和执行计算图。在 deeplearningai 课程中,ng和他的课程组给我们提供了signs dataset(手势)数据集,其中训练集包括1080张64x64像素的手势图片,并给定了 6 种标注,测试集包括120张64x64的手势图片,我们需要对训练集构建神经网络模型然后对测试集给出预测。
先来简单看一下数据集:
# loading the datasetx_train_orig, y_train_orig, x_test_orig, y_test_orig, classes = load_dataset()# flatten the training and test imagesx_train_flatten = x_train_orig.reshape(x_train_orig.shape[0], -1).t x_test_flatten = x_test_orig.reshape(x_test_orig.shape[0], -1).t# normalize image vectorsx_train = x_train_flatten/255.x_test = x_test_flatten/255.# convert training and test labels to one hot matricesy_train = convert_to_one_hot(y_train_orig, 6) y_test = convert_to_one_hot(y_test_orig, 6)print (number of training examples = + str(x_train.shape[1]))print (number of test examples = + str(x_test.shape[1]))print (x_train shape: + str(x_train.shape))print (y_train shape: + str(y_train.shape))print (x_test shape: + str(x_test.shape))print (y_test shape: + str(y_test.shape))
下面就根据 ng 给定的找个数据集利用tensorflow搭建神经网络模型。我们选择构建一个包含 2 个隐层的神经网络,网络结构大致如下:linear -> relu -> linear -> relu -> linear -> softmax正如我们之前利用numpy手动搭建一样,搭建一个神经网络的主要步骤如下:-定义网络结构-初始化模型参数-执行前向计算/计算当前损失/执行反向传播/权值更新
创建 placeholder
根据tensorflow的语法,我们首先创建输入x和输出y的占位符变量,这里需要注意shape参数的设置。
def create_placeholders(n_x, n_y): x = tf.placeholder(tf.float32, shape=(n_x, none), name='x') y = tf.placeholder(tf.float32, shape=(n_y, none), name='y') return x, y
初始化模型参数
其次就是初始化神经网络的模型参数,三层网络包括六个参数,这里我们采用xavier初始化方法:
def initialize_parameters(): tf.set_random_seed(1) w1 = tf.get_variable(w1, [25, 12288], initializer = tf.contrib.layers.xavier_initializer(seed = 1)) b1 = tf.get_variable(b1, [25, 1], initializer = tf.zeros_initializer()) w2 = tf.get_variable(w2, [12, 25], initializer = tf.contrib.layers.xavier_initializer(seed = 1)) b2 = tf.get_variable(b2, [12, 1], initializer = tf.zeros_initializer()) w3 = tf.get_variable(w3, [6, 12], initializer = tf.contrib.layers.xavier_initializer(seed = 1)) b3 = tf.get_variable(b3, [6,1], initializer = tf.zeros_initializer()) parameters = {w1: w1, b1: b1, w2: w2, b2: b2, w3: w3, b3: b3} return parameters
执行前向传播
def forward_propagation(x, parameters): implements the forward propagation for the model: linear -> relu -> linear -> relu -> linear -> softmax w1 = parameters['w1'] b1 = parameters['b1'] w2 = parameters['w2'] b2 = parameters['b2'] w3 = parameters['w3'] b3 = parameters['b3'] z1 = tf.add(tf.matmul(w1, x), b1) a1 = tf.nn.relu(z1) z2 = tf.add(tf.matmul(w2, a1), b2) a2 = tf.nn.relu(z2) z3 = tf.add(tf.matmul(w3, a2), b3) return z3
计算损失函数
在tensorflow中损失函数的计算要比手动搭建时方便很多,一行代码即可搞定:
def compute_cost(z3, y): logits = tf.transpose(z3) labels = tf.transpose(y) cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = labels)) return cost
代码整合:执行反向传播和权值更新
跟计算损失函数类似,tensorflow中执行反向传播的梯度优化非常简便,两行代码即可搞定,定义完整的神经网络模型如下:
def model(x_train, y_train, x_test, y_test, learning_rate = 0.0001, num_epochs = 1500, minibatch_size = 32, print_cost = true): ops.reset_default_graph() tf.set_random_seed(1) seed = 3 (n_x, m) = x_train.shape n_y = y_train.shape[0] costs = [] # create placeholders of shape (n_x, n_y) x, y = create_placeholders(n_x, n_y) # initialize parameters parameters = initialize_parameters() # forward propagation: build the forward propagation in the tensorflow graph z3 = forward_propagation(x, parameters) # cost function: add cost function to tensorflow graph cost = compute_cost(z3, y) # backpropagation: define the tensorflow optimizer. use an adamoptimizer. optimizer = tf.train.gradientdescentoptimizer(learning_rate = learning_rate).minimize(cost) # initialize all the variables init = tf.global_variables_initializer() # start the session to compute the tensorflow graph with tf.session() as sess: # run the initialization sess.run(init) # do the training loop for epoch in range(num_epochs): epoch_cost = 0. num_minibatches = int(m / minibatch_size) seed = seed + 1 minibatches = random_mini_batches(x_train, y_train, minibatch_size, seed) for minibatch in minibatches: # select a minibatch (minibatch_x, minibatch_y) = minibatch _ , minibatch_cost = sess.run([optimizer, cost], feed_dict={x: minibatch_x, y: minibatch_y}) epoch_cost += minibatch_cost / num_minibatches # print the cost every epoch if print_cost == true and epoch % 100 == 0: print (cost after epoch %i: %f % (epoch, epoch_cost)) if print_cost == true and epoch % 5 == 0: costs.append(epoch_cost) # plot the cost plt.plot(np.squeeze(costs)) plt.ylabel('cost') plt.xlabel('iterations (per tens)') plt.title(learning rate = + str(learning_rate)) plt.show() # lets save the parameters in a variable parameters = sess.run(parameters) print (parameters have been trained!) # calculate the correct predictions correct_prediction = tf.equal(tf.argmax(z3), tf.argmax(y)) # calculate accuracy on the test set accuracy = tf.reduce_mean(tf.cast(correct_prediction, float)) print (train accuracy:, accuracy.eval({x: x_train, y: y_train})) print (test accuracy:, accuracy.eval({x: x_test, y: y_test})) return parameters
执行模型:
parameters = model(x_train, y_train, x_test, y_test)
根据模型的训练误差和测试误差可以看到:模型整体效果虽然没有达到最佳,但基本也能达到预测效果。
总结
tensorflow语法中两个基本的对象类是 tensor 和 operator.
tensorflow执行计算的基本步骤为
创建计算图(张量、变量和占位符变量等)
创建会话
初始化会话
在计算图中执行会话
可以看到的是,在 tensorflow 中编写神经网络要比我们手动搭建要方便的多,这也正是深度学习框架存在的意义之一。功能强大的深度学习框架能够帮助我们快速的搭建起复杂的神经网络模型,在经历了手动搭建神经网络的思维训练过程之后,这对于我们来说就不再困难了。
路由器有信号没网络怎么回事
联发科技展示5G基带芯片Helio M70:向下兼容4G
罗德与施瓦茨示波器分析脉冲宽度调制信号
常见的异形LED屏有什么样的原理及特点
uClinux下动态Web技术的实现方法
如何利用Tensorflow搭建神经网络?
预测性维护应把ICS网络纳入防护范围
石墨烯电池:电动汽车可能三分钟把电充满,石墨烯到底是什么?
经纬恒润推出车载T-Box产线测试系统
三星Note8太贵,iPhoneX难产:华为或靠华为Mate10统治高端手机市场
空调市场趋于增长,可为何会库存严重?问题竟在这里!
为什么HALT测试和HASS测试在PDLC中很重要
小米6原型机曝光:骁龙835或联发科Helio X30处理器?
详解5G新体制天线技术
如何利用Raspberry Pi建造半自动4WD汽车
充电宝哪个牌子好耐用质量好,值得入手的充电宝推荐
5G加速助推自动驾驶更加的成熟
如何正确挑选水银接头
索尼XperiaXZ和iPhone7Plus哪个好
PCB设计软件allegro蓝牙音箱案例实操讲解