在Google Colab中运行Elmo嵌入时出现错误

时间:2019-03-23 21:07:40

标签: python-3.x tensorflow google-colaboratory tensorflow-hub

我正在通过elmo提取特征。训练和测试是文本数据。在Google colab中执行时遇到错误。我已经检查了以前的Stackoverflow问题,但无法解决。带有指针的精确代码会有所帮助。

elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
def elmo_vectors(x):
  embeddings = elmo(x.tolist(), signature="default", as_dict=True)["elmo"]

  with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  sess.run(tf.tables_initializer())
  # return average of ELMo features
  return sess.run(tf.reduce_mean(embeddings,1))

import tensorflow as tf
import tensorflow_hub as hub

list_train = [train[i:i+100] for i in range(0,train.shape[0],100)]
list_test = [test[i:i+100] for i in range(0,test.shape[0],100)]

# Extract ELMo embeddings
elmo_train = [elmo_vectors(x['clean_tweet']) for x in list_train]
elmo_test = [elmo_vectors(x['clean_tweet']) for x in list_test]    

我遇到以下错误: UnknownError:无法获得卷积算法。这可能是因为cuDNN初始化失败,所以请尝试查看上面是否打印了警告日志消息。      [[node module_apply_default_1 / bilm / CNN_2 / Conv2D_6(在/usr/local/lib/python3.6/dist-packages/tensorflow_hub/native_module.py:517中定义)]]      [[节点均值(定义为:8)]]

1 个答案:

答案 0 :(得分:0)

我现在在带有和不带有GPU的Python 3运行时中在colab.research.google.com上尝试过,并且运行了以下代码修改:

import tensorflow as tf
import tensorflow_hub as hub

elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
def elmo_vectors(x):
  embeddings = elmo(x,  # Note plain x here.
                    signature="default", as_dict=True)["elmo"]
  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(tf.tables_initializer())
    # return average of ELMo features
    return sess.run(tf.reduce_mean(embeddings, 1))

elmo_vectors(["Hello world"])

我得到输出:

array([[ 0.45319763, -0.99154925, -0.26539633, ..., -0.13455263,
         0.48878008,  0.31264588]], dtype=float32)

我相信这不是TF Hub问题。