Tensorflow/Keras LSTM 错误 "Function call stack: distributed_function"

Tensorflow / Keras LSTM errors "Function call stack: distributed_function"

我正在使用堆叠 LSTM 进行多class class化,其中我有 5 个“字符串”标签。 这是代码片段:

# define parameters
#epochs, batch_size = 20, 46

epochs, batch_size = 5, 40
# define model
model = Sequential()
model.add(LSTM(128,input_shape=(X_train.shape[1],X_train.shape[2]),return_sequences=True))
model.add(LSTM(100, activation='relu',return_sequences=True))
model.add(LSTM(64, activation='relu'))
model.add(Dense(5, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
#-------------------------------------------------------------------
history = model.fit(X_train, Y_train, 
                    epochs=epochs, 
                    batch_size=batch_size, 
                    verbose=1)

我收到这个错误:

UnimplementedError:  Cast string to float is not supported
     [[node metrics/accuracy/Cast (defined at C:\Users\"emitted"LSTM.py:152) ]] [Op:__inference_distributed_function_4954348]

Function call stack:
distributed_function

我想到了如何解决这个错误! 有谁知道可能是什么原因?以及如何调试此错误?

您似乎正试图将字符串数据直接馈送到网络中。因此错误 Cast string to float is not supported。如果您正在处理分类数据,则应先将其转换为数值。根据您使用的分类数据的类型,应应用不同的技术。对于文本,请考虑阅读官方 Tensorflow guide on embedding. Or if your data consists of single tokens, like Toyota, BMW, Ford, check out category_encoders.