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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tensorflow 读取cifar_浅入浅出TensorFlow 4 - 训练CIFAR数据

發布時間:2025/4/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow 读取cifar_浅入浅出TensorFlow 4 - 训练CIFAR数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#coding=utf-8

import cifar10,cifar10_input

import tensorflow as tf

import numpy as np

import time

# define max_iter_step batch_size

max_iter_step = 1000

batch_size = 128

# define variable_with_weight_loss

# 和之前定義的weight有所不同,

# 這里定義附帶loss的weight,通過權重懲罰避免部分權重系數過大,導致overfitting

def variable_with_weight_loss(shape,stddev,w1):

var = tf.Variable(tf.truncated_normal(shape,stddev=stddev))

if w1 is not None:

weight_loss = tf.multiply(tf.nn.l2_loss(var),w1,name='weight_loss')

tf.add_to_collection('losses',weight_loss)

return var

# 下載數據集 - 調用cifar10函數下載并解壓

cifar10.maybe_download_and_extract()

cifar_dir = '/tmp/cifar10_data/cifar-10-batches-bin'

# 采用 data augmentation進行數據處理

# 生成訓練數據,訓練數據通過cifar10_input的distort變化

images_train, labels_train = cifar10_input.distorted_inputs(data_dir=cifar_dir,batch_size=batch_size)

# 測試數據(eval_data 測試數據)

images_test,labels_test = cifar10_input.inputs(eval_data=True,data_dir=cifar_dir,batch_size=batch_size)

# 創建輸入數據,采用 placeholder

x_input = tf.placeholder(tf.float32,[batch_size,24,24,3])

y_input = tf.placeholder(tf.int32,[batch_size])

# 創建第一個卷積層 input:3(channel) kernel:64 size:5*5

weight1 = variable_with_weight_loss(shape=[5,5,3,64],stddev=5e-2,w1=0.0)

bias1 = tf.Variable(tf.constant(0.0,shape=[64]))

conv1 = tf.nn.conv2d(x_input,weight1,[1,1,1,1],padding='SAME')

relu1 = tf.nn.relu(tf.nn.bias_add(conv1,bias1))

pool1 = tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME')

norm1 = tf.nn.lrn(pool1,4,bias=1.0,alpha=0.001/9.0,beta=0.75)

# 創建第二個卷積層 input:64 kernel:64 size:5*5

weight2 = variable_with_weight_loss(shape=[5,5,64,64],stddev=5e-2,w1=0.0)

bias2 = tf.Variable(tf.constant(0,1,shape=[64]))

conv2 = tf.nn.conv2d(norm1,weight2,[1,1,1,1],padding='SAME')

relu2 = tf.nn.relu(tf.nn.bias_add(conv2,bias2))

norm2 = tf.nn.lrn(relu2,4,bias=1.0,alpha=0.001/9.0,beta=0.75)

pool2 = tf.nn.max_pool(norm2,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME')

# 創建第三個層-全連接層 output:384

reshape = tf.reshape(pool2,[batch_size,-1])

dim = reshape.get_shape()[1].value

weight3 = variable_with_weight_loss(shape=[dim,384],stddev=0.04,w1=0.004)

bias3 = tf.Variable(tf.constant(0.1,shape=[384]))

local3 = tf.nn.relu(tf.matmul(reshape,weight3)+bias3)

# 創建第四個層-全連接層 output:192

weight4 = variable_with_weight_loss(shape=[384,192],stddev=0.04,w1=0.004)

bias4 = tf.Variable(tf.constant(0.1,shape=[192]))

# 最后一層 output:10

weight5 = variable_with_weight_loss(shape=[192,10],stddev=1/192.0,w1=0.0)

bias5 = tf.Variable(tf.constant(0.0,shape=[10]))

results = tf.add(tf.matmul(local4,weight5),bias5)

# 定義loss

def loss(results,labels):

labels = tf.cast(labels,tf.int64)

cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=results,labels=labels,name='cross_entropy_per_example')

cross_entropy_mean = tf.reduce_mean(cross_entropy,name='cross_entropy')

tf.add_to_collection('losses',cross_entropy_mean)

return tf.add_n(tf.get_collection('losses'),name='total_loss')

# 計算loss

loss = loss(results,y_input)

train_op = tf.train.AdamOptimizer(1e-3).minimize(loss) # Adam

top_k_op = tf.nn.in_top_k(results,y_input,1) # top1 準確率

sess = tf.InteractiveSession() # 創建session

tf.global_variable_initializer().run() # 初始化全部模型

tf.train.start_queue_runners() # 啟動多線程加速

# 開始訓練

for step in range(max_steps):

start_time = time.time()

image_batch,label_batch = sess.run([images_train,labels_train])

_,loss_value = sess.run([train_op,loss],

feed_dict={x_input:image_batch, y_input:label_batch})

duration = time.time() - start_time

if step % 10 == 0:

examples_per_sec = batch_size/duration

sec_per_batch = float(duration)

format_str = ('step%d,loss=%.2f(%.1fexamples/sec;%.3fsec/batch')

print(format_str % (step,loss_value,examples_per_sec,sec_per_batch))

# 評測模型在測試集上的準確度

num_examples = 10000

import math

num_iter = int(math.ceil(num_examples/batch_size))

true_count = 0

total_sample_count = num_iter * batch_size

step = 0

while step < num_iter:

image_batch,label_batch = sess.run([images_test,labels_test])

predictions = sess.run([top_k_op],feed_dict={x_input:image_batch,y_input:label_batch})

true_count += np.sum(predictions)

step += 1

# 打印結果

precision = true_count / total_sample_count

print('precision @ 1 =%.3f' % precision)

總結

以上是生活随笔為你收集整理的tensorflow 读取cifar_浅入浅出TensorFlow 4 - 训练CIFAR数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。