简单神经网络
tf.random_normal()函数用于从“服从指定正态分布的序列”中随机取出指定个数的值。
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
x = tf.random.normal([2,784])
tf.Variable(initializer,name),参数initializer是初始化参数,name是可自定义的变量名称。
tf.random.truncated_normal()用法——截断正态分布
输出截断的正态分布的随机值。
tf.random.truncated_normal(
shape,
mean=0.0,
stddev=1.0,
dtype=tf.dtypes.float32,
seed=None,
name=None
)
shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
mean: A 0-D Tensor or Python value of type dtype. The mean of the truncated normal distribution.均值默认为0
stddev: A 0-D Tensor or Python value of type dtype. The standard deviation of the normal distribution, before truncation.截断前正态分布的标准偏差,默认为1.0
dtype: The type of the output.
seed: A Python integer. Used to create a random seed for the distribution. Seetf.compat.v1.set_random_seed for behavior.
name: A name for the operation (optional).
tf.multiply()两个矩阵中对应元素各自相乘
tf.matmul()将矩阵a乘以矩阵b,生成a * b
dense :全连接层 相当于添加一个层
tf.layers.dense(
inputs,
units,
activation=None,
use_bias=True,
kernel_initializer=None, ##卷积核的初始化器
bias_initializer=tf.zeros_initializer(), ##偏置项的初始化器,默认初始化为0
kernel_regularizer=None, ##卷积核的正则化,可选
bias_regularizer=None, ##偏置项的正则化,可选
activity_regularizer=None, ##输出的正则化函数
kernel_constraint=None,
bias_constraint=None,
trainable=True,
name=None, ##层的名字
reuse=None ##是否重复使用参数
)
部分参数解释:
inputs:输入该网络层的数据
units:输出的维度大小,改变inputs的最后一维
activation:激活函数,即神经网络的非线性变化
use_bias:使用bias为True(默认使用),不用bias改成False即可,是否使用偏置项
trainable=True:表明该层的参数是否参与训练。如果为真则变量加入到图集合中
GraphKeys.TRAINABLE_VARIABLES (see tf.Variable)
在其他网站上看到的使用现象
dense1 = tf.layers.dense(inputs=pool3, units=1024, activation=tf.nn.relu,
kernel_regularizer=tf.contrib.layers.l2_regularizer(0.003))
全连接层
dense1 = tf.layers.dense(inputs=pool3, units=1024, activation=tf.nn.relu)
dense2= tf.layers.dense(inputs=dense1, units=512, activation=tf.nn.relu)
logits= tf.layers.dense(inputs=dense2, units=10, activation=None)