在 运行 elmo 嵌入 google colab 时出现错误

Getting Errors while running elmo embeddings in google colab

我正在通过 elmo 提取特征。训练和测试是文本 data.I 在 google colab 中执行时出现错误。我检查了以前的 Whosebug 问题,但无法解决。带有指针的确切代码会有所帮助。

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 初始化失败,所以尝试查看上面是否打印了警告日志消息。 [[节点 module_apply_default_1/bilm/CNN_2/Conv2D_6(定义于 /usr/local/lib/python3.6/dist-packages/tensorflow_hub/native_module.py:517)]] [[节点均值(定义为:8)]]

我现在在 colab.research.google.com 上尝试了 Python 3 个有和没有 GPU 的运行时,并且你的代码的以下改编运行:

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 问题。

这也发生在我身上。我认为这是关于我们在 Google Colab 免费层版本中获得的免费 RAM 使用量。为了 运行 它,我不得不减少一些卷积层并减小批量大小。另外,我无法在 GPU 上 运行 它。 所以,我想你可以考虑使用 Google Colab Pro