2017-08-29 69 views
0

我正在尝试构建可变批量大小,可变整形和可变重量形状的图形。我正在使用tensorflow 1.3.0。Tensorflow可变批量大小,可变整形和权重

用下面的代码,tf.get_variable抛出一个类型错误:INT()参数必须是字符串或数字,而不是“张量”pool2被定义在代码的其他地方。

# declare placeholder for variable batch size 
images_ph = tf.placeholder(tf.float32, shape=[None, 64, 64, 1]) 
# code for 2 layers of convolution, normalization and max pooling 
# reshape to perform, one matrix multiply 
reshape = tf.reshape(pool2, [tf.shape(images_ph)[0], -1]) 
dim = tf.shape(reshape)[1] 
var = tf.get_variable('name', [dim, 384], validate_shape=False) 

我还试图替代暗淡具有“正确的”类型如下:

dim = reshape.get_shape()[1] 

暗淡等于和抛出一个ValueError异常:的形状一个新的变量(local3/xpto)必须被完全定义,而是(?,384)。

+0

它看起来像pool2没有定义。 – Kamran

+0

pool2未在代码段中定义,但在代码的其他位置定义 – rafaelvalle

回答

0

感谢大家的努力帮助。

鉴于我将pool2的输出展平以执行一个matmul,所以解决方案是显式计算重塑的第二维的长度。这是在第1行和第2行计算的,其中第一行的偏移量来自可变批量大小。

我无法使用所有尺寸未指定的重塑。

pool2_shapes = pool2.get_shape().as_list()[1:] 
pool2_features_length = reduce(lambda x, y: x*y, pool2_shapes) 
reshape = tf.reshape(pool2, [tf.shape(images)[0], pool2_features_length]) 
+0

您可以将'-1'传递给'reshape'的任何一个维度来获得'rest',所以只需使用'pool2 = tf.reshape(pool2,(tf.shape(images)[0],-1) )' – DomJack

+0

这不起作用,因为tf.shape(图像)[0]是无。这正是我的问题。 – rafaelvalle

+0

啊,在这种情况下,你可以节省查找'None'形状,'tf.reshape(pool2,(-1,pool2_features_length))'的努力。不是很大的节约,但如果你问我,会使代码更具可读性。 – DomJack

1

的第二个参数get_variable取整数或字符串,但作为错误说,你给它一个张[dim, 384]

参见:https://www.tensorflow.org/api_docs/python/tf/get_variable

+0

感谢您的评论。我也试过这个,但它引发了另一个错误。我认为这可能是可以解决的,如果我使用validate_shape作为False。 – rafaelvalle

+0

尝试类似于 'var = tf.get_variable('name',tf.shape(reshape))' – Kamran

+0

这将返回一个错误,因为tf.shape(reshape)[1]的类型是张量。 – rafaelvalle

0

tf.shape返回一个张量,所以在[dim, 384]昏暗是张量;这里是一个int所需要的。

尝试dim = reshape.get_shape().as_list()[1]#输出为int类型的

+0

感谢您的评论。我也试过这个,但它引发了另一个错误。请检查更新的问题。 – rafaelvalle

+0

您错过了'as_list()' – prometeu

+0

虽然as_list从Dimension更改为None类型,但它不能解决问题。 – rafaelvalle