Error converting shape to a TensorShape: int() argument must be a string or a number, not 'tuple'.

本文探讨了Python中星号(*)解包语法的使用场景,特别是在不同版本的Python环境中,*表达式在函数调用中的行为变化。文章指出,在Python 3中,*表达式的使用规则有所调整,以避免潜在的混淆,并引用了相关提案讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

def model_inputs(real_dim, z_dim):
#    inputs_real = tf.placeholder(tf.float32, (None, *real_dim), name='input_real')
    inputs_real = tf.placeholder(tf.float32, (None, 32,32,3), name='input_real')   
    inputs_z = tf.placeholder(tf.float32, (None, z_dim), name='input_z')
    
    return inputs_real, inputs_z

 

备注:real_dim =(32,32,3)

 

为什么别人的代码可以使用*real_dim,我的环境下确不行?

 

 

我预计最新的Python环境下,可以成功运行;

        while current_index + batch_size <= self.shape[0]:
            data_batch = get_batch(
                self.data_files[current_index:current_index + batch_size],
#                *self.shape[1:3],
                self.image_mode,
                *self.shape[1:3])

 

SyntaxError: only named arguments may follow *expression

 

30 down vote accepted

As Raymond Hettinger's answer points out, this may change has changed in Python 3 and here is a related proposal, which has been accepted. Especially related to the current question, here's one of the possible changes to that proposal that was discussed:

Only allow a starred expression as the last item in the exprlist. This would simplify the unpacking code a bit and allow for the starred expression to be assigned an iterator. This behavior was rejected because it would be too surprising.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值