1.将标签数字转化成列向量,如
def convert_to_one_hot(Y, C):#Y为标签,C为类别数
Y = np.eye(C)[Y.reshape(-1)].T
return Y
Y.shape = (1,m),m为样本数
np.eye()返回一个单位矩阵,[Y.reshape(-1)]的shape = (m,)中的m决定列方向的延展数量,而行方向的元素个数由C决定,行方向置为1的位置由Y1.reshape(-1)这个list的值所提供,转置后得到最终的结果。
2.为X,Y创建占位符,X为样本,Y为标签
def create_placeholders(n_H0, n_W0, n_C0, n_y):
"""
Creates the placeholders for the tensorflow session.
Arguments:
n_H0 -- scalar, height of an input image
n_W0 -- scalar, width of an input image
n_C0 -- scalar, number of channels of the input
n_y -- scalar, number of classes
Returns:
X -- placeholder for the data input, of shape [None, n_H0, n_W0,n_C0] and dtype "float"
Y -- placeholder for the input labels, of shape [None, n_y] and dtype "float"
"""
X = tf.placeholder(tf.float32, shape=(None, n_H0, n_W0, n_C0))
Y = tf.placeholder(tf.float32, shape=(None, n_y))
3.卷积层
tf.nn.conv2d(
input,
filter,
strides,
padding,
use_cudnn_on_gpu=True,
data_format='NHWC',
dilations=[1, 1, 1, 1],
name=None
)
input:输入,形状为[batch, in_height, in_width, in_channels]
filter:过滤器(W,即权重),形状为[filter_height, filter_width, in_channels, out_channels]
strides:步长,[1,s,s,1]表示对于输入每一维的步长
padding:填充,有same和VALID两种。
padding与正常理解的不同:
padding = ‘SAME’, 计算公式为
padding = ‘VALID’, 计算公式为
并且均向上取整
详细见:https://www.tensorflow.org/api_docs/python/tf/nn/conv2d
4.最大池化层
tf.nn.max_pool(
value,
ksize,
strides,
padding,
data_format='NHWC',
name=None
)
value: