欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

tensorflow實(shí)現(xiàn)殘差網(wǎng)絡(luò)方式(mnist數(shù)據(jù)集)

 更新時(shí)間:2020年05月26日 14:23:36   作者:Tom Hardy  
這篇文章主要介紹了tensorflow實(shí)現(xiàn)殘差網(wǎng)絡(luò)方式(mnist數(shù)據(jù)集),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

介紹

殘差網(wǎng)絡(luò)是何凱明大神的神作,效果非常好,深度可以達(dá)到1000層。但是,其實(shí)現(xiàn)起來并沒有那末難,在這里以tensorflow作為框架,實(shí)現(xiàn)基于mnist數(shù)據(jù)集上的殘差網(wǎng)絡(luò),當(dāng)然只是比較淺層的。

如下圖所示:

實(shí)線的Connection部分,表示通道相同,如上圖的第一個(gè)粉色矩形和第三個(gè)粉色矩形,都是3x3x64的特征圖,由于通道相同,所以采用計(jì)算方式為H(x)=F(x)+x

虛線的的Connection部分,表示通道不同,如上圖的第一個(gè)綠色矩形和第三個(gè)綠色矩形,分別是3x3x64和3x3x128的特征圖,通道不同,采用的計(jì)算方式為H(x)=F(x)+Wx,其中W是卷積操作,用來調(diào)整x維度的。

根據(jù)輸入和輸出尺寸是否相同,又分為identity_block和conv_block,每種block有上圖兩種模式,三卷積和二卷積,三卷積速度更快些,因此在這里選擇該種方式。

具體實(shí)現(xiàn)見如下代碼:

#tensorflow基于mnist數(shù)據(jù)集上的VGG11網(wǎng)絡(luò),可以直接運(yùn)行
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
#tensorflow基于mnist實(shí)現(xiàn)VGG11
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

#x=mnist.train.images
#y=mnist.train.labels
#X=mnist.test.images
#Y=mnist.test.labels
x = tf.placeholder(tf.float32, [None,784])
y = tf.placeholder(tf.float32, [None, 10])
sess = tf.InteractiveSession()

def weight_variable(shape):
#這里是構(gòu)建初始變量
 initial = tf.truncated_normal(shape, mean=0,stddev=0.1)
#創(chuàng)建變量
 return tf.Variable(initial)

def bias_variable(shape):
 initial = tf.constant(0.1, shape=shape)
 return tf.Variable(initial)

#在這里定義殘差網(wǎng)絡(luò)的id_block塊,此時(shí)輸入和輸出維度相同
def identity_block(X_input, kernel_size, in_filter, out_filters, stage, block):
 """
 Implementation of the identity block as defined in Figure 3

 Arguments:
 X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
 kernel_size -- integer, specifying the shape of the middle CONV's window for the main path
 filters -- python list of integers, defining the number of filters in the CONV layers of the main path
 stage -- integer, used to name the layers, depending on their position in the network
 block -- string/character, used to name the layers, depending on their position in the network
 training -- train or test

 Returns:
 X -- output of the identity block, tensor of shape (n_H, n_W, n_C)
 """

 # defining name basis
 block_name = 'res' + str(stage) + block
 f1, f2, f3 = out_filters
 with tf.variable_scope(block_name):
  X_shortcut = X_input

  #first
  W_conv1 = weight_variable([1, 1, in_filter, f1])
  X = tf.nn.conv2d(X_input, W_conv1, strides=[1, 1, 1, 1], padding='SAME')
  b_conv1 = bias_variable([f1])
  X = tf.nn.relu(X+ b_conv1)

  #second
  W_conv2 = weight_variable([kernel_size, kernel_size, f1, f2])
  X = tf.nn.conv2d(X, W_conv2, strides=[1, 1, 1, 1], padding='SAME')
  b_conv2 = bias_variable([f2])
  X = tf.nn.relu(X+ b_conv2)

  #third

  W_conv3 = weight_variable([1, 1, f2, f3])
  X = tf.nn.conv2d(X, W_conv3, strides=[1, 1, 1, 1], padding='SAME')
  b_conv3 = bias_variable([f3])
  X = tf.nn.relu(X+ b_conv3)
  #final step
  add = tf.add(X, X_shortcut)
  b_conv_fin = bias_variable([f3])
  add_result = tf.nn.relu(add+b_conv_fin)

 return add_result


