from datetime import datetime
import math
import time
import tensorflow as tf
首先构建conv_op函数,用于创建卷积层并把本层的参数存入参数列表;
def conv_op(input_op, name, kh,kw, n_out, dh, dw, p):
n_in = input_op.get_shape()[-1].value
with tf.name_scope(name) as scope:
kernel = tf.get_variable(scope+"w",shape=[kh, kw, n_in, n_out], dtype = tf.float32, initializer = tf.contrib.layers.xavier_initializer_conv2d())
input_op是输入的tensor,
name是这一层的名称,
kh是kernel height即为卷积核的高,
kw是卷积核的宽,
n_out是卷积核数量即为输出通道,
dh是步长的高,
dw是步长的宽,
p是参数列表。
通过get_shape()[-1].value获取输入的tensor的通道数;
通过tf.name_scope(name)设置scope;
通过get_variable创建卷积核参数。
shape为[kh,kw,n_in,n_out]即为卷积核的高,卷积核的宽,输入通道数,输出通道数;
使用tf.contrib.layers.xavier_initializer_conv2d()进行参数初始化。
conv = tf.nn.conv2d(input_op, kernel, (1, dh, dw, 1),padding='SAME')
bias_init_val = tf.constant(0.0, shape=[n_out], dtype=tf.float32)
biases = tf.Variable(bias_init_val, trainable=True, name='b')
z = tf.nn.bias_add(conv, biases)
activation = tf.nn.relu(z,name=scope)
p+=[kernel,biases]
return activation
通过tf.nn.conv2d对input