如何将张量流张量切成多个
Posted
技术标签:
【中文标题】如何将张量流张量切成多个【英文标题】:how to slice the tensorflow tensor to multiple 【发布时间】:2021-05-05 12:38:54 【问题描述】:张量是: 批次(3) * 长度(5) * 暗淡(2)
tensor = tf.constant([[[1,1],[2,2],[3,3],[4,4],[5,5]],[[1,1],[2,2],[3,3],[4,4],[5,5]],[[1,1],[2,2],[3,3],[4,4],[5,5]]] )
我想根据 length_axis_index[0,1,2,3,4] 通过 length_index [0,0],[0,1] ... [3,4],[4,4] 获得更多切片,类似的操作
spans_length=0
with tf.variable_scope("loss_span"):
output=[]
for i in range(0,1+n_spans):
for j in range(1,seq_length):
if j + i < seq_length:
res = tf.slice(output_layer_sequence, [0, j, 0], [-1, j+i-j+1, -1])
res = tf.reduce_sum(res,axis=1)
output.append(res)
# output = tf.convert_to_tensor(output)
spans_length+=1
output = tf.convert_to_tensor(output)
vsp = tf.transpose(output, [1,0,2])#batch , spans_length,hidden_size
vsp = tf.reshape(vsp,[-1,hidden_size])#batch * span_length,hidden_size
span_logits = tf.matmul(vsp, output_span_weight, transpose_b=True) # output:[batch * spans_length,class_labels]
span_logits = tf.nn.bias_add(span_logits, output_span_bias) # output:[batch * spans_length,class_labels]
span_matrix = tf.reshape(span_logits,[-1,spans_length,class_labels],name="span_matrix_val")#[batch , spans_length,class_labels]
label_span_logists = tf.one_hot(indices=label_span,depth=class_labels, on_value=1, off_value=0, axis=-1, dtype=tf.int32)
label_span_logists=tf.cast(label_span_logists,tf.int64)
span_loss = tf.nn.softmax_cross_entropy_with_logits(logits=span_matrix, labels=label_span_logists)
span_loss = tf.reduce_mean(span_loss, name='loss_span')
当我做这样的操作时,训练模型的时间很长;如何加速它。谢谢
【问题讨论】:
请澄清 -slice(0,0)
是什么意思。是tensor[:,0,0]
吗?或者`张量[:,0:0,:] ?
slice(0,0) 表示 a1=tf.slice(tensor,[0,0,0],[-1,0,-1])) slice(0,1) 表示 a2 =tf.slice(tensor,[0,0,0],[-1,1,-1])) slice(0,2) 表示 a3=tf.slice(tensor,[0,0,0],[ -1,2,-1])),
【参考方案1】:
此代码有效:
# tensor = tf.constant([[[1,1],[2,2],[3,3],[4,4],[5,5]],[[1,1],[2,2],[3,3],[4,4],[5,5]],[[1,1],[2,2],[3,3],[4,4],[5,5]]] )
tensor = tf.random.uniform((3, 2000, 2))
length = tf.shape(tensor)[1].numpy()
output = []
for begins in range(length):
for size in range(length - begins):
res = tf.slice(tensor, [0, begins, 0], [-1, size + 1, -1])
res = tf.reduce_sum(res)
output.append(res)
output = tf.convert_to_tensor(output)
我尝试使用tf.scan()
,但没有看到任何好处:
output = tf.constant([], tf.int32)
for begins in range(length):
t = tensor[:, begins:, :]
t = tf.transpose(t, (1, 0, 2))
t = tf.scan(lambda a, x: a + x, t)
t = tf.transpose(t, (1, 0, 2))
t = tf.reduce_sum(t, [0, 2])
output = tf.concat([output, t], 0)
编辑:
尝试在预处理中沿未使用的维度 [0, 2] 应用 reduce_sum()
:
tensor = tf.reduce_sum(tensor, [0, 2])
output = tf.constant([])
for begins in range(length):
t = tensor[begins:]
t = tf.scan(lambda a, x: a + x, t)
output = tf.concat([output, t], 0)
仍然没有看到性能优势。
【讨论】:
我在做这样的操作时,训练时间很长;如何加快速度。谢谢***.com/a/66011043/8375120 @Colt 查看编辑。我认为不可能显着改善 - 瓶颈是scan()
,我怀疑是否有可能比scan()
更快地计算累积和【参考方案2】:
for i in range(0,50):
for j in range(1,200):
if j + i < 200:
res = tf.slice(output_layer_sequence, [0, j, 0], [-1, j+i-j+1, -1])
res = tf.reduce_sum(res,axis=1)
output.append(res)
output = tf.convert_to_tensor(output)
当我做这样的操作时,训练时间很长;如何加快它。谢谢
【讨论】:
以上是关于如何将张量流张量切成多个的主要内容,如果未能解决你的问题,请参考以下文章