在numpy形状参数中使用元组

时间:2017-08-26 03:22:19

标签: python python-3.x numpy

第一篇文章:)如果我做错了什么,不要开枪!

是否有更简便的方法来定义下面的形状?它有效,但有点长而且没有动态。

def neural_net_image_input(image_shape):
    """
    Return a Tensor for a batch of image input
    : image_shape: Shape of the images (taken from CIFAR10)
    : return: Tensor for image input.
    """
    x = tf.placeholder(tf.float32, shape=[None, image_shape[0], image_shape[1],  image_shape[2]], name='x')
    return x

我在SO和其他网站上搜索了大约一个小时没有成功。我最初尝试过这个,

shape = [None, image_shape]

但有错误(我明白了)

TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

那么有没有办法将元组更改为我的形状参数中可接受的形式?

1 个答案:

答案 0 :(得分:3)

使用元组添加:

shape=(None,)+image_shape
# or if you want to allow lists and other sequences for image_shape:
shape=(None,)+tuple(image_shape)

或具有可迭​​代解包概括的最新Python版本:

shape=(None, *image_shape)
相关问题