keras 在Graph执行中不允许使用符号'tf.Tensor'作为Python 'bool',请使用Eager执行或使用@tf.function修饰此函数

unftdfkk  于 2022-12-13  发布在  Python
关注(0)|答案(1)|浏览(353)

我正在尝试构建Keras功能模型,这是我代码

input_l = Input(shape = [155])
dense_layer1 = Dense(units=512,activation=nn.ReLU)(input_l)
dense_layer2 = Dense(units=256,activation=nn.ReLU)(dense_layer1)
dense_layer3 = Dense(units=128,activation=nn.ReLU)(dense_layer2)
dense_layer4 = Dense(units=64,activation=nn.ReLU)(dense_layer3)
output_layer = Dense(units=5,activation=nn.Softmax)(dense_layer4)
fmodel = keras.Model(inputs=input_l, outputs=output_layer)
print(fmodel.summary())

出现错误

{
    "name": "OperatorNotAllowedInGraphError",
    "message": "Using a symbolic `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.",
    "stack": "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mOperatorNotAllowedInGraphError\u001b[0m            Traceback (most recent call last)\nCell \u001b[1;32mIn[218], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m input_l \u001b[39m=\u001b[39m Input(shape \u001b[39m=\u001b[39m [\u001b[39m155\u001b[39m])\n\u001b[1;32m----> 2\u001b[0m dense_layer1 \u001b[39m=\u001b[39m Dense(units\u001b[39m=\u001b[39;49m\u001b[39m512\u001b[39;49m,activation\u001b[39m=\u001b[39;49mnn\u001b[39m.\u001b[39;49mReLU)(input_l)\n\u001b[0;32m      3\u001b[0m dense_layer2 \u001b[39m=\u001b[39m Dense(units\u001b[39m=\u001b[39m\u001b[39m256\u001b[39m,activation\u001b[39m=\u001b[39mnn\u001b[39m.\u001b[39mReLU)(dense_layer1)\n\u001b[0;32m      4\u001b[0m dense_layer3 \u001b[39m=\u001b[39m Dense(units\u001b[39m=\u001b[39m\u001b[39m128\u001b[39m,activation\u001b[39m=\u001b[39mnn\u001b[39m.\u001b[39mReLU)(dense_layer2)\n\nFile \u001b[1;32mc:\\Users\\ahmed\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\utils\\traceback_utils.py:70\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m     67\u001b[0m     filtered_tb \u001b[39m=\u001b[39m _process_traceback_frames(e\u001b[39m.\u001b[39m__traceback__)\n\u001b[0;32m     68\u001b[0m     \u001b[39m# To get the full stack trace, call:\u001b[39;00m\n\u001b[0;32m     69\u001b[0m     \u001b[39m# `tf.debugging.disable_traceback_filtering()`\u001b[39;00m\n\u001b[1;32m---> 70\u001b[0m     \u001b[39mraise\u001b[39;00m e\u001b[39m.\u001b[39mwith_traceback(filtered_tb) \u001b[39mfrom\u001b[39;00m \u001b[39mNone\u001b[39m\n\u001b[0;32m     71\u001b[0m \u001b[39mfinally\u001b[39;00m:\n\u001b[0;32m     72\u001b[0m     \u001b[39mdel\u001b[39;00m filtered_tb\n\nFile \u001b[1;32mc:\\Users\\ahmed\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\torch\\nn\\modules\\module.py:1997\u001b[0m, in \u001b[0;36mModule.__repr__\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   1994\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m__repr__\u001b[39m(\u001b[39mself\u001b[39m):\n\u001b[0;32m   1995\u001b[0m     \u001b[39m# We treat the extra repr like the sub-module, one item per line\u001b[39;00m\n\u001b[0;32m   1996\u001b[0m     extra_lines \u001b[39m=\u001b[39m []\n\u001b[1;32m-> 1997\u001b[0m     extra_repr \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mextra_repr()\n\u001b[0;32m   1998\u001b[0m     \u001b[39m# empty string will be split into list ['']\u001b[39;00m\n\u001b[0;32m   1999\u001b[0m     \u001b[39mif\u001b[39;00m extra_repr:\n\nFile \u001b[1;32mc:\\Users\\ahmed\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\torch\\nn\\modules\\activation.py:105\u001b[0m, in \u001b[0;36mReLU.extra_repr\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    104\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mextra_repr\u001b[39m(\u001b[39mself\u001b[39m) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m \u001b[39mstr\u001b[39m:\n\u001b[1;32m--> 105\u001b[0m     inplace_str \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39minplace=True\u001b[39m\u001b[39m'\u001b[39m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39minplace \u001b[39melse\u001b[39;00m \u001b[39m'\u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m    106\u001b[0m     \u001b[39mreturn\u001b[39;00m inplace_str\n\n\u001b[1;31mOperatorNotAllowedInGraphError\u001b[0m: Using a symbolic `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function."
}

Python版本3.10.0tensorflow 版本'2.11.0'
找不到相关问题

sdnqo3pr

sdnqo3pr1#

要使用函数API创建模型,请编写如下代码:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Input

input_l = Input(shape = [155])
dense_layer1 = Dense(units=512,activation="relu")(input_l)
dense_layer2 = Dense(units=256,activation="relu")(dense_layer1)
dense_layer3 = Dense(units=128,activation="relu")(dense_layer2)
dense_layer4 = Dense(units=64,activation="relu")(dense_layer3)
output_layer = Dense(units=5,activation="softmax")(dense_layer4)
fmodel = keras.Model(inputs=input_l, outputs=output_layer)
print(fmodel.summary())

相关问题