使用大于 2GB 的数组初始化 tensorflow 变量

编程入门 行业动态 更新时间:2024-10-12 10:21:44
本文介绍了使用大于 2GB 的数组初始化 tensorflow 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试使用预训练的 word2vec 嵌入初始化 tensorflow Variable.

I am trying to initialize a tensorflow Variable with pre-trained word2vec embeddings.

我有以下代码:

import tensorflow as tf
from gensim import models

model = models.Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
X = model.syn0

embeddings = tf.Variable(tf.random_uniform(X.shape, minval=-0.1, maxval=0.1), trainable=False)

sess.run(tf.initialize_all_variables())

sess.run(embeddings.assign(X))

我收到以下错误:

ValueError: Cannot create an Operation with a NodeDef larger than 2GB.

我尝试分配的数组 (X) 的形状为 (3000000, 300),大小为 3.6GB.

The array (X) I am trying to assign is of shape (3000000, 300) and its size is 3.6GB.

如果我也尝试 tf.convert_to_tensor(X),我也会遇到同样的错误.

I am getting the same error if I try tf.convert_to_tensor(X) as well.

我知道它失败是因为数组大于 2GB.但是,我不知道如何将大于 2GB 的数组分配给 tensorflow Variable

I know that it fails due to the fact that the array is larger than 2GB. However, I do not know how to assign an array larger than 2GB to a tensorflow Variable

推荐答案

最简单的解决方案是将其 feed_dict'ing 到一个占位符节点,用于 tf.assign 给变量.

The easiest solution is to feed_dict'ing it into a placeholder node that you use to tf.assign to the variable.

X = tf.Variable([0.0])
place = tf.placeholder(tf.float32, shape=(3000000, 300))
set_x = X.assign(place)
# set up your session here....
sess.run(set_x, feed_dict={place: model.syn0})

正如 Joshua Little 在单独的回答中指出的那样,您也可以在初始化程序中使用它:

As Joshua Little noted in a separate answer, you can also use it in the initializer:

X = tf.Variable(place)    # place as defined above
...
init = tf.initialize_all_variables()
... create sess ...
sess.run(init, feed_dict={place: model.syn0})

这篇关于使用大于 2GB 的数组初始化 tensorflow 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 05:30:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   初始化   变量   GB   tensorflow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!