笔记 - tensorflow用法:实现交叉熵公式

import tensorflow as tf

"""
给出n个样本的预测值与真实值进行计算交叉熵(label已进行热编码)
即两个矩阵

交叉熵公式倒背如流
"""
y = tf.constant([[0, 0, 1], [1, 0, 0]], dtype=tf.float32)
y_pred = tf.random_uniform(shape=(2, 3), minval=0, maxval=1)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_pred), axis=1), axis=0)

with tf.Session() as sess:
    print(cross_entropy.eval())

猜你喜欢

转载自blog.csdn.net/chen_holy/article/details/90105346