tf.contrib.learn.preprocessing.VocabularyProcessor()

tf.contrib.learn.preprocessing.VocabularyProcessor (max_document_length, min_frequency=0, vocabulary=None, tokenizer_fn=None)

参数:

max_document_length: 文档的最大长度。如果文本的长度大于最大长度,那么它会被剪切,反之则用0填充。 
min_frequency: 词频的最小值,出现次数小于最小词频则不会被收录到词表中。 
vocabulary: CategoricalVocabulary 对象。 
tokenizer_fn:分词函数



例子:

from tensorflow.contrib import learn
import numpy as np
max_document_length = 4
x_text =[
    ‘i love you‘,
    ‘me too‘
]
vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length)
vocab_processor.fit(x_text)
print next(vocab_processor.transform([‘i me too‘])).tolist()
x = np.array(list(vocab_processor.fit_transform(x_text)))
print x

运行结果为:

[1, 2, 3, 0]
[[1 4 5 0]
 [2 3 0 0]]

看一下词和索引的对应:

embedding_size = len(vocab_processor.vocabulary_)print embedding_sizevocab_dict = vocab_processor.vocabulary_._mapping
sorted_vocab = sorted(vocab_dict.items(), key = lambda x : x[1])
vocabulary = list(list(zip(*sorted_vocab))[0])
print vocabulary

结果是:

6[‘<UNK>‘, ‘i‘, ‘me‘, ‘too‘, ‘love‘, ‘you‘]

原文地址:https://www.cnblogs.com/helloworld0604/p/9002337.html

时间: 2024-08-30 01:33:47

tf.contrib.learn.preprocessing.VocabularyProcessor()的相关文章

学习笔记TF044:TF.Contrib组件、统计分布、Layer、性能分析器tfprof

TF.Contrib,开源社区贡献,新功能,内外部测试,根据反馈意见改进性能,改善API友好度,API稳定后,移到TensorFlow核心模块.生产代码,以最新官方教程和API指南参考. 统计分布.TF.contrib.ditributions模块,Bernoulli.Beta.Binomial.Gamma.Ecponential.Normal.Poisson.Uniform等统计分布,统计研究.应用中常用,各种统计.机器学习模型基石,概率模型.图形模型依赖. 每个不同统计分布不同特征.函数,同

tf.contrib.slim的介绍

本文主要参考博客:博客连接 前言基础: 验证本地的tf.contrib.slim模块是否有效: 1 python -c "import tensorflow.contrib.slim as slim;eval=slim.evaluation.evaluate_once" 下载models模块: 下载连接.下载后解压到你设定的文件夹,笔者解压到"E:\TENSORFLOW\models" 找到并且打开文件夹"E:\TENSORFLOW\models\rese

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/78238807 MachineLP的Github(欢迎follow):https://github.com/MachineLP 我的GitHub:https://github.com/MachineLP/train_cnn-rnn-attention 自己搭建的一个框架,包含模型有:vgg(vgg16,vg

关于tensorflow里面的tf.contrib.rnn.BasicLSTMCell 中num_units参数问题

这里的num_units参数并不是指这一层油多少个相互独立的时序lstm,而是lstm单元内部的几个门的参数,这几个门其实内部是一个神经网络,答案来自知乎: class TRNNConfig(object): """RNN配置参数""" # 模型参数 embedding_dim = 100 # 词向量维度 seq_length = 100 # 序列长度 num_classes = 2 # 类别数 vocab_size = 10000 # 词汇表达

深度学习原理与框架-递归神经网络-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)  # 构建单层的LSTM网络 参数说明:num_hidden表示隐藏层的个数,reuse表示LSTM的参数进行复用 2.rnn.DropoutWrapper(cell, output_keep_prob=keep_prob) # 表示对rnn的输出层进行dropout 参数说明:cell表示单层的lstm,o

TensorFlow中的L2正则化函数:tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()的用法与异同

tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()都是TensorFlow中的L2正则化函数,tf.contrib.layers.l2_regularizerd()函数在tf 2.x版本中被弃用了. 两者都能用来L2正则化处理,但运算有一点不同. import tensorflow as tf sess = InteractiveSession() a = tf.constant([1, 2, 3], dtype=tf.float32) b =

tf.contrib.slim.arg_scope 完整

缘由 最近一直在看深度学习的代码,又一次看到了slim.arg_scope()的嵌套使用,具体代码如下: with slim.arg_scope( [slim.conv2d, slim.separable_conv2d], weights_initializer=tf.truncated_normal_initializer( stddev=weights_initializer_stddev), activation_fn=activation_fn, normalizer_fn=slim.b

tf.contrib.slim.data数据加载 综述

TF-Slim为了方便加载各种数据类型(如TFRocords或者文本文件)的数据,创建了这个库. Dataset 这里的数据库与通常意义下数据库是不同的,这里数据库是python一个类,它负责将原始数据通过流水线加工成为我们需要的数据格式. TF-Slim defines a dataset to be a set of files (that may or may not be encoded) representing a finite set of samples, and which c

tensorflow VocabularyProcessor

from tensorflow.contrib import learn import numpy as np vocab_process = learn.preprocessing.VocabularyProcessor(max_document_length=8) # 定义sentence固定长度 vocab_process.fit(w2v_vocab) vocab_process.transform( np.reshape(' '.join(text1_words), [-1])) # t