前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >tensorflow 0.10 word2vec 源码解析

tensorflow 0.10 word2vec 源码解析

作者头像
ke1th
发布于 2019-05-27 04:19:02
发布于 2019-05-27 04:19:02
84100
代码可运行
举报
运行总次数:0
代码可运行

版权声明:本文为博主原创文章,转载请注明出处。 https://cloud.tencent.com/developer/article/1436566

关于word2vec 的解释见word2vec的数学原理

本代码主要是实现了skip-gram模型,通过神经网络,对概率进行建模(概率模型中的最大似然,其实就是神经网络中的最小损失)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections
import math
import os
import random
import zipfile
import numpy as np    #在引用包方面,对源码进行了一些修改,因为源码应该是基于python3的 
import urllib         #而我使用的python2
import tensorflow as tf

# Step 1: Download the data.
url = 'http://mattmahoney.net/dc/'

def maybe_download(filename, expected_bytes):
  """Download a file if not present, and make sure it's the right size."""
  if not os.path.exists(filename):
    filename, _ = urllib.urlretrieve(url + filename, filename)
  statinfo = os.stat(filename)
  if statinfo.st_size == expected_bytes:
    print('Found and verified', filename)
  else:
    print(statinfo.st_size)
    raise Exception(
        'Failed to verify ' + filename + '. Can you get to it with a browser?')
  return filename

filename = maybe_download('text8.zip', 31344016)


# Read the data into a list of strings.
def read_data(filename):
  """Extract the first file enclosed in a zip file as a list of words"""
  with zipfile.ZipFile(filename) as f:
    data = tf.compat.as_str(f.read(f.namelist()[0])).split()
  return data

words = read_data(filename)
print('Data size', len(words))

# Step 2: Build the dictionary and replace rare words with UNK token.
vocabulary_size = 50000

def build_dataset(words): #words in corpus , list
  count = [['UNK', -1]]
  count.extend(collections.Counter(words).most_common(vocabulary_size - 1)) 
  # 因为使用了most_common,所以count中的word,是按照word在文本中出现的次数从大到小排列的
  dictionary = dict()
  for word, _ in count:
    dictionary[word] = len(dictionary)   # assign id to word
  data = list()
  unk_count = 0
  for word in words:
    if word in dictionary:
      index = dictionary[word]
    else:
      index = 0  # dictionary['UNK'] UNK:unknown
      unk_count += 1
    data.append(index) #translate word to id
  count[0][1] = unk_count
  reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys()))
  return data, count, dictionary, reverse_dictionary #data:ids count:list of [word,num] dictionary:word->id ,

data, count, dictionary, reverse_dictionary = build_dataset(words)
del words  # Hint to reduce memory.
print('Most common words (+UNK)', count[:5])
print('Sample data', data[:10], [reverse_dictionary[i] for i in data[:10]])

data_index = 0


