简体   繁体   English

Keras 符号输入/输出未实现 __len__ 错误

[英]Keras symbolic inputs/outputs do not implement __len__ Error

I want to build an AI to solve an optimization problem in a given environment, but I get the following error我想构建一个 AI 来解决给定环境中的优化问题,但出现以下错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-352-765c5782fe72> in <module>()
      1 model=Model(inputs=input_layer,outputs=output)
----> 2 model.compile(optimizer='adam',loss=-RewardFn,metrics=['acc'])
      3 model.summary()

1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/keras_tensor.py in __len__(self)
    219 
    220   def __len__(self):
--> 221     raise TypeError('Keras symbolic inputs/outputs do not '
    222                     'implement `__len__`. You may be '
    223                     'trying to pass Keras symbolic inputs/outputs '

TypeError: Keras symbolic inputs/outputs do not implement `__len__`. You may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model. This error will also get raised if you try asserting a symbolic input/output directly.

I found out about this error and it is said to be a problem with tensorflow.我发现了这个错误,据说是 tensorflow 的问题。 But I don't know how to solve it.但我不知道如何解决它。 This is my model这是我的 model

!pip install keras-rl2
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from google.colab import files
import io
# %matplotlib inline
import seaborn as sns

sns.set(style='darkgrid')
uploaded=files.upload()
cols=['node1x','node2x','node3x','node4x','node1y','node2y','node3y','node4y','Rmin']
Dataset=pd.read_csv(io.StringIO(uploaded['DNNsamples.csv'].decode('utf-8')),names=cols,header=None)

Dataset.head(20)

from sklearn.model_selection import train_test_split
X_train,X_test=train_test_split(Dataset,test_size=0.2,random_state=42)

from tensorflow.keras.layers import Input,Dense,Activation,Dropout,Flatten
from tensorflow.keras.models import Model
------

input_layer=Input(shape=(Dataset.shape[1],))
dense_layer1=Dense(21,activation='relu')(input_layer)
dense_layer2=Dense(21,activation='relu')(dense_layer1)
dense_layer3=Dense(21,activation='relu')(dense_layer2)
dense_layer4=Dense(21,activation='relu')(dense_layer3)
dense_layer5=Dense(21,activation='relu')(dense_layer4)
dense_layer6=Dense(21,activation='relu')(dense_layer5)
output=Dense(outputss,activation='sigmoid')(dense_layer6)
-----
RewardFn=Ravg+Constraint1+Constraint2+Constraint3+Constraint4+Constraint5
tf.shape(RewardFn)

model=Model(inputs=input_layer,outputs=output)
model.compile(loss=-RewardFn,optimizer='adam',metrics=['acc'])
model.summary()

Could it be a problem to use input and output values in a loss function?在损失 function 中使用输入和 output 值会不会有问题? I use Google Colab.我使用谷歌 Colab。

You do not need to specifically install the Keras package separately.您无需单独安装Keras package。 You can import Keras from TensorFlow .您可以从TensorFlow Keras Also, please provide the right alias while importing Input as below.另外,请在导入输入时提供正确的别名,如下所示。 Input is submodule of tf.keras API, not part of tensorflow.keras.layers API. Input is submodule of tf.keras API, not part of tensorflow.keras.layers API.

from tensorflow import keras
from tensorflow.keras import Input
from tensorflow.keras.layers import Dense,Activation,Dropout,Flatten

Please check the tensorflow and keras version should be as per this tested build configurations .请检查tensorflowkeras版本应符合此测试的构建配置 Let us know if the issue still persists.让我们知道问题是否仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM