最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

TensorFlow深度學(xué)習(xí)另一種程序風(fēng)格實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)

 更新時(shí)間:2021年11月04日 08:47:08   作者:零尾  
這篇文章主要介紹了TensorFlow卷積神經(jīng)網(wǎng)絡(luò)的另一種程序風(fēng)格實(shí)現(xiàn)方式示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
import tensorflow as tf
import numpy as np
import input_data
mnist = input_data.read_data_sets('data/', one_hot=True)
print("MNIST ready")
n_input  = 784 # 28*28的灰度圖,像素個(gè)數(shù)784
n_output = 10  # 是10分類問題
# 權(quán)重項(xiàng)
weights = {
    # conv1,參數(shù)[3, 3, 1, 32]分別指定了filter的h、w、所連接輸入的維度、filter的個(gè)數(shù)即產(chǎn)生特征圖個(gè)數(shù)
    'wc1': tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.1)),   
    # conv2,這里參數(shù)3,3同上,32是當(dāng)前連接的深度是32,即前面特征圖的個(gè)數(shù),64為輸出的特征圖的個(gè)數(shù)
    'wc2': tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.1)), 
    # fc1,將特征圖轉(zhuǎn)換為向量,1024由自己定義
    'wd1': tf.Variable(tf.random_normal([7*7*64, 1024], stddev=0.1)), 
    # fc2,做10分類任務(wù),前面連1024,輸出10分類
    'wd2': tf.Variable(tf.random_normal([1024, n_output], stddev=0.1)) 
}
"""
特征圖大小計(jì)算:
f_w = (w-f+2*pad)/s + 1 = (28-3+2*1)/1 + 1 = 28 # 說明經(jīng)過卷積層并沒有改變圖片的大小
f_h = (h-f+2*pad)/s + 1 = (28-3+2*1)/1 + 1 = 28
# 特征圖的大小是經(jīng)過池化層后改變的
第一次pooling后28*28變?yōu)?4*14
第二次pooling后14*14變?yōu)?*7,即最終是一個(gè)7*7*64的特征圖

"""
# 偏置項(xiàng)
biases = {
    'bc1': tf.Variable(tf.random_normal([32], stddev=0.1)),      # conv1,對應(yīng)32個(gè)特征圖
    'bc2': tf.Variable(tf.random_normal([64], stddev=0.1)),      # conv2,對應(yīng)64個(gè)特征圖
    'bd1': tf.Variable(tf.random_normal([1024], stddev=0.1)),    # fc1,對應(yīng)1024個(gè)向量
    'bd2': tf.Variable(tf.random_normal([n_output], stddev=0.1)) # fc2,對應(yīng)10個(gè)輸出
}

def conv_basic(_input, _w, _b, _keep_prob):
    # INPUT
    # 對圖像做預(yù)處理,轉(zhuǎn)換為tf支持的格式,即[n, h, w, c],-1是確定好其它3維后,讓tf去推斷剩下的1維
    _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') 
    # [1, 1, 1, 1]分別代表batch_size、h、w、c的stride
    # padding有兩種選擇:'SAME'(窗口滑動時(shí),像素不夠會自動補(bǔ)0)或'VALID'(不夠就跳過)兩種選擇
    _conv1 = tf.nn.relu(tf.nn.bias_add(_conv1, _b['bc1'])) # 卷積層后連激活函數(shù)
    # 最大值池化,[1, 2, 2, 1]其中1,1對應(yīng)batch_size和channel,2,2對應(yīng)2*2的池化
    _pool1 = tf.nn.max_pool(_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    # 隨機(jī)殺死一些神經(jīng)元,_keepratio為保留神經(jīng)元比例,如0.6 
    _pool_dr1 = tf.nn.dropout(_pool1, _keep_prob) 

    # 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, _keep_prob) # dropout

    # VECTORIZE向量化
    # 定義全連接層的輸入,把pool2的輸出做一個(gè)reshape,變?yōu)橄蛄康男问?
    _densel = tf.reshape(_pool_dr2, [-1, _w['wd1'].get_shape().as_list()[0]]) 

    # FULLY CONNECTED LAYER 1
    _fc1 = tf.nn.relu(tf.add(tf.matmul(_densel, _w['wd1']), _b['bd1'])) # w*x+b,再通過relu
    _fc_dr1 = tf.nn.dropout(_fc1, _keep_prob) # dropout

    # FULLY CONNECTED LAYER 2
    _out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2']) # w*x+b,得到結(jié)果

    # RETURN
    out = {'input_r': _input_r, 'conv1': _conv1, 'pool1': _pool1, 'pool_dr1': _pool_dr1,
           'conv2': _conv2, 'pool2': _pool2, 'pool_dr2': _pool_dr2, 'densel': _densel,
           'fc1': _fc1, 'fc_dr1': _fc_dr1, 'out': _out
           }
    return out
print("CNN READY")
x = tf.placeholder(tf.float32, [None, n_input]) # 用placeholder先占地方,樣本個(gè)數(shù)不確定為None
y = tf.placeholder(tf.float32, [None, n_output]) # 用placeholder先占地方,樣本個(gè)數(shù)不確定為None
keep_prob = tf.placeholder(tf.float32)
_pred = conv_basic(x, weights, biases, keep_prob)['out'] # 前向傳播的預(yù)測值
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(_pred, y)) # 交叉熵?fù)p失函數(shù)
optm = tf.train.AdamOptimizer(0.001).minimize(cost) # 梯度下降優(yōu)化器
_corr = tf.equal(tf.argmax(_pred, 1), tf.argmax(y, 1)) # 對比預(yù)測值索引和實(shí)際label索引,相同返回True,不同返回False
accr = tf.reduce_mean(tf.cast(_corr, tf.float32)) # 將True或False轉(zhuǎn)換為1或0,并對所有的判斷結(jié)果求均值
init = tf.global_variables_initializer()
print("FUNCTIONS READY")

# 上面神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)定義好之后,下面定義一些超參數(shù)
training_epochs = 1000 # 所有樣本迭代1000次
batch_size = 100 # 每進(jìn)行一次迭代選擇100個(gè)樣本
display_step = 1
# LAUNCH THE GRAPH
sess = tf.Session() # 定義一個(gè)Session
sess.run(init) # 在sess里run一下初始化操作
# OPTIMIZE
for epoch in range(training_epochs):
    avg_cost = 0.
    total_batch = int(mnist.train.num_examples/batch_size)
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 逐個(gè)batch的去取數(shù)據(jù)
        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys, keep_prob:0.5})
        avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob:1.0})/total_batch
    if epoch % display_step == 0:
        train_accuracy = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.0})
        test_accuracy = sess.run(accr, feed_dict={x: mnist.test.images, y: mnist.test.labels, keep_prob:1.0})
        print("Epoch: %03d/%03d cost: %.9f TRAIN ACCURACY: %.3f TEST ACCURACY: %.3f"
              % (epoch, training_epochs, avg_cost, train_accuracy, test_accuracy))
print("DONE")

我用的顯卡是GTX960,在跑這個(gè)卷積神經(jīng)網(wǎng)絡(luò)的時(shí)候,第一次filter分別設(shè)的是64和128,結(jié)果報(bào)蜜汁錯(cuò)誤了,反正就是我顯存不足,所以改成了32和64,讓特征圖少一點(diǎn)。所以,是讓我換1080的意思嘍

I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:885] Found device 0 with properties: 
name: GeForce GTX 960
major: 5 minor: 2 memoryClockRate (GHz) 1.304
pciBusID 0000:01:00.0
Total memory: 4.00GiB
Free memory: 3.33GiB
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:906] DMA: 0 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:916] 0:   Y 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 960, pci bus id: 0000:01:00.0)
W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\bfc_allocator.cc:217] Ran out of memory trying to allocate 2.59GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\bfc_allocator.cc:217] Ran out of memory trying to allocate 1.34GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\bfc_allocator.cc:217] Ran out of memory trying to allocate 2.10GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\bfc_allocator.cc:217] Ran out of memory trying to allocate 3.90GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
Epoch: 000/1000 cost: 0.517761162 TRAIN ACCURACY: 0.970 TEST ACCURACY: 0.967
Epoch: 001/1000 cost: 0.093012387 TRAIN ACCURACY: 0.960 TEST ACCURACY: 0.979
.
.
.
省略

以上就是TensorFlow另一種程序風(fēng)格實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)的詳細(xì)內(nèi)容,更多關(guān)于TensorFlow卷積神經(jīng)網(wǎng)絡(luò)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python星號*與**用法分析

    Python星號*與**用法分析

    這篇文章主要介紹了Python星號*與**用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python中的星號*與**在函數(shù)參數(shù)及數(shù)值運(yùn)算中的相關(guān)使用技巧,需要的朋友可以參考下
    2018-02-02
  • 利用matplotlib實(shí)現(xiàn)兩張子圖分別畫函數(shù)圖

    利用matplotlib實(shí)現(xiàn)兩張子圖分別畫函數(shù)圖

    這篇文章主要介紹了利用matplotlib實(shí)現(xiàn)兩張子圖分別畫函數(shù)圖問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python基于checksum計(jì)算文件是否相同的方法

    Python基于checksum計(jì)算文件是否相同的方法

    這篇文章主要介紹了Python基于checksum計(jì)算文件是否相同的方法,涉及Python針對二進(jìn)制文件的讀取與判定技巧,需要的朋友可以參考下
    2015-07-07
  • Python中關(guān)于元組 集合 字符串 函數(shù) 異常處理的全面詳解

    Python中關(guān)于元組 集合 字符串 函數(shù) 異常處理的全面詳解

    本篇文章介紹了我在學(xué)習(xí)python過程中對元組、集合、字符串、函數(shù)、異常處理的總結(jié),通讀本篇對大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下
    2021-10-10
  • Python中性能分析利器pyinstrument詳細(xì)講解

    Python中性能分析利器pyinstrument詳細(xì)講解

    大家好,本篇文章主要講的是Python中性能分析利器pyinstrument詳細(xì)講解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Python?Pandas數(shù)據(jù)合并pd.merge用法詳解

    Python?Pandas數(shù)據(jù)合并pd.merge用法詳解

    在合并數(shù)據(jù)的操作中,除了pd.concat()函數(shù),另一個(gè)常用的函數(shù)就是pd.merge()了,下面這篇文章主要給大家介紹了關(guān)于Python?Pandas數(shù)據(jù)合并pd.merge用法的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • Python中yield關(guān)鍵字的理解與使用

    Python中yield關(guān)鍵字的理解與使用

    yield關(guān)鍵字用于創(chuàng)建生成器函數(shù),一種高效利用內(nèi)存的函數(shù)類型,可以像迭代器對象一樣使用,本文主要介紹了Python中的yield關(guān)鍵字的應(yīng)用,需要的可以參考下
    2023-08-08
  • PyTorch中permute的基本用法示例

    PyTorch中permute的基本用法示例

    pytorch中的permute就像是numpy中的transpose()函數(shù)一樣,根據(jù)指定的維度進(jìn)行轉(zhuǎn)置,下面這篇文章主要給大家介紹了關(guān)于PyTorch中permute的基本用法,需要的朋友可以參考下
    2022-04-04
  • python中eval的用法及說明

    python中eval的用法及說明

    這篇文章主要介紹了python中eval的用法及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Python入門之模塊與包

    Python入門之模塊與包

    這篇文章主要為大家介紹了Python的模塊與包,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12

最新評論

方城县| 固始县| 连平县| 大姚县| 广元市| 仙桃市| 广德县| 磐石市| 阳江市| 巫山县| 张家界市| 衢州市| 安远县| 镇赉县| 和平区| 施秉县| 自治县| 泗水县| 廊坊市| 蕲春县| 镇坪县| 桂阳县| 昌吉市| 满城县| 永福县| 沿河| 延吉市| 昌平区| 丹寨县| 鹤壁市| 台南县| 清新县| 罗甸县| 阿城市| 类乌齐县| 徐水县| 广昌县| 三门峡市| 富蕴县| 岳西县| 微山县|