# Step 3: Function to generate a training batch for the skip-gram model.
#skip_skip:从span里面取出多少个word, skip_window:|contex(w)| / 2
#span: w上下文的word, 只能从span这个范围中获取
def generate_batch(batch_size, num_skips, skip_window):
  global data_index
  assert batch_size % num_skips == 0
  assert num_skips <= 2 * skip_window   # num_skip? skip_window? skip-gram
  batch = np.ndarray(shape=(batch_size), dtype=np.int32) #batch_size: the number of words in one batch
  labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32)
  span = 2 * skip_window + 1 # [ skip_window target skip_window ]
  buffer = collections.deque(maxlen=span) #buffer用来存取w上下文word的id
  for _ in range(span):
    buffer.append(data[data_index]) # data:ids
    data_index = (data_index + 1) % len(data)
  for i in range(batch_size // num_skips): #how many num_skips in a batch
    target = skip_window  # target label at the center of the buffer
    targets_to_avoid = [ skip_window ] # extract the middle word
    for j in range(num_skips):
      while target in targets_to_avoid:#context中的word,一个只取一次
        target = random.randint(0, span - 1)
      targets_to_avoid.append(target) #
      batch[i * num_skips + j] = buffer[skip_window]
      labels[i * num_skips + j, 0] = buffer[target]
    buffer.append(data[data_index]) #update the buffer, append the next word to buffer
    data_index = (data_index + 1) % len(data)
  return batch, labels #batch: ids [batch_size] lebels:ids [batch_size*1]

batch, labels = generate_batch(batch_size=8, num_skips=2, skip_window=1)
for i in range(8):
  print(batch[i], reverse_dictionary[batch[i]],
      '->', labels[i, 0], reverse_dictionary[labels[i, 0]])
#========================================================
#上面的操作会形成一个这样的输出 batch中存储的是id, 假设我们去skip_size = 4, skip_window = 2
#那么,单词 as 所对应的context的word个数就是4个,所以batch中有4as, 所对应的就是context中的word
# 12 as -> 195 term
# 12 as -> 5239 anarchism
# 12 as -> 6 a
# 12 as -> 3084 originated
# 6 a -> 12 as
# 6 a -> 3084 originated
# 6 a -> 2 of
# 6 a -> 195 term
#=======================================================
# Step 4: Build and train a skip-gram model.

#在本代码中,batch_size代表的是一个batch中,word的个数,而不是sentense的个数。
batch_size = 128
embedding_size = 128  # Dimension of the embedding vector.
skip_window = 1       # How many words to consider left and right.
num_skips = 2         # How many times to reuse an input(buffer) to generate a label.

# We pick a random validation set to sample nearest neighbors. Here we limit the
# validation samples to the words that have a low numeric ID, which by
# construction are also the most frequent.
valid_size = 16     # Random set of words to evaluate similarity on.
valid_window = 100  # Only pick dev samples in the head of the distribution.
valid_examples = np.random.choice(valid_window, valid_size, replace=False)
num_sampled = 64    # Number of negative examples to sample.

graph = tf.Graph()

with graph.as_default():

  # Input data.
  #在这里,我们只输入word对应的id,假设batch_size是128,那么我们第一次就输入文本前128个word所对应的id
  train_inputs = tf.placeholder(tf.int32, shape=[batch_size])
  #labels和inputs是一样的, 只不过一个是行向量(tensor),一个是列向量(tensor)
  train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])
  valid_dataset = tf.constant(valid_examples, dtype=tf.int32)

  # Ops and variables pinned to the CPU because of missing GPU implementation
  with tf.device('/cpu:0'):
    # Look up embeddings for inputs.
    embeddings = tf.Variable(
        tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
    # embedding_look 返回值的shape 是 shape(train_inputs)+shape(embeddings)[1:]
    embed = tf.nn.embedding_lookup(embeddings, train_inputs)

    # Construct the variables for the NCE loss
    nce_weights = tf.Variable(   #every word has a corresponding nce_weight ad nce_biase
        tf.truncated_normal([vocabulary_size, embedding_size],
                            stddev=1.0 / math.sqrt(embedding_size)))
    nce_biases = tf.Variable(tf.zeros([vocabulary_size]))

word2vec的数学原理中的skip-gram模型解释中,作者提到了一个公式

σ(xTwθu+bu)σ(xwTθu+bu)\sigma(x_w^T\theta^u+b^u) 其中 xTw_xwTx_w^T 是输入的word_vector ,θu_θu\theta^u 和 _bu_bub^u 就是上面的nce_weights nce_bias 其中nce_weightsu_id nce_biasu_id 对应与单词u

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  # Compute the average NCE loss for the batch.
  # tf.nce_loss automatically draws a new sample of the negative labels each
  # time we evaluate the loss.
  loss = tf.reduce_mean(
      tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,
                     num_sampled, vocabulary_size))#关于nce_loss的介绍在文章最后

  # Construct the SGD optimizer using a learning rate of 1.0.
  optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss)

  # Compute the cosine similarity between minibatch examples and all embeddings.
  norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))
  normalized_embeddings = embeddings / norm
  valid_embeddings = tf.nn.embedding_lookup(
      normalized_embeddings, valid_dataset)
  similarity = tf.matmul(
      valid_embeddings, normalized_embeddings, transpose_b=True)

  # Add variable initializer.
  init = tf.initialize_all_variables()

# Step 5: Begin training.
num_steps = 10001

