首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理UserWarning:将稀疏IndexedSlices转换为形状未知的密集张量

如何处理UserWarning:将稀疏IndexedSlices转换为形状未知的密集张量
EN

Stack Overflow用户
提问于 2017-08-25 21:19:43
回答 1查看 16.3K关注 0票数 19

我在Tensorflow中收到以下警告: UserWarning:将稀疏IndexedSlices转换为形状未知的密集张量。这可能会消耗大量内存。

我得到这个的原因是:

代码语言:javascript
复制
import tensorflow as tf
# Flatten batch elements to rank-2 tensor where 1st max_length rows 
    #belong to first batch element and so forth
all_timesteps = tf.reshape(raw_output, [-1, n_dim])  # (batch_size*max_length, n_dim)
# Indices to last element of each sequence.
# Index to first element is the sequence order number times max 
    #sequence length.
# Index to last element is the index to first element plus sequence 
    #length.
row_inds = tf.range(0, batch_size) * max_length + (seq_len - 1)
# Gather rows with indices to last elements of sequences
# http://stackoverflow.com/questions/35892412/tensorflow-dense-gradient-explanation
# This is due to gather returning IndexedSlice which is later 
    #converted into a Tensor for gradient
# calculation.
last_timesteps = tf.gather(all_timesteps, row_inds)  # (batch_size,n_dim)  

tf.gather导致了此问题。我一直忽略它,直到现在,因为我的架构并不是很大。然而,现在,我有了更大的架构和大量的数据。当训练批处理大小大于10的时候,我面临着内存不足的问题。我相信处理这个警告将允许我在GPU中适应我的模型。

请注意,我使用的是Tensorflow 1.3。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-28 19:05:49

我通过使用tf.dynnamic_partition而不是tf.gather解决了这个问题。我像这样替换了上面的代码:

代码语言:javascript
复制
# Flatten batch elements to rank-2 tensor where 1st max_length rows belong to first batch element and so forth
all_timesteps = tf.reshape(raw_output, [-1, n_dim])  # (batch_size*max_length, n_dim)
# Indices to last element of each sequence.
# Index to first element is the sequence order number times max sequence length.
# Index to last element is the index to first element plus sequence length.
row_inds = tf.range(0, batch_size) * max_length + (seq_len - 1)
# Creating a vector of 0s and 1s that will specify what timesteps to choose.
partitions = tf.reduce_sum(tf.one_hot(row_inds, tf.shape(all_timesteps)[0], dtype='int32'), 0)
# Selecting the elements we want to choose.
last_timesteps = tf.dynamic_partition(all_timesteps, partitions, 2)  # (batch_size, n_dim)
last_timesteps = last_timesteps[1]
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45882401

复制
相关文章

相似问题

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