日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人工智能 > 卷积神经网络 >内容正文

卷积神经网络

tensorflow学习笔记七----------卷积神经网络

發(fā)布時間:2023/12/10 卷积神经网络 85 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow学习笔记七----------卷积神经网络 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

卷積神經(jīng)網(wǎng)絡(luò)比神經(jīng)網(wǎng)絡(luò)稍微復(fù)雜一些,因為其多了一個卷積層(convolutional layer)和池化層(pooling layer)。

使用mnist數(shù)據(jù)集,n個數(shù)據(jù),每個數(shù)據(jù)的像素為28*28*1=784。先讓這些數(shù)據(jù)通過第一個卷積層,在這個卷積上指定一個3*3*1的feature,這個feature的個數(shù)設(shè)為64。接著經(jīng)過一個池化層,讓這個池化層的窗口為2*2。然后在經(jīng)過一個卷積層,在這個卷積上指定一個3*3*64的feature,這個featurn的個數(shù)設(shè)置為128,。接著經(jīng)過一個池化層,讓這個池化層的窗口為2*2。讓結(jié)果經(jīng)過一個全連接層,這個全連接層大小設(shè)置為1024,在經(jīng)過第二個全連接層,大小設(shè)置為10,進行分類。

import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('data/', one_hot=True) trainimg = mnist.train.images trainlabel = mnist.train.labels testimg = mnist.test.images testlabel = mnist.test.labels print ("MNIST ready") #像素點為784 n_input = 784 #十分類 n_output = 10 #wc1,第一個卷積層參數(shù),3*3*1,共有64個 #wc2,第二個卷積層參數(shù),3*3*64,共有128個 #wd1,第一個全連接層參數(shù),經(jīng)過兩個池化層被壓縮到7*7 #wd2,第二個全連接層參數(shù) weights = {'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64], stddev=0.1)),'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.1)),'wd1': tf.Variable(tf.random_normal([7*7*128, 1024], stddev=0.1)),'wd2': tf.Variable(tf.random_normal([1024, n_output], stddev=0.1))} biases = {'bc1': tf.Variable(tf.random_normal([64], stddev=0.1)),'bc2': tf.Variable(tf.random_normal([128], stddev=0.1)),'bd1': tf.Variable(tf.random_normal([1024], stddev=0.1)),'bd2': tf.Variable(tf.random_normal([n_output], stddev=0.1))}

定義前向傳播函數(shù)。先將輸入數(shù)據(jù)預(yù)處理,變成tensorflow支持的四維圖像;進行第一層的卷積層處理,調(diào)用conv2d函數(shù);將卷積結(jié)果用激活函數(shù)進行處理(relu函數(shù));將結(jié)果進行池化層處理,ksize代表窗口大小;將池化層的結(jié)果進行隨機刪除節(jié)點;進行第二層卷積和池化...;進行全連接層,先將數(shù)據(jù)進行reshape(此處為7*7*128);進行激活函數(shù)處理;得出結(jié)果。前向傳播結(jié)束。

def conv_basic(_input, _w, _b, _keepratio):# INPUT_input_r = tf.reshape(_input, shape=[-1, 28, 28, 1])# CONV LAYER 1_conv1 = tf.nn.conv2d(_input_r, _w['wc1'], strides=[1, 1, 1, 1], padding='SAME')_conv1 = tf.nn.relu(tf.nn.bias_add(_conv1, _b['bc1']))_pool1 = tf.nn.max_pool(_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')_pool_dr1 = tf.nn.dropout(_pool1, _keepratio)# CONV LAYER 2_conv2 = tf.nn.conv2d(_pool_dr1, _w['wc2'], strides=[1, 1, 1, 1], padding='SAME')_conv2 = tf.nn.relu(tf.nn.bias_add(_conv2, _b['bc2']))_pool2 = tf.nn.max_pool(_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')_pool_dr2 = tf.nn.dropout(_pool2, _keepratio)# VECTORIZE_dense1 = tf.reshape(_pool_dr2, [-1, _w['wd1'].get_shape().as_list()[0]])# FULLY CONNECTED LAYER 1_fc1 = tf.nn.relu(tf.add(tf.matmul(_dense1, _w['wd1']), _b['bd1']))_fc_dr1 = tf.nn.dropout(_fc1, _keepratio)# FULLY CONNECTED LAYER 2_out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2'])# RETURNout = { 'input_r': _input_r, 'conv1': _conv1, 'pool1': _pool1, 'pool1_dr1': _pool_dr1,'conv2': _conv2, 'pool2': _pool2, 'pool_dr2': _pool_dr2, 'dense1': _dense1,'fc1': _fc1, 'fc_dr1': _fc_dr1, 'out': _out}return out print ("CNN READY")

定義損失函數(shù),定義優(yōu)化器

x = tf.placeholder(tf.float32, [None, n_input]) y = tf.placeholder(tf.float32, [None, n_output]) keepratio = tf.placeholder(tf.float32)# FUNCTIONS _pred = conv_basic(x, weights, biases, keepratio)['out'] cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(_pred, y)) optm = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost) _corr = tf.equal(tf.argmax(_pred,1), tf.argmax(y,1)) accr = tf.reduce_mean(tf.cast(_corr, tf.float32)) init = tf.global_variables_initializer()# SAVER save_step = 1 saver = tf.train.Saver(max_to_keep=3) print ("GRAPH READY")

進行迭代

do_train = 1 sess = tf.Session() sess.run(init)training_epochs = 15 batch_size = 16 display_step = 1 if do_train == 1:for epoch in range(training_epochs):avg_cost = 0.total_batch = int(mnist.train.num_examples/batch_size)# Loop over all batchesfor i in range(total_batch):batch_xs, batch_ys = mnist.train.next_batch(batch_size)# Fit training using batch datasess.run(optm, feed_dict={x: batch_xs, y: batch_ys, keepratio:0.7})# Compute average lossavg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keepratio:1.})/total_batch# Display logs per epoch stepif epoch % display_step == 0: print ("Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost))train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys, keepratio:1.})print (" Training accuracy: %.3f" % (train_acc))#test_acc = sess.run(accr, feed_dict={x: testimg, y: testlabel, keepratio:1.})#print (" Test accuracy: %.3f" % (test_acc))print ("OPTIMIZATION FINISHED")

?

轉(zhuǎn)載于:https://www.cnblogs.com/xxp17457741/p/9480521.html

總結(jié)

以上是生活随笔為你收集整理的tensorflow学习笔记七----------卷积神经网络的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。