1、损失函数的loss值一直维持在0.69?
首先0.69是个什么数?
一般采用的都是cross entropy loss value,定义如下:
发现就是网络预测给出的二类概率向量为[0.5,0.5],也就是a和1-a都是0.5,不管y取值0/1,整个的平均loss就是-ln(0.5)=0.69.
a的值是softmax的输出,在二分类的情况下为0.5,表明输入softmax的值x是(近似)相等的。
出现这个情况,往往是因为relu导致的大量神经元坏死,或者是tanh导致的梯度消失,可以尝试换一个损失函数(relu换成tanh,或者tanh换成relu),如果还是不行,可以尝试leak_relu。
2、激活函数relu与tanh的区别?分别适用于哪种情况?
我整理了tensorflow中的损失函数及其优缺点,可以看这个文章。
3、tf.nn.in_top_k(predictions, targets, k, name=None)
其中,prediction就是表示预测的结果,大小就是预测样本的数量乘以输出的维度,类型是tf.float32等。
target就是实际样本类别的标签,大小就是样本数量的个数。
k表示每个样本的预测结果的前K个最大的数里面是否含有target中的值。一般都是取1。
(返回一个bool类型的张量)。
4、损失函数维持在一定值上下跳动
如果在刚开始训练的时候损失值比较大,迭代几次后维持在一定值左右不变时,可以考虑将BATCH_SIZE降低一些,可以取得比较好的效果。
5、如何使用np.random.shuffle打乱二维数组
np.random.shuffle(train_sample[0])
np.random.shuffle(train_sample[1])
使用上述两条语句可以分别打乱train_sample的第一行和第二行;
例如[1 2 3 4;1 2 3 4]可以被打乱为[2 1 3 4;4 1 3 2]。但是如果想要将两行按照同样的顺序打乱怎么办呢?
state = np.random.get_state()
np.random.shuffle(train_sample[0])
np.random.set_state(state)
np.random.shuffle(train_sample[1])
通过get_state()保存状态,set_state()重新载入状态,可以使得train_sample[0],train_sample[1]能够在保证对应关系不变的情况下,完成随机打乱。
6、tf.contrib.layers函数
def convolution(inputs,
num_outputs,
kernel_size,
stride=1,
padding='SAME',
data_format=None,
rate=1,
activation_fn=nn.relu,
normalizer_fn=None,
normalizer_params=None,
weights_initializer=initializers.xavier_initializer(),
weights_regularizer=None,
biases_initializer=init_ops.zeros_initializer(),
biases_regularizer=None,
reuse=None,
variables_collections=None,
outputs_collections=None,
trainable=True,
scope=None):
常用的参数说明如下:
def max_pool2d(inputs,
kernel_size,
stride=2,
padding='VALID',
data_format=DATA_FORMAT_NHWC,
outputs_collections=None,
scope=None):
参数说明如下:
inputs: A 4-D tensor of shape `[batch_size, height, width, channels]` if`data_format` is `NHWC`, and `[batch_size, channels, height, width]` if `data_format` is `NCHW`.
kernel_size: A list of length 2: [kernel_height, kernel_width] of the pooling kernel over which the op is computed. Can be an int if both values are the same.
stride: A list of length 2: [stride_height, stride_width].Can be an int if both strides are the same. Note that presently both strides must have the same value.
padding: The padding method, either 'VALID' or 'SAME'.
data_format: A string. `NHWC` (default) and `NCHW` are supported.
outputs_collections: The collections to which the outputs are added.
scope: Optional scope for name_scope.