在numpy形状argumen中使用元组

2024-04-19 20:40:46 发布

您现在位置:Python中文网/ 问答频道 /正文

第一个帖子:)如果我做错了什么,别开枪!在

有没有更简捷的方法来定义下面的形状?它是可行的,但有点冗长,没有活力。在

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

我已经搜索了大约一个小时没有成功,在某某和其他网站。我最初确实试过这个

^{pr2}$

但有错误(我明白)

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

那么,有没有一种方法可以将元组更改为我的形状参数中可以接受的形式?在


Tags: of方法imageforinputreturn定义tf
1条回答
网友
1楼 · 发布于 2024-04-19 20:40:46

使用元组加法:

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

或者在最新的Python版本中使用iterable解压归纳:

^{pr2}$

相关问题 更多 >