#這里定義conv_block模塊,由于該模塊定義時(shí)輸入和輸出尺度不同,故需要進(jìn)行卷積操作來改變尺度,從而得以相加
def convolutional_block( X_input, kernel_size, in_filter,
    out_filters, stage, block, stride=2):
 """
 Implementation of the convolutional block as defined in Figure 4

 Arguments:
 X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
 kernel_size -- integer, specifying the shape of the middle CONV's window for the main path
 filters -- python list of integers, defining the number of filters in the CONV layers of the main path
 stage -- integer, used to name the layers, depending on their position in the network
 block -- string/character, used to name the layers, depending on their position in the network
 training -- train or test
 stride -- Integer, specifying the stride to be used

 Returns:
 X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
 """

 # defining name basis
 block_name = 'res' + str(stage) + block
 with tf.variable_scope(block_name):
  f1, f2, f3 = out_filters

  x_shortcut = X_input
  #first
  W_conv1 = weight_variable([1, 1, in_filter, f1])
  X = tf.nn.conv2d(X_input, W_conv1,strides=[1, stride, stride, 1],padding='SAME')
  b_conv1 = bias_variable([f1])
  X = tf.nn.relu(X + b_conv1)

  #second
  W_conv2 =weight_variable([kernel_size, kernel_size, f1, f2])
  X = tf.nn.conv2d(X, W_conv2, strides=[1,1,1,1], padding='SAME')
  b_conv2 = bias_variable([f2])
  X = tf.nn.relu(X+b_conv2)

  #third
  W_conv3 = weight_variable([1,1, f2,f3])
  X = tf.nn.conv2d(X, W_conv3, strides=[1, 1, 1,1], padding='SAME')
  b_conv3 = bias_variable([f3])
  X = tf.nn.relu(X+b_conv3)
  #shortcut path
  W_shortcut =weight_variable([1, 1, in_filter, f3])
  x_shortcut = tf.nn.conv2d(x_shortcut, W_shortcut, strides=[1, stride, stride, 1], padding='VALID')

  #final
  add = tf.add(x_shortcut, X)
  #建立最后融合的權(quán)重
  b_conv_fin = bias_variable([f3])
  add_result = tf.nn.relu(add+ b_conv_fin)


 return add_result



x = tf.reshape(x, [-1,28,28,1])
w_conv1 = weight_variable([2, 2, 1, 64])
x = tf.nn.conv2d(x, w_conv1, strides=[1, 2, 2, 1], padding='SAME')
b_conv1 = bias_variable([64])
x = tf.nn.relu(x+b_conv1)
#這里操作后變成14x14x64
x = tf.nn.max_pool(x, ksize=[1, 3, 3, 1],
    strides=[1, 1, 1, 1], padding='SAME')


#stage 2
x = convolutional_block(X_input=x, kernel_size=3, in_filter=64, out_filters=[64, 64, 256], stage=2, block='a', stride=1)
#上述conv_block操作后,尺寸變?yōu)?4x14x256
x = identity_block(x, 3, 256, [64, 64, 256], stage=2, block='b' )
x = identity_block(x, 3, 256, [64, 64, 256], stage=2, block='c')
#上述操作后張量尺寸變成14x14x256
x = tf.nn.max_pool(x, [1, 2, 2, 1], strides=[1,2,2,1], padding='SAME')
#變成7x7x256
flat = tf.reshape(x, [-1,7*7*256])

w_fc1 = weight_variable([7 * 7 *256, 1024])
b_fc1 = bias_variable([1024])

h_fc1 = tf.nn.relu(tf.matmul(flat, w_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
w_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, w_fc2) + b_fc2


#建立損失函數(shù),在這里采用交叉熵函數(shù)
cross_entropy = tf.reduce_mean(
 tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_conv))

