首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Tensorflow嵌入空间不足

Tensorflow嵌入空间不足
EN

Stack Overflow用户
提问于 2016-07-14 22:40:20
回答 1查看 548关注 0票数 0

我正在尝试在tensorflow上为1,000,000个单词创建嵌入。每个单词将具有表示该单词的256个float32向量。问题是我一直在耗尽内存。这对我来说没有意义,因为我的GTX 1080上有8 8GB的内存。嵌入应该只占用1e6 * 256 *4=1 Gb内存。我还有另一个大小相同的输出矩阵。除此之外,还有一些其他的张量,它们应该比较小。因此,我只看到需要大约2-3 GB的内存来存储模型,并且当我调用sess.run(tf.initialize_all_variables())时,它会失败。我所有的记忆都到哪里去了?你有什么建议可以帮我解决这个问题吗?

代码语言:javascript
运行
复制
import tensorflow as tf
import nltk
import numpy as np
import os
import multiprocessing
import itertools
import pickle
from unidecode import unidecode

BATCH_SIZE = 32
TIME_STEPS = 64
WORD_VEC_SIZE = 256

words, training_data = pickle.load(open('vocab.pickle', 'rb'))
word2index = {w:i for i, w in enumerate(words)}
index2word = {i:w for i, w in enumerate(words)}

input_tensor = tf.placeholder(tf.int32, (BATCH_SIZE, TIME_STEPS + 1), 'input_tensor')
embedding = tf.Variable(tf.random_uniform((len(words), WORD_VEC_SIZE), -1, 1), name = 'embedding')

rnn = tf.nn.rnn_cell.BasicRNNCell(WORD_VEC_SIZE)
state = tf.zeros((BATCH_SIZE, rnn.state_size))
input_vectors = tf.nn.embedding_lookup([embedding], input_tensor[:, :TIME_STEPS])
cost = 0

with tf.variable_scope('rnn') as scope:
    W_out = tf.get_variable('W_out', (WORD_VEC_SIZE, len(words)), initializer = tf.truncated_normal_initializer(0.0, 1 / np.sqrt(WORD_VEC_SIZE)))
    b_out = tf.get_variable('b_out', (len(words), ), initializer = tf.truncated_normal_initializer(0.0, 0.01))
    for t in range(TIME_STEPS):
        y, state = rnn(tf.reshape(input_vectors[:, t, :], (-1, WORD_VEC_SIZE)), state)
        cost += tf.reduce_mean(tf.nn.sampled_softmax_loss(W_out, b_out, y, tf.reshape(input_tensor[:, t + 1], (-1, 1)), 1000, len(words)))
        scope.reuse_variables()

train_step = tf.train.AdamOptimizer(1e-4).minimize(cost)

sess = tf.Session()
sess.run(tf.initialize_all_variables())
saver = tf.train.Saver()
EN

回答 1

Stack Overflow用户

发布于 2016-07-14 22:46:54

我没有考虑到的是AdamOptimizer。我忘记了这需要为我的模型中的每个权重存储各种参数。当我改用GraidentDecent优化器时,它现在适合我的图形处理器了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38377189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档