首页 文章

Tensorflow可变批量大小,可变重塑和重量

提问于
浏览
1

我正在尝试构建一个具有可变批量大小,可变重塑和可变重量形状的图形 . 我正在使用tensorflow 1.3.0 .

使用下面的代码, tf.get_variable 会抛出 TypeError: int() argument must be a string or a number, not 'Tensor' . 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 替换'correct'类型,如下所示:

dim = reshape.get_shape()[1]

dim 等于 None 并抛出 ValueError: Shape of a new variable (local3/xpto) must be fully defined, but instead was (?, 384).

3 回答

  • 1

    get_variable 的第二个参数采用整数或字符串,但正如错误所说,你给它一个张量 [dim, 384] .

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

  • 0

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

    试试 dim = reshape.get_shape().as_list()[1] #output是int类型

  • 0

    谢谢大家帮忙 .

    鉴于我将 pool2 的输出展平为仅执行一个matmul,解决方案是明确计算重塑的第二维的长度 . 这是在第1行和第2行计算的,其中第一行的 [1:] 偏移量来自变量批量大小 .

    我不能使用reshape,所有尺寸都未指定 .

    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])
    

相关问题