确切地说,我正在寻找的损失函数是当绝对误差小于0.5时的平方误差,并且当绝对误差大于0.5时它是绝对误差本身.这样,误差函数的梯度不超过1,因为一旦平方误差函数的梯度达到1,绝对误差函数就会启动,并且梯度保持恒定为1.我已经将我当前的实现包含在下面.出于某种原因,它给我的性能不仅仅是平方误差.
fn_choice_maker1 = (tf.to_int32(tf.sign(y - y_ + 0.5)) + 1)/2
fn_choice_maker2 = (tf.to_int32(tf.sign(y_ - y + 0.5)) + 1)/2
choice_maker_sqr = tf.to_float(tf.mul(fn_choice_maker1, fn_choice_maker2))
sqr_contrib = tf.mul(choice_maker_sqr, tf.square(y - y_))
abs_contrib = tf.abs(y - y_)-0.25 - tf.mul(choice_maker_sqr, tf.abs(y - y_)-0.25)
loss = tf.reduce_mean(sqr_contrib + abs_contrib)
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
Run Code Online (Sandbox Code Playgroud)
choice_maker_sqr是一个列张量,只要误差介于0.5和-0.5之间就是一个.这些名字非常自我解释.
我在tensorflow网站上构建了一个基于本教程的回归卷积神经网络.
当我一次评估多个图像的结果时,我得到的结果与我逐个评估每个图像的结果不同.
更具体地说,对于三个相同的样本图像,我得到[ 729027.5625 729027.5625 729027.5625]当我作为批处理[ 729026.4375] [ 729026.4375] [ 729026.4375]计算它们的输出时,但是当我逐个计算图像的输出时,我得到了.
知道为什么会这样吗?神经网络的输入定义如下:
x = tf.placeholder(tf.float32, shape=[None, 784])
Run Code Online (Sandbox Code Playgroud)
在进行批量评估时,我输入了一个2维的numpy图像阵列 (shape = (100, 784))
编辑:请参阅下面的MWE
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
import numpy as np
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], …Run Code Online (Sandbox Code Playgroud)