with tf.Session(graph=graph) as session:
  # We must initialize all variables before we use them.
  init.run()
  print("Initialized")

  average_loss = 0
  for step in xrange(num_steps):
    batch_inputs, batch_labels = generate_batch(
        batch_size, num_skips, skip_window)
    feed_dict = {train_inputs : batch_inputs, train_labels : batch_labels}

    # We perform one update step by evaluating the optimizer op (including it
    # in the list of returned values for session.run()
    _, loss_val = session.run([optimizer, loss], feed_dict=feed_dict)
    average_loss += loss_val

    if step % 2000 == 0:
      if step > 0:
        average_loss /= 2000
      # The average loss is an estimate of the loss over the last 2000 batches.
      print("Average loss at step ", step, ": ", average_loss)
      average_loss = 0

    # Note that this is expensive (~20% slowdown if computed every 500 steps)
    if step % 10000 == 0:
      sim = similarity.eval()
      for i in xrange(valid_size):
        valid_word = reverse_dictionary[valid_examples[i]]
        top_k = 8 # number of nearest neighbors
        nearest = (-sim[i, :]).argsort()[1:top_k+1]
        log_str = "Nearest to %s:" % valid_word
        for k in xrange(top_k):
          close_word = reverse_dictionary[nearest[k]]
          log_str = "%s %s," % (log_str, close_word)
        print(log_str)
  final_embeddings = normalized_embeddings.eval()

# Step 6: Visualize the embeddings.

def plot_with_labels(low_dim_embs, labels, filename='tsne.png'):
  assert low_dim_embs.shape[0] >= len(labels), "More labels than embeddings"
  plt.figure(figsize=(18, 18))  #in inches
  for i, label in enumerate(labels):
    x, y = low_dim_embs[i,:]
    plt.scatter(x, y)
    plt.annotate(label,
                 xy=(x, y),
                 xytext=(5, 2),
                 textcoords='offset points',
                 ha='right',
                 va='bottom')

  plt.savefig(filename)

try:
  from sklearn.manifold import TSNE
  import matplotlib.pyplot as plt

  tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
  plot_only = 500
  low_dim_embs = tsne.fit_transform(final_embeddings[:plot_only,:])
  labels = [reverse_dictionary[i] for i in xrange(plot_only)]
  plot_with_labels(low_dim_embs, labels)

except ImportError:
  print("Please install sklearn, matplotlib, and scipy to visualize embeddings.")

细说nce_loss

nce_loss函数提供了,取negtive sample,计算loss一条龙服务,相当方便。源码中的注释是这么写的

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def nce_loss(weights, #[num_classes, dim] dim就是emdedding_size
             biases,  #[num_classes] num_classes就是word的个数(不包括重复的)
             inputs, #[batch_size, dim]
             labels,  #[batch_size, num_true] 这里,我们的num_true设置为1,就是一个输入对应一个输出
             num_sampled,#要取的负样本的个数(per batch)
             num_classes,#类别的个数(在这里就是word的个数(不包含重复的))
             num_true=1,
             sampled_values=None,
             remove_accidental_hits=False,
             partition_strategy="mod",
             name="nce_loss"):
      logits, labels = _compute_sampled_logits(
      weights,
      biases,
      inputs,
      labels,
      num_sampled,
      num_classes,
      num_true=num_true,
      sampled_values=sampled_values,
      subtract_log_q=True,
      remove_accidental_hits=remove_accidental_hits,
      partition_strategy=partition_strategy,
      name=name)
  sampled_losses = sigmoid_cross_entropy_with_logits(
      logits, labels, name="sampled_losses") 
      #此函数返回的tensor与输入logits同维度。 _sum_rows之后,就得到了每个样本的corss entropy。
  # sampled_losses is batch_size x {true_loss, sampled_losses...}
  # We sum out true and sampled losses.
  return _sum_rows(sampled_losses)
  #在word2vec中对此函数的返回调用了reduce_mean() 就获得了平均 cross entropy

nce_loss调用了_compute_sammpled_logits, 这个函数是搞什么的呢?

