我正在研究基于Tensorflow的成本敏感神经网络 . But because of the static graph structure of Tensorflow. Some NN structure couldn't be realized by myself.
我的损失函数(成本),成本矩阵和计算进度描述如下,我的目标是计算总成本,然后优化NN:
近似计算进度:
-
y_
是CNN的最后一个完整连接输出,其形状为(1024,5)
-
y
是一个Tensor,它有形状(1024)并表示x[i]
的基本事实 -
y_soft[i] [j]
表示x[i]
成为类j
的概率
我怎样才能在Tensorflow中实现这一点?
1 回答
cost_matrix:
标签:
Y *:
Y(预测):
标签,cost_matrix - > cost_embedding:
显然[0.2,0.3,0.5]中的0.3表示[0,1,0]的正确标准,因此它不应该归于损失 .
0.7英寸[0.1,0.2,0.7]是相同的 . 换句话说,y *中值为1的pos不会导致丢失 .
所以我有(1-y *):
然后熵是目标* log(预测)(1目标)* log(1预测),y 中的值0,应该使用(1-target) log(1-predict),所以我使用(1 -predict)说(1-y)
1-Y:
(斜体数字无用)
自定义损失是
你可以看到(1-y *)可以放在这里
所以损失是-tf.reduce_mean(cost_embedding * log(1-y)),为使它适用,应该是:
演示如下