Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用tf.keras.layers.reforme时出现类型错误_Python_Tensorflow_Machine Learning_Keras - Fatal编程技术网

Python 使用tf.keras.layers.reforme时出现类型错误

Python 使用tf.keras.layers.reforme时出现类型错误,python,tensorflow,machine-learning,keras,Python,Tensorflow,Machine Learning,Keras,在Keras中构建模型时,我遇到以下错误: TypeError: Expected int32, got 8.0 of type 'float' instead. 该错误发生在最初构建模型时(与执行期间相反),更具体地说是在本代码段的最后一行: d_dense1 = Dense( ((IMAGE_SIZE/4)**2)*(n if vanilla_architecture else 3*n), input_shape = (h,), ac

在Keras中构建模型时,我遇到以下错误:

TypeError: Expected int32, got 8.0 of type 'float' instead.
该错误发生在最初构建模型时(与执行期间相反),更具体地说是在本代码段的最后一行:

    d_dense1 = Dense(
        ((IMAGE_SIZE/4)**2)*(n if vanilla_architecture else 3*n),
        input_shape = (h,),
        activation = "relu",
        name = name_prefix + "dense1"
    )(d_in)
    d_reshape1 = tf.keras.layers.Reshape(
        (IMAGE_SIZE/4, IMAGE_SIZE/4, (n if vanilla_architecture else 3*n)),
        name = name_prefix + "reshape1"
    )(d_dense1)
旁注:我使用的是tf.keras.layers.Dense,IMAGE_SIZE是一个整数,vanilla_架构是一个布尔值,n是一个整数

显然,稠密层将通过一个浮标张量,因为这是一个机器学习操作。问题似乎是重塑需要整数张量。我读了文档,但里面没有信息

以下是我尝试过的一些事情:

  • 使用tf.reforme
    • 同一问题
  • 使用numpy重塑
    • 简单是不行的
  • 阅读示例代码,如
    • 他们似乎在做和我一样的事情,但他们的工作

奇怪的是,它在使用急切执行时工作得很好。但我不想启用急切执行,因为我想使用tensorboard

解决方案是在此行中使用整数除法:

(IMAGE_SIZE//4, IMAGE_SIZE//4, (n if vanilla_architecture else 3*n)),

类型错误不是由于张量造成的,而是IMAGE\u SIZE/4返回了一个浮点值

那么
tf.cast(IMAGE\u SIZE,tf.int32)
呢?可能是因为除法
IMAGE\u SIZE/4
导致了一个浮点值。正如@KotaMori提到的,也许可以尝试使用
tf.cast(IMAGE\u SIZE/4,tf.int32)
将其显式转换为int?谢谢!这就成功了,现在我觉得自己很笨,哈哈托补充了这个答案:你可以通过显式转换为
整数来轻松解决这个问题,就像这样:
(int(IMAGE\u SIZE//4),int(IMAGE\u SIZE//4,…)