tflearn自定义余弦相似度损失函数



我在tflearn中有一个lstm网络,它在给定前面单词的上下文的情况下预测序列中的下一个单词。单词作为特定大小词汇表的索引输入网络,并以二进制类输出,例如:

上下文:[4524311906,4,2,0,0,0]

标签:[0,0,0…….10,0,…0,0,0](VOCAP_SIZE的长度)

然而,该模型每次都会学习预测几乎相同的单词,因为我在回归层中使用了"类别交叉熵"目标函数。

我想尝试基于单词嵌入(word2vec)来评估损失,这是我已经为数据集生成的。因此,预测"你好"的模型的损失要比预测"披萨"的损失小得多。该计划是计算两个嵌入向量之间的余弦,以获得单词之间的"相似性"。

我将这个自定义的loss函数添加到tflearn安装中的objectives.py文件中,但在尝试将预测转换为嵌入向量时遇到了障碍。

tflearn/objectives.py:

vocab = np.loadtxt('/home/vocab.txt',dtype='str')
embedding_model = gensim.models.Word2Vec.load('/home/embedding')
def embedded_similarity(y_pred, y_true):
global vocab, embedding_model
with tf.name_scope("Similarity"):
#convert one-hot format to indices of max values (predictions)
pred_max = tf.argmax(y_pred,dimension=1)
true_max = tf.argmax(y_true,dimension=1)
#convert indices into embedded vectors
pred_vectors = tf.map_fn(lambda x: embedding_model[vocab[x]], pred_max)
true_vectors = tf.map_fn(lambda x: embedding_model[vocab[x]], true_max)
#calc dot product
dot_products = tf.reduce_sum(tf.mul(pred_vectors,true_vectors),axis=1)
#return inverse mean of dot products
return -1*(tf.reduce_mean(dot_products))

返回的错误为:

ValueError: Index out of range using input dim 0; input has only 0 dims for 'Similarity/map/while/strided_slice' (op: 'StridedSlice') with input shapes: [], [1], [1], [1].

这表明我不能使用张量来索引vocab(一个numpy数组)。然而,我不能使用eval()来获取张量的值,因为这不是在会话中运行的。因此,我需要一种方法来设置索引的一维张量到包含相应单词向量的张量的转换,以便计算损失。

任何关于这个问题的帮助,或者评估我的模型的其他方法,我们都将不胜感激。

我使用了tf.gather来解决这个问题。此外,我添加了根据算法分类的置信度进行缩放,以修复我遇到的无法计算梯度的错误。以下是代码:

objectives.py:标题处的代码

import numpy as np
import gensim
vocab = np.genfromtxt('/home/vocab.txt',dtype='str')
embedding_model = gensim.models.Word2Vec.load('/home/embedding')
i2v = []
for v in vocab:
i2v.append(embedding_model[v])

embedded_similarity(y_pred,y_true):

global i2v
with tf.name_scope("Similarity"):
i2v_tensors = [ tf.cast(tf.constant(iv), tf.float32) for iv in i2v ]
i2v_tensorarray = tf.pack(i2v_tensors)
#convert one-hot to indices
pred_max = tf.cast(tf.argmax(y_pred,dimension=1), tf.int32)
true_max = tf.cast(tf.argmax(y_true,dimension=1), tf.int32)
#extract probabilities for scaling later
pred_iter = tf.concat(tf.constant(1),[y_pred,tf.pack([tf.cast(pred_max,tf.float32)],axis=1)])
confidence_scaler = 1 / tf.map_fn(lambda x: tf.gather(x, tf.cast(tf.gather(x,tf.constant(5002)),tf.int32)), pred_iter, tf.float32)
#convert indices into vectors (of type tensor)
pred_vectors = tf.map_fn(lambda x: tf.gather(i2v_tensorarray, x), pred_max, tf.float32)
true_vectors = tf.map_fn(lambda x: tf.gather(i2v_tensorarray, x), true_max, tf.float32)
#calc dot product
dot_products = tf.reduce_sum(tf.mul(pred_vectors,true_vectors),axis=1)
#divide by magnitudes
pred_magnitudes = tf.sqrt(tf.reduce_sum(tf.mul(pred_vectors,pred_vectors),axis=1))
true_magnitudes = tf.sqrt(tf.reduce_sum(tf.mul(true_vectors,true_vectors),axis=1))
cosines = dot_products / tf.mul(pred_magnitudes,true_magnitudes)
loss = -1*tf.cast(cosines, tf.float32) + 2 #min loss is 1, max is 3
scaled_loss = tf.multiply(loss, confidence_scaler)
# loss = -1*cosines + 1
# return inverse sum of dot products
return tf.reduce_mean(scaled_loss)

然而,我遇到了一个奇怪的bug。当我尝试拟合模型时,代码运行得非常好,直到它列出了训练和验证样本的数量,如下所示:

---------------------------------
Training samples: 800
Validation samples: 200

然后输出只是冻结,但不是整个计算机。我无法控制代码,必须启动另一个终端。系统似乎也没有明显放缓,我已经尝试将训练集大小和批量大小都减少到可笑的低数字,但没有结果。

我将把这个问题标记为已解决,因为我回答了我遇到的主要问题,但如果有人之前遇到过这种行为,请发表评论。谢谢

最新更新