张量流得到张量的一个数

2024-04-27 20:00:14 发布

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

我有一个随机搜索问题。我需要超参数

z_mean = Dense(units=hp.Int('unitsBottle',min_value=8,max_value=48,step=8))(h)
latentVariable = hp.get('unitsBottle')
z_log_var = Dense(units=latentVariable)(h)

latentVariable是张量。我可以打印它:

tf.Tensor(16, shape=(), dtype=int32)

但是我需要16号。不是张量。作为一个整数(标量),这样我就可以把它放到

z = Lambda(sampling, 
            output_shape=(latenteVariable,))([z_mean, 
            z_log_var, latenteVariable])

我怎么能只知道电话号码


Tags: log参数valuevarminmeanmaxdense
2条回答

@Ynjxsjmh所述,使用.numpy()更直观。然而,我们也可以在张量上使用eval来获得值

from tensorflow.keras.backend import eval 

one = tf.constant(8)
two = tf.constant(8)
product = tf.add(one, two)

eval(one), eval(two), eval(product)
(8, 8, 16)

这就像在张量上调用int()一样简单:

>>> tensor = tf.constant(16)
>>> print(tensor)
tf.Tensor(16, shape=(), dtype=int32)

>>> int(tensor)
16

>>> tensor.numpy()
16
>>> print(type(tensor.numpy()))
<class 'numpy.int32'>

相关问题 更多 >