2017-07-24 86 views
0

我想了解tf.strided_slice如何工作。为了做到这一点,我已经写了下面的代码:Tensorflow tf_strided_slice详细说明

import numpy as np 
import tensorflow as tf 

# parameters 
record_size = 841 

# create a random vector of 1682 integers in range [0.255] 
content = np.random.randint(255,size=[1682]) 

depth_major = tf.reshape(
    tf.strided_slice(content, [0], 
        [record_size]), 
        [1, 29, 29]) 

depth_major1 = tf.reshape(
    tf.strided_slice(content, [record_size+1], 
        [2*record_size]), 
        [1, 29, 29]) 

# Initializing the variables 
init = tf.global_variables_initializer() 

with tf.Session as sess: 
    sess.run(depth_major) 
    print("depth_major", depth_major.shape) 

当我执行上面的例子中,我得到以下错误:

ValueError: Cannot reshape a tensor with 840 elements to shape [1,29,29] (841 elements) for 'Reshape_1' (op: 'Reshape') with input shapes: [840], [3] and with input tensors computed as partial shapes: input[1] = [1,29,29]. 

我根本无法理解,为什么元素的数量自从我在[0]开始并以[record_size]结束后,是840?

回答

0

错误是抱怨Reshape。由于错误消息中的Reshape名称为Reshape_1,因此您可以看到此消息。第二次重塑中的strided_slicerecord_size+1变为2*record_size,尺寸为1682 - 842 = 840。因此,有关错误大小的错误消息。我想你想指定record_size而不是record_size+1

希望有帮助!

相关问题