繁体   English   中英

在numpy形状参数中使用元组

[英]Using tuple in a numpy shape argument

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

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

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'

那么有没有办法将元组变成一个可以在我的形状参数中被接受的形式?

使用元组添加:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM