本文实例为大家分享了Tensorflow实现AlexNet卷积神经网络的具体实现代码,供大家参考,具体内容如下
之前已经介绍过了AlexNet的网络构建了,这次主要不是为了训练数据,而是为了对每个批的前馈(向前)和反馈(向后)的平均耗时进行计算。在设计网络的过程中,分类的结果很重要,但是运算速率也相当重要,尤其是在跟踪(跟踪)的任务中,如果使用的网络太深,那么也会导致实时性不好。
从datetime进口datetime 导入数学 导入的时间 进口tensorflow特遣部队 batch_size=32 num_batches=100 def print_activations (t): 打印(t.op.name“t.get_shape () .as_list ()) def推理(图片): 参数=[] 与tf.name_scope (conv1)范围: 内核=tf.Variable (tf。truncated_normal ([11, 11, 64], dtype=特遣部队。float32 stddev=1 e 1) name=叭ㄖ亍? conv=tf.nn。conv2d(图像、内核(1、4、4、1)填充=跋嗤? 偏见=tf.Variable (tf.constant(0.0、形状=[64],dtype=tf.float32),可训练的=True, name=捌? 偏见=tf.nn。bias_add (conv偏见) conv1=tf.nn。relu(偏见,名称=范围) print_activations (conv1) 参数+=(内核,偏见) lrn1=tf.nn。lrn (conv1 4偏见=1.0,α=0.001/9,β=0.75,name=' lrn1 ') pool1=tf.nn。max_pool (lrn1 ksize=[1, 3, 3, 1],进步=(1、2、2、1)填充='有效' name=' pool1 ') print_activations (pool1) 与tf.name_scope (conv2)范围: 内核=tf.Variable (tf。truncated_normal ([5, 5、64、192], dtype=特遣部队。float32 stddev=1 e 1) name=叭ㄖ亍? conv=tf.nn。conv2d (pool1内核,(1,1,1,1),填充=跋嗤? 偏见=tf.Variable (tf.constant(0.0、形状=[192],dtype=tf.float32),可训练的=True, name=捌? 偏见=tf.nn。bias_add (conv偏见) conv2=tf.nn。relu(偏见,名称=范围) 参数+=(内核,偏见) print_activations (conv2) lrn2=tf.nn。lrn (conv2 4偏见=1.0,α=0.001/9,β=0.75,name=' lrn2 ') pool2=tf.nn。max_pool (lrn2 ksize=[1, 3, 3, 1],进步=(1、2、2、1)填充='有效' name=' pool2 ') print_activations (pool2) 与tf.name_scope (conv3)范围: 内核=tf.Variable (tf。truncated_normal ([3, 3, 192, 384], dtype=特遣部队。float32 stddev=1 e 1) name=叭ㄖ亍? conv=tf.nn。conv2d (pool2内核,(1,1,1,1),填充=跋嗤? 偏见=tf.Variable (tf.constant(0.0、形状=[384],dtype=tf.float32),可训练的=True, name=捌? 偏见=tf.nn。bias_add (conv偏见) conv3=tf.nn。relu(偏见,名称=范围) 参数+=(内核,偏见) print_activations (conv3) 与tf.name_scope (conv4)范围: 内核=tf.Variable (tf。truncated_normal ([3, 3, 384, 256], dtype=特遣部队。float32 stddev=1 e 1) name=叭ㄖ亍? conv=tf.nn。conv2d (conv3内核,(1,1,1,1),填充=跋嗤? 偏见=tf.Variable (tf.constant(0.0、形状=[256],dtype=tf.float32),可训练的=True, name=捌? 偏见=tf.nn。bias_add (conv偏见) conv4=tf.nn。relu(偏见,名称=范围) 参数+=(内核,偏见) print_activations (conv4) 与tf.name_scope (conv5)范围: 内核=tf.Variable (tf。truncated_normal ([3, 3, 256, 256], dtype=特遣部队。float32 stddev=1 e 1) name=叭ㄖ亍? conv=tf.nn。conv2d (conv4内核,(1,1,1,1),填充=跋嗤? 偏见=tf.Variable (tf.constant(0.0、形状=[256],dtype=tf.float32),可训练的=True, name=捌? 偏见=tf.nn。bias_add (conv偏见) conv5=tf.nn。relu(偏见,名称=范围) 参数+=(内核,偏见) print_activations (conv5) pool5=tf.nn。max_pool (conv5 ksize=[1, 3, 3, 1],进步=(1、2、2、1)填充='有效' name=' pool5 ') print_activations (pool5) 返回pool5,参数 def time_tensorflow_run(会话、目标info_string): num_steps_burn_in=10 total_duration=0.0 total_duration_squared=0.0 因为我在范围(num_batches + num_steps_burn_in): start_time=time.time () _=session.run(目标) 时间=time.time () - start_time 如果我在=num_steps_burn_in: 如果不是我% 10: 打印(“% d % s:一步,时间=%。3 f ' % (datetime.now(),我——num_steps_burn_in,持续时间)) total_duration +=持续时间 total_duration_squared +=*时间持续时间 mn=total_duration/num_batches vr=total_duration_squared/num_batches - mn *锰 sd=math.sqrt (vr) 打印(' % s:在% d % s的步骤,%。3 f +/- %。3 f秒/批' % (datetime.now (), info_string, num_batches, mn, sd)) def run_benchmark (): 用tf.Graph () .as_default (): image_size=224 图像=tf.Variable (tf。random_normal ([batch_size, image_size image_size 3], dtype=特遣部队。float32 stddev=1 e 1)) pool5参数=推理(图片) init=tf.global_variables_initializer () 税=tf.Session () sess.run (init) pool5 time_tensorflow_run(税,“前进”) 目标=tf.nn.l2_loss (pool5) 研究生=特遣部队。梯度(目标、参数) time_tensorflow_run(税、研究生“Forward-backward”) run_benchmark ()Tensorflow实现AlexNet卷积神经网络及运算时间评测