sequence_loss是nlp算法中非常重要的一个函数.rnn,lstm,attention都要用到这个函数.看下面代码:

# coding: utf-8
import numpy as np
import tensorflow as tf
from tensorflow.contrib.seq2seq import sequence_loss logits_np = np.array([
[[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5]],
[[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5]]
])
targets_np = np.array([
[0, 0, 0],
[0, 0, 0]
], dtype=np.int32) logits = tf.convert_to_tensor(logits_np)
targets = tf.convert_to_tensor(targets_np)
cost = sequence_loss(logits=logits,
targets=targets,
weights=tf.ones_like(targets, dtype=tf.float64))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
r = sess.run(cost)
print(r)

先对每个[0.5,0.5,0.5,0.5]取softmax. softmax([0.5,0.5,0.5,0.5])=(0.25,0.25,0.25,0.25)然后再计算-ln(0.25)*6/6=1.38629436112.

再看一个例子

# coding:utf-8
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division from tensorflow.contrib.seq2seq import sequence_loss import tensorflow as tf
import numpy as np output_np = np.array(
[
[[0.6, 0.5, 0.3, 0.2], [0.9, 0.5, 0.3, 0.2], [1.0, 0.5, 0.3, 0.2]],
[[0.2, 0.5, 0.3, 0.2], [0.3, 0.5, 0.3, 0.2], [0.4, 0.5, 0.3, 0.2]]
]
)
print(output_np.shape)
target_np = np.array([[0, 1, 2],
[3, 0, 1]],
dtype=np.int32)
print(target_np.shape)
output = tf.convert_to_tensor(output_np, np.float32)
target = tf.convert_to_tensor(target_np, np.int32) cost = sequence_loss(output,
target,
tf.ones_like(target, dtype=np.float32)) init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
cost_r = sess.run(cost)
print(cost_r)

这个代码作用和下面的tf.reduce_mean(softmax_cross_entropy_with_logits)作用一致.

# coding:utf-8
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division import tensorflow as tf
import numpy as np def to_onehot(a):
max_index = np.max(a)
b = np.zeros((a.shape[0], max_index + 1))
b[np.arange(a.shape[0]), a] = 1
return b logits_ph = tf.placeholder(tf.float32, shape=(None, None))
labels_ph = tf.placeholder(tf.float32, shape=(None, None))
output_np = np.array([
[0.6, 0.5, 0.3, 0.2],
[0.9, 0.5, 0.3, 0.2],
[1.0, 0.5, 0.3, 0.2],
[0.2, 0.5, 0.3, 0.2],
[0.3, 0.5, 0.3, 0.2],
[0.4, 0.5, 0.3, 0.2]]) cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=labels_ph, logits=logits_ph))
target_np = np.array([0, 1, 2, 3, 0, 1])
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
cost_r = sess.run(cost, feed_dict={logits_ph: output_np, labels_ph: to_onehot(target_np)})
print(cost_r)

再取交叉熵,再取平均.

最新文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(63)-Excel导入和导出-自定义表模导入
  2. 被我们忽略的HttpSession线程安全问题
  3. SubSonic3.0插件分页查询速度测试
  4. Life Cycle of Thread – Understanding Thread States in Java
  5. VS2012编译的Windows服务启动后立即停止的解决方案
  6. Android之滑屏动画和自定义控件
  7. jquery上传插件Jquery.uploadify.js-转
  8. 多校6 1003 HDU5795 A Simple Nim (sg函数)
  9. CUDA从入门到精通
  10. 【转】使用GDB调试Coredump文件
  11. android项目在eclipse下编译运行的问题
  12. Jquery Datatables 动态列名
  13. 【Spring】详解Spring中Bean的加载
  14. 【T09】要认识到TCP是一个可靠的,但不是绝对可靠的协议
  15. vue,在模块中动态添加dom节点,并监听
  16. pbft流程深层分析和解释(转)
  17. P1002 谁拿了最多奖学金
  18. python 取整
  19. PCB布线经验
  20. 【LOJ】#2384. 「HNOI2013」切糕

热门文章

  1. 手把手教你实现热更新功能,带你了解 Arthas 热更新背后的原理
  2. 手把手教你用netty撸一个ZkClient
  3. 解决vuex的数据刷新(F5)后会被初始化的问题
  4. Java开发者入职必备条件
  5. Docker从入门到实践(2)
  6. Ubuntu 16.04 安装Maven3.3.9
  7. static declaration follows non-static declaration
  8. AsyncDisplayKit编译和使用注意事项
  9. php如何处理大数据高并发
  10. Beta阶段贡献分配