cifar10 (2) 訓練模型

參考博客:github.com/kevin28520/M

tf.slice用法tensorflow學習(三):操作圖片的tf.slice()函數

TF Boys (TensorFlow Boys ) 養成記(二): TensorFlow 數據讀取

Tensorflow樣例代碼分析cifar10 - 追憶次風暴 - 博客園

tensorflow數據讀取機制:十圖詳解tensorflow數據讀取機制(附代碼)

import os import os.pathimport mathimport numpy as npimport tensorflow as tfimport cifar10_inputBATCH_SIZE = 128learning_rate = 0.05MAX_STEP = 10000def inference(images): Args: image:4D tensor [batch_size,img_width,img_height,img_channel] kernel size :[kernel_size,kernel_size,input channels,output channels] #conv1 , [5,5,3,96] 輸入圖片3通道,卷積核數量96,大小(5,5) with tf.variable_scope(conv1) as scope: weights = tf.get_variable(weights, shape = [3,3,3,96], dtype = tf.float32, initializer = tf.truncated_normal_initializer(stddev=0.05,dtype=tf.float32)) biases = tf.get_variable(biaes, shape=[96], dtype=tf.float32, initializer=tf.constant_initializer(0.0)) #[128,32,32,3]>>[128,32,32,96] conv = tf.nn.conv2d(images,weights,strides=[1,1,1,1],padding=SAME) print(conv.shape) pre_activation = tf.nn.bias_add(conv,biases) conv1 = tf.nn.relu(pre_activation,name=scope.name) #pool1 and norm1 with tf.variable_scope(pooling1_lrn) as scope: #[128,32,32,96]>>[128,16,16,96] pool1 = tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1], padding=SAME,name=pooling1) print(pool1.shape) norm1 = tf.nn.lrn(pool1,depth_radius=4,bias=1.0,alpha=0.001/9.0, beta=0.75,name=norm1) #conv2 with tf.variable_scope(conv2) as scope: weights = tf.get_variable(weights, shape=[3,3,96,64], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.05,dtype=tf.float32)) biases = tf.get_variable(biases, shape=[64], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) #[128,16,16,96]>>[128,16,16,64] conv = tf.nn.conv2d(norm1,weights,strides=[1,1,1,1],padding=SAME) pre_activation = tf.nn.bias_add(conv,biases) conv2 = tf.nn.relu(pre_activation,name=conv2) print(conv2.shape) #pool2 and norm2 with tf.variable_scope(pooling2_lrn) as scope: norm2 = tf.nn.lrn(conv2,depth_radius=4,bias=1.0,alpha=0.001/9.0, beta=0.75,name=norm2) #[128,16,16,64]>>[128,16,16,64] pool2 = tf.nn.max_pool(norm2,ksize=[1,3,3,1],strides=[1,1,1,1], padding=SAME,name=pooling2) print(pool2.shape) #local3 with tf.variable_scope(local3) as scope: reshape = tf.reshape(pool2,shape=[BATCH_SIZE,-1]) dim = reshape.get_shape()[1].value print(dim)#16384 weights = tf.get_variable(weights, shape=[dim,384], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.004,dtype=tf.float32)) biases = tf.get_variable(biases, shape=[384], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) local3 = tf.nn.relu(tf.matmul(reshape,weights) + biases,name=scope.name) print(local3.shape)#(128,384) #local4 with tf.variable_scope(local4) as scope: weights = tf.get_variable(weights, shape=[384,192], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.004,dtype=tf.float32)) biases = tf.get_variable(biases, shape=[192], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) local4 = tf.nn.relu(tf.matmul(local3,weights) + biases,name=local4) print(local4.shape)#(128,192) #softmax with tf.variable_scope(softmax_linear) as scope: weights = tf.get_variable(softmax_linear, shape=[192,10], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.004,dtype=tf.float32)) biases = tf.get_variable(biases, shape=[10], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) softmax_linear = tf.add(tf.matmul(local4,weights),biases,name=softmax_linear) print(softmax_linear.shape)#(128,10) return softmax_lineardef losses(logits,labels): with tf.variable_scope(loss) as scope: labels = tf.cast(labels,tf.int64) #使用這個函數,需要用one-hot編碼 cross_entropy = tf.nn.softmax_cross_entropy_with_logits (logits=logits,labels=labels,name=xentropy_per_example) loss = tf.reduce_mean(cross_entropy,name=loss)# tf.summary.scalar(scope,name +/loss,loss) return lossdef train(): my_global_step = tf.Variable(0,name=global_step,trainable=False) data_dir = C:eclipseeclipseworkspacecifar10數據集整理cifar-10-batches-py log_dir = C:eclipseeclipseworkspacecifar10數據集整理log images ,labels = cifar10_input.read_cifar10(data_dir=data_dir, is_train=True, batch_size=BATCH_SIZE, shuffle=True) logits = inference(images) loss = losses(logits,labels) optimizer = tf.train.GradientDescentOptimizer(learning_rate) train_op = optimizer.minimize(loss,global_step=my_global_step) saver = tf.train.Saver(tf.global_variables()) summary_op = tf.summary.merge_all() init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess,coord=coord) summary_writer = tf.summary.FileWriter(log_dir,sess.graph) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _,loss_value = sess.run([train_op,loss]) if step %50 == 0: print(Step: %d, loss: %.4f % (step,loss_value)) if step % 100 == 0 : summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str,step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path .join(log_dir,model.ckpt) saver.save(sess,checkpoint_path,global_step=step) except tf.errors.OutOfRangeError: print(Done training -- epoch limit reached) finally: coord.request_stop() coord.join(threads) sess.close()train()

推薦閱讀:

初學者如何學好數控編程呢?
佛系建模-ICM中加入LID設施後如何調整子集水區面積
使用midas civil未知荷載係數功能求斜拉橋初索力係數 ?
如何方便的使用Infoworks ICM對比兩個不同2d方案的淹水結果
建築能耗分析軟體簡介系列2---Equest簡介

TAG:模型 | 建模 | 機器學習 |