看源码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 def _compute_sampled_logits(weights,
                            biases,
                            inputs,
                            labels,
                            num_sampled,
                            num_classes,
                            num_true=1,
                            sampled_values=None,
                            subtract_log_q=True,
                            remove_accidental_hits=False,
                            partition_strategy="mod",
                            name=None):
  if not isinstance(weights, list):
    weights = [weights]

  with ops.op_scope(weights + [biases, inputs, labels], name,
                    "compute_sampled_logits"):
    if labels.dtype != dtypes.int64:
      labels = math_ops.cast(labels, dtypes.int64)
    labels_flat = array_ops.reshape(labels, [-1])

    # Sample the negative labels.
    #   sampled shape: [num_sampled] tensor
    #   true_expected_count shape = [batch_size, 1] tensor
    #   sampled_expected_count shape = [num_sampled] tensor
    if sampled_values is None:
      sampled_values = candidate_sampling_ops.log_uniform_candidate_sampler(
          true_classes=labels,
          num_true=num_true,
          num_sampled=num_sampled,
          unique=True,
          range_max=num_classes)

NOTE:这个函数是通过log-uniform进行取样的P(class)=(log(class+2)−log(class+1))/log(rangmax+1)P(class)=(log⁡(class+2)−log⁡(class+1))/log⁡(rangmax+1)P(class)=(\log(class+2)-\log(class+1))/\log(rang_max+1),取样范围是0, range_max ,用这种方法取样就要求我们的word是按照频率从高到低排列的。之前对word的处理的确是这样

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    # NOTE: pylint cannot tell that 'sampled_values' is a sequence
    # pylint: disable=unpacking-non-sequence
    sampled, true_expected_count, sampled_expected_count = sampled_values
    # pylint: enable=unpacking-non-sequence

    # labels_flat is a [batch_size * num_true] tensor
    # sampled is a [num_sampled] int tensor
    all_ids = array_ops.concat(0, [labels_flat, sampled])

    # weights shape is [num_classes, dim]
    all_w = embedding_ops.embedding_lookup(
        weights, all_ids, partition_strategy=partition_strategy)
    all_b = embedding_ops.embedding_lookup(biases, all_ids)
    # true_w shape is [batch_size * num_true, dim]
    # true_b is a [batch_size * num_true] tensor
    true_w = array_ops.slice(
        all_w, [0, 0], array_ops.pack([array_ops.shape(labels_flat)[0], -1]))
    true_b = array_ops.slice(all_b, [0], array_ops.shape(labels_flat))

    # inputs shape is [batch_size, dim]
    # true_w shape is [batch_size * num_true, dim]
    # row_wise_dots is [batch_size, num_true, dim]
    dim = array_ops.shape(true_w)[1:2]
    new_true_w_shape = array_ops.concat(0, [[-1, num_true], dim])
    row_wise_dots = math_ops.mul(
        array_ops.expand_dims(inputs, 1),
        array_ops.reshape(true_w, new_true_w_shape))
    # We want the row-wise dot plus biases which yields a
    # [batch_size, num_true] tensor of true_logits.
    dots_as_matrix = array_ops.reshape(row_wise_dots,
                                       array_ops.concat(0, [[-1], dim]))
    true_logits = array_ops.reshape(_sum_rows(dots_as_matrix), [-1, num_true])
    true_b = array_ops.reshape(true_b, [-1, num_true])
    true_logits += true_b

    # Lookup weights and biases for sampled labels.
    #   sampled_w shape is [num_sampled, dim]
    #   sampled_b is a [num_sampled] float tensor
    sampled_w = array_ops.slice(
        all_w, array_ops.pack([array_ops.shape(labels_flat)[0], 0]), [-1, -1])
    sampled_b = array_ops.slice(all_b, array_ops.shape(labels_flat), [-1])

    # inputs has shape [batch_size, dim]
    # sampled_w has shape [num_sampled, dim]
    # sampled_b has shape [num_sampled]
    # Apply X*W'+B, which yields [batch_size, num_sampled]
    sampled_logits = math_ops.matmul(
        inputs, sampled_w, transpose_b=True) + sampled_b

    if remove_accidental_hits:
      acc_hits = candidate_sampling_ops.compute_accidental_hits(
          labels, sampled, num_true=num_true)
      acc_indices, acc_ids, acc_weights = acc_hits

      # This is how SparseToDense expects the indices.
      acc_indices_2d = array_ops.reshape(acc_indices, [-1, 1])
      acc_ids_2d_int32 = array_ops.reshape(
          math_ops.cast(acc_ids, dtypes.int32), [-1, 1])
      sparse_indices = array_ops.concat(1, [acc_indices_2d, acc_ids_2d_int32],
                                        "sparse_indices")
      # Create sampled_logits_shape = [batch_size, num_sampled]
      sampled_logits_shape = array_ops.concat(
          0,
          [array_ops.shape(labels)[:1], array_ops.expand_dims(num_sampled, 0)])
      if sampled_logits.dtype != acc_weights.dtype:
        acc_weights = math_ops.cast(acc_weights, sampled_logits.dtype)
      sampled_logits += sparse_ops.sparse_to_dense(
          sparse_indices,
          sampled_logits_shape,
          acc_weights,
          default_value=0.0,
          validate_indices=False)

    if subtract_log_q:
      # Subtract log of Q(l), prior probability that l appears in sampled.
      true_logits -= math_ops.log(true_expected_count)
      sampled_logits -= math_ops.log(sampled_expected_count)

    # Construct output logits and labels. The true labels/logits start at col 0.
    out_logits = array_ops.concat(1, [true_logits, sampled_logits])
    # true_logits is a float tensor, ones_like(true_logits) is a float tensor
    # of ones. We then divide by num_true to ensure the per-example labels sum
    # to 1.0, i.e. form a proper probability distribution.
    out_labels = array_ops.concat(1,
                                  [array_ops.ones_like(true_logits) / num_true,
                                   array_ops.zeros_like(sampled_logits)])

  return out_logits, out_labels
  """
  代码很长,但是我们主要关心的是返回的out_logits和out_labels是什么样子的,想搞清楚到底是怎么样用神经网络对概率分布进行建模。
  """

