tf.contrib.seq2seq.sequence_loss example:seqence loss 实例代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
params=np.random.normal(loc=0.0,scale=1.0,size=[10,10])
encoder_inputs=tf.placeholder(dtype=tf.int32,shape=[10,10])
decoder_inputs=tf.placeholder(dtype=tf.int32,shape=[10,10])
logits=tf.placeholder(dtype=tf.float32,shape=[10,10,10])
targets=tf.placeholder(dtype=tf.int32,shape=[10,10])
weights=tf.placeholder(dtype=tf.float32,shape=[10,10])
train_encoder_inputs=np.ones(shape=[10,10],dtype=np.int32)
train_decoder_inputs=np.ones(shape=[10,10],dtype=np.int32)
train_weights=np.ones(shape=[10,10],dtype=np.float32)
num_encoder_symbols=10
num_decoder_symbols=10
embedding_size=10
cell=tf.nn.rnn_cell.BasicLSTMCell(10)
def seq2seq(encoder_inputs,decoder_inputs,cell,num_encoder_symbols,num_decoder_symbols,embedding_size):
encoder_inputs = tf.unstack(encoder_inputs, axis=0)
decoder_inputs = tf.unstack(decoder_inputs, axis=0)
results,states=tf.contrib.legacy_seq2seq.embedding_rnn_seq2seq(
encoder_inputs,
decoder_inputs,
cell,
num_encoder_symbols,
num_decoder_symbols,
embedding_size,
output_projection=None,
feed_previous=False,
dtype=None,
scope=None
)
return results
def get_loss(logits,targets,weights):
loss=tf.contrib.seq2seq.sequence_loss(
logits,
targets=targets,
weights=weights
)
return loss
results=seq2seq(encoder_inputs,decoder_inputs,cell,num_encoder_symbols,num_decoder_symbols,embedding_size)
logits=tf.stack(results,axis=0)
print(logits)
loss=get_loss(logits,targets,weights)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
results_value=sess.run(results,feed_dict={encoder_inputs:train_encoder_inputs,decoder_inputs:train_decoder_inputs})
print(type(results_value[0]))
print(len(results_value))
cost = sess.run(loss, feed_dict={encoder_inputs: train_encoder_inputs, targets: train_decoder_inputs,
weights:train_weights,decoder_inputs:train_decoder_inputs})
print(cost)
更多教程:http://www.tensorflownews.com/
tf.contrib.seq2seq.sequence_loss example:seqence loss 实例代码的更多相关文章
- 学习笔记TF044:TF.Contrib组件、统计分布、Layer、性能分析器tfprof
TF.Contrib,开源社区贡献,新功能,内外部测试,根据反馈意见改进性能,改善API友好度,API稳定后,移到TensorFlow核心模块.生产代码,以最新官方教程和API指南参考. 统计分布.T ...
- 深度学习原理与框架-递归神经网络-RNN网络基本框架(代码?) 1.rnn.LSTMCell(生成单层LSTM) 2.rnn.DropoutWrapper(对rnn进行dropout操作) 3.tf.contrib.rnn.MultiRNNCell(堆叠多层LSTM) 4.mlstm_cell.zero_state(state初始化) 5.mlstm_cell(进行LSTM求解)
问题:LSTM的输出值output和state是否是一样的 1. rnn.LSTMCell(num_hidden, reuse=tf.get_variable_scope().reuse) # 构建 ...
- tensorflow笔记3:CRF函数:tf.contrib.crf.crf_log_likelihood()
在分析训练代码的时候,遇到了,tf.contrib.crf.crf_log_likelihood,这个函数,于是想简单理解下: 函数的目的:使用crf 来计算损失,里面用到的优化方法是:最大似然估计 ...
- TensorFlow高级API(tf.contrib.learn)及可视化工具TensorBoard的使用
一.TensorFlow高层次机器学习API (tf.contrib.learn) 1.tf.contrib.learn.datasets.base.load_csv_with_header 加载cs ...
- 关于tensorflow里面的tf.contrib.rnn.BasicLSTMCell 中num_units参数问题
这里的num_units参数并不是指这一层油多少个相互独立的时序lstm,而是lstm单元内部的几个门的参数,这几个门其实内部是一个神经网络,答案来自知乎: class TRNNConfig(obje ...
- 第十六节,使用函数封装库tf.contrib.layers
这一节,介绍TensorFlow中的一个封装好的高级库,里面有前面讲过的很多函数的高级封装,使用这个高级库来开发程序将会提高效率. 我们改写第十三节的程序,卷积函数我们使用tf.contrib.lay ...
- tf.contrib.rnn.core_rnn_cell.BasicLSTMCell should be replaced by tf.contrib.rnn.BasicLSTMCell.
For Tensorflow 1.2 and Keras 2.0, the line tf.contrib.rnn.core_rnn_cell.BasicLSTMCell should be repl ...
- tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别
tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别 https://blog.csdn.net/u014365862/article/details/78238 ...
- TensorFlow高层次机器学习API (tf.contrib.learn)
TensorFlow高层次机器学习API (tf.contrib.learn) 1.tf.contrib.learn.datasets.base.load_csv_with_header 加载csv格 ...
随机推荐
- thinkphp中的常见静态常亮
thinkphp __PUBLIC__的定义 __ROOT__等常量的定义 1 2 3 4 5 6 7 8 9 '__TMPL__' => APP_TMPL_PATH, // 项目 ...
- LeetCode & Q20-Valid Parentheses-Easy
Stack String Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ...
- 浅谈移动端适配-rem
对于移动端开发来说,无可避免的就是直面各种设备不同分辨率和不同DPR(设备像素比)的问题,在此忽略其他兼容性问题的探讨. 一. 移动端开发有关于像素的概念: 1.设备像素(dp),也叫物理像素.指设备 ...
- poj 2945 Find the Clones
https://vjudge.net/problem/POJ-2945 题意: 给出n个长度相同的DNA序列,如果一个DNA序列出现过两次,那么就有说明它被复制了一次.问被复制0次,1次,2次--n- ...
- Hibernate(十五):QBC检索、本地SQL检索和HQL删除
QBC检索 QBC查询就是通过使用Hibernate提供的Query By Criteria API来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口. 1)通过Cr ...
- Python将excel文件从xls转换为xlsx
本文使用场景:将一个xls格式Excel文件转换为xlsx文件格式.接下来将一步一步演示该操作.你也可以对代码进行修改使其适用于你所需的场景. 安装Python3 首先需要安装Python,我这里安装 ...
- nginx location的命中过程
1 先判断精准命中,立即返回结果并结束解析过程 2 判断普通命中,如果有多个命中,"记录"下"最长"的命中结果(注意:记录但不结束,最长的为准) 3 继续判断正 ...
- C# WinForm 富文本编辑器 用kindeditor实现本地图片只选取不上传到编辑器
以下资料有参考网上其它童鞋作品,原作者看到务喷!!!! 以下资料有参考网上其它童鞋作品,原作者看到务喷!!!! 重要的事只要说两遍... 网上找了几天关于WinForm富文本编辑效果都不理想,各种坑, ...
- pyqt5 动画学习(一) 改变控件大小
入坑pyqt也有半年了,由于人们对事物的审美,静态界面已经不能满足用户,而动画却给人眼前一亮,so,今天来学习pyqt的动画了 由于资料真的是太少,本人也是有啃外国佬的英文进行摸索学习,可能也是触及皮 ...
- hive中No space left on device问题或者Java HotSpot(TM)64-Bit Server VM warning:Insufficient space for shared memory