train_step = tf.train.AdamOptimizer(1e-3).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#初始化變量

sess.run(tf.global_variables_initializer())

print("cuiwei")
for i in range(2000):
 batch = mnist.train.next_batch(10)
 if i%100 == 0:
 train_accuracy = accuracy.eval(feed_dict={
 x:batch[0], y: batch[1], keep_prob: 1.0})
 print("step %d, training accuracy %g"%(i, train_accuracy))
 train_step.run(feed_dict={x: batch[0], y: batch[1], keep_prob: 0.5})

以上這篇tensorflow實(shí)現(xiàn)殘差網(wǎng)絡(luò)方式(mnist數(shù)據(jù)集)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python用內(nèi)置模塊來構(gòu)建REST服務(wù)與RPC服務(wù)實(shí)戰(zhàn)

    Python用內(nèi)置模塊來構(gòu)建REST服務(wù)與RPC服務(wù)實(shí)戰(zhàn)

    這篇文章主要介紹了Python用內(nèi)置模塊來構(gòu)建REST服務(wù)與RPC服務(wù)實(shí)戰(zhàn),python在網(wǎng)絡(luò)方面封裝一些內(nèi)置模塊,可以用很簡潔的代碼實(shí)現(xiàn)端到端的通信,比如HTTP、RPC服務(wù),下文實(shí)戰(zhàn)詳情,需要的朋友可以參考一下
    2022-09-09
  • Python 日期區(qū)間處理 (本周本月上周上月...)

    Python 日期區(qū)間處理 (本周本月上周上月...)

    這篇文章主要介紹了Python 日期區(qū)間處理 (本周本月上周上月...),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python讀寫配置文件操作示例

    python讀寫配置文件操作示例

    這篇文章主要介紹了python讀寫配置文件操作,結(jié)合實(shí)例形式分析了Python針對(duì)ini配置文件的讀取、解析、寫入等相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • Windows10+anacond+GPU+pytorch安裝詳細(xì)過程

    Windows10+anacond+GPU+pytorch安裝詳細(xì)過程

    這篇文章主要介紹了Windows10+anacond+GPU+pytorch安裝詳細(xì)過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Tensorflow訓(xùn)練模型默認(rèn)占滿所有GPU的解決方案

    Tensorflow訓(xùn)練模型默認(rèn)占滿所有GPU的解決方案

    這篇文章主要介紹了Tensorflow訓(xùn)練模型默認(rèn)占滿所有GPU的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Python實(shí)現(xiàn)Pig Latin小游戲?qū)嵗a

    Python實(shí)現(xiàn)Pig Latin小游戲?qū)嵗a

    這篇文章主要介紹了Python實(shí)現(xiàn)Pig Latin小游戲?qū)嵗a,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • python根據(jù)路徑導(dǎo)入模塊的方法

    python根據(jù)路徑導(dǎo)入模塊的方法

    這篇文章主要介紹了python根據(jù)路徑導(dǎo)入模塊的方法,分析了傳統(tǒng)方法與改進(jìn)方法,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-09-09
  • Python?urllib?入門使用詳細(xì)教程

    Python?urllib?入門使用詳細(xì)教程

    urllib?庫,它是?Python?內(nèi)置的?HTTP?請(qǐng)求庫,不需要額外安裝即可使用,這篇文章主要介紹了Python?urllib?入門使用,需要的朋友可以參考下
    2022-11-11
  • python 中sys.getsizeof的用法說明

    python 中sys.getsizeof的用法說明

    這篇文章主要介紹了python 中sys.getsizeof的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • NumPy內(nèi)存布局的實(shí)現(xiàn)

    NumPy內(nèi)存布局的實(shí)現(xiàn)

    本文主要介紹了NumPy內(nèi)存布局的實(shí)現(xiàn),括連續(xù)內(nèi)存布局(C順序)和分散內(nèi)存布局(Fortran順序),并通過實(shí)例演示如何操作數(shù)組的內(nèi)存布局,感興趣的可以了解一下
    2024-01-01

最新評(píng)論