最终输出的logits是这样的,假设(uicontext(w)ui∈context(w)u_i \in context(w), nicontext(w)ni∉context(w)n_i \notin context(w)) batch_size, 1+num_sampled

|_xTwθu_0xwTθu0x_w^T\theta^{u_0}| _xTwθn_0xwTθn0x_w^T\theta^{n_0}|_xTwθn_1xwTθn1x_w^T\theta^{n_1}|_xTwθn_2xwTθn2x_w^T\theta^{n_2}|_xTwθn_3xwTθn3x_w^T\theta^{n_3}|…|

|_xTwθu_1xwTθu1x_w^T\theta^{u_1}| _xTwθn_0xwTθn0x_w^T\theta^{n_0}|_xTwθn_1xwTθn1x_w^T\theta^{n_1}|_xTwθn_2xwTθn2x_w^T\theta^{n_2}|_xTwθn_3xwTθn3x_w^T\theta^{n_3}|…|

|_xTwθu_2xwTθu2x_w^T\theta^{u_2}| _xTwθn_0xwTθn0x_w^T\theta^{n_0}|_xTwθn_1xwTθn1x_w^T\theta^{n_1}|_xTwθn_2xwTθn2x_w^T\theta^{n_2}|_xTwθn_3xwTθn3x_w^T\theta^{n_3}|…|

是输出的out_labels是这样的 batch_size, 1+num_sampled

|1 |0 |0 |0 |0 |…|

|1 |0 |0 |0 |0 |…|

|1 |0 |0 |0 |0 |…|

word2vec的数学原理中告诉我们的是,要求logits的最大似然。就相当与最小化out_labels与logits的损失函数。

之后将输出的logits和out_labels输入到sampled_losses = sigmoid_cross_entropy_with_logits(

代码语言:txt
AI代码解释
复制
   logits, labels, name=”sampled\_losses”)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def sigmoid_cross_entropy_with_logits(logits, targets, name=None):
  with ops.name_scope(name, "logistic_loss", [logits, targets]) as name:
    logits = ops.convert_to_tensor(logits, name="logits")
    targets = ops.convert_to_tensor(targets, name="targets")
    try:
      targets.get_shape().merge_with(logits.get_shape())
    except ValueError:
      raise ValueError("logits and targets must have the same shape (%s vs %s)"
                       % (logits.get_shape(), targets.get_shape()))

    # The logistic loss formula from above is
    #   x - x * z + log(1 + exp(-x))
    # For x < 0, a more numerically stable formula is
    #   -x * z + log(1 + exp(x))
    # Note that these two expressions can be combined into the following:
    #   max(x, 0) - x * z + log(1 + exp(-abs(x)))
    # To allow computing gradients at zero, we define custom versions of max and
    # abs functions.
    zeros = array_ops.zeros_like(logits, dtype=logits.dtype)
    cond = (logits >= zeros)
    relu_logits = math_ops.select(cond, logits, zeros)
    neg_abs_logits = math_ops.select(cond, -logits, logits)
    return math_ops.add(relu_logits - logits * targets,
                        math_ops.log(1 + math_ops.exp(neg_abs_logits)),
                        name=name)

此函数返回的tensor与输入logits同维度。 _sum_rows之后,就得到了每个样本的corss entropy。

水平有限,如有问题,请指正

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年10月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Tensorflow实现word2vec
摘要总结:本文研究了如何通过使用技术社区中的内容编辑人员来提高内容质量,并总结了相关的方法和实践。
GavinZhou
2018/01/02
1.4K0
Tensorflow实现word2vec
Tensorflow 的 NCE-Loss 的实现和 word2vec
可以看到,它这里并没有传sampled_values,那么它的负样本是怎么得到的呢?继续看nce_loss的实现,可以看到里面处理sampled_values=None的代码如下:
阿泽 Crz
2021/01/11
1K0
Word2vec原理浅析及tensorflow实现
词向量的重要意义在于将自然语言转换成了计算机能够理解的向量。相对于词袋模型、TF-IDF等模型,词向量能抓住词的上下文、语义,衡量词与词的相似性,在文本分类、情感分析等许多自然语言处理领域有重要作用。
用户1332428
2018/07/30
6390
Word2vec原理浅析及tensorflow实现
TensorFlow2.0 代码实战专栏(四):Word2Vec (Word Embedding)
使用TensorFlow 2.0实现Word2Vec算法计算单词的向量表示,这个例子是使用一小部分维基百科文章来训练的。
磐创AI
2019/12/05
3.7K0
【NLP实战】tensorflow词向量训练实战
实战是学习一门技术最好的方式,也是深入了解一门技术唯一的方式。因此,NLP专栏计划推出一个实战专栏,让有兴趣的同学在看文章之余也可以自己动手试一试。
用户1508658
2019/10/14
1.1K0
【NLP实战】tensorflow词向量训练实战
从锅炉工到AI专家(9)
无监督学习 前面已经说过了无监督学习的概念。无监督学习在实际的工作中应用还是比较多见的。 从典型的应用上说,监督学习比较多用在“分类”上,利用给定的数据,做出一个决策,这个决策在有限的给定可能性中选择其中一种。各类识别、自动驾驶等都属于这一类。 无监督学习则是“聚类”,算法自行寻找输入数据集的规律,并把它们按照规律分别组合,同样特征的放到一个类群。像自然语言理解、推荐算法、数据画像等,都属于这类(实际实现中还是比较多用半监督学习,但最早概念的导入还是属于无监督学习)。 无监督学习的确是没有人工的标注,
俺踏月色而来
2018/06/20
6370
[TensorFlow深度学习深入]实战一·使用embedding_lookup模块对Word2Vec训练保存与简单使用
One hot representation用来表示词向量非常简单,但是却有很多问题。最大的问题是我们的词汇表一般都非常大,比如达到百万级别,这样每个词都用百万维的向量来表示简直是内存的灾难。这样的向量其实除了一个位置是1,其余的位置全部都是0,表达的效率不高,能不能把词向量的维度变小呢?
小宋是呢
2019/06/27
1.8K0
DL杂记:word2vec之TF-IDF、共轭矩阵、cbow、skip-gram
版权声明:本文为博主原创文章,未经博主允许不得转载。有问题可以加微信:lp9628(注明CSDN)。 https://blog.csdn.net/u014365862/article/details/87800246
MachineLP
2019/05/26
8170
cs224d-第二课-word2vec
首先我想说下为什么会去学习cs224d,原先我一直是做工程的,做了大概3年,产品做了好多,但是大多不幸夭折了,上线没多久就下线,最后实在是经受不住心灵的折磨,转行想做大数据,机器学习的,前不久自己学习完了Udacity的深度学习,课程挺好,但是在实际工作中,发现课程中的数据都是给你准备好的,实践中哪来这么多好的数据,只能自己去通过各种手段搞数据,苦不堪言。在找数据的过程中,发现做多的数据还是文本数据,不懂个nlp怎么处理呢,于是就来学习cs224d这门课程,希望在学习过程中能快速将课程所学应用到工作中,fighting!
zhuanxu
2018/08/23
7160
cs224d-第二课-word2vec
教程 | 在Python和TensorFlow上构建Word2Vec词嵌入模型
选自adventuresinmachinelearning 机器之心编译 参与:李诗萌、刘晓坤 本文详细介绍了 word2vector 模型的模型架构,以及 TensorFlow 的实现过程,包括数据
机器之心
2018/05/09
1.9K0
教程 | 在Python和TensorFlow上构建Word2Vec词嵌入模型
TensorFlow-9-词的向量表示
今日资料: https://www.tensorflow.org/tutorials/word2vec 中文版: http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/word2vec.html 这一节是关于 word2vec 模型的,可以用来学习词的向量表达,也叫‘word embeddings’。 之前写过一篇:word2vec 模型思想和代码实现,里面有 skip-gram 算法的简单实现。 http://www.jianshu
杨熹
2018/04/03
9570
TensorFlow-9-词的向量表示
Tensorflow 的 word2vec 详细解释:basic篇
本文介绍了如何使用Tensorflow实现Word2Vec的Skip-Gram模型进行训练,并使用NCE Loss进行优化。同时,还介绍了如何使用Cosine Similarity计算文本相似度,并调用sklearn的TSNE模块进行降维。
腾讯移动品质中心TMQ
2017/08/08
3K0
Tensorflow 的 word2vec 详细解释:basic篇
tensorflow学习笔记(十一):seq2seq Model相关接口介绍
tf.sampled_softmax_loss()中调用了_compute_sampled_logits() 关于__compute_sampled_logits()
ke1th
2019/05/26
9140
【NLP实战】如何基于Tensorflow搭建一个聊天机器人
实战是学习一门技术最好的方式,也是深入了解一门技术唯一的方式。因此,NLP专栏计划推出一个实战专栏,让有兴趣的同学在看文章之余也可以自动动手试一试。
用户1508658
2019/11/06
8450
【NLP实战】如何基于Tensorflow搭建一个聊天机器人
猪年快乐之TensorFlow中实现word2vec及如何结构化TensorFlow模型
猪年快乐之TensorFlow中实现word2vec及如何结构化TensorFlow模型
公众号guangcity
2019/09/20
1.2K0
猪年快乐之TensorFlow中实现word2vec及如何结构化TensorFlow模型
TensorFlow实现Attention机制原理介绍论文阅读代码实现
原理介绍 图片1 图片2 图片3 更多资料: https://distill.pub/2016/augmented-rnns/#attentional-interfaces https://
致Great
2018/06/13
8.8K0
tensorflow学习笔记(三):损失函数
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u012436149/article/details/52874718
ke1th
2019/05/27
7760
TF入门04-TF实现Word2Vec
Word2Vec是一组用来产生词嵌入的模型,包括两种主要的模型:skip-gram和CBOW。
公众号-不为谁写的歌
2020/07/23
1.1K0
无所不能的Embedding 1 - Word2vec模型详解&代码实现
word2vec是google 2013年提出的,从大规模语料中训练词向量的模型,在许多场景中都有应用,信息提取相似度计算等等。也是从word2vec开始,embedding在各个领域的应用开始流行,所以拿word2vec来作为开篇再合适不过了。本文希望可以较全面的给出Word2vec从模型结构概述,推导,训练,和基于tf.estimator实现的具体细节。完整代码戳这里 https://github.com/DSXiangLi/Embedding
风雨中的小七
2020/08/11
1.9K0
无所不能的Embedding 1 - Word2vec模型详解&代码实现
Sampled Softmax
本文提出了一种用于神经机器翻译的概率采样方法,以解决大词汇表条件下的翻译问题。该方法通过采样来近似计算softmax,从而避免了巨大的词汇表所带来的计算成本。该方法在翻译质量、生成速度和硬件加速方面取得了显著的优势,具有很好的应用前景。
用户1148830
2018/01/04
2.5K0
Sampled Softmax
相关推荐
Tensorflow实现word2vec
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验