Search code examples
pythontensorflowkeraslstmsoftmax

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


I am using a stacked LSTM for a multi-class classification where I have 5 "string" labels. Here is a snippet of the code:

# 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)

I got this error:

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

I have on idea how to revolve this error! Does anyone know what might be the reason? and How to debug this error?


Solution

  • It seems that you are trying to feed string data directly into the network. Hence the error Cast string to float is not supported. If you are dealing with categorical data, you should convert it into numerical first. Depending on the type of categorical data you are using, different techniques should be applied. For text, consider reading official Tensorflow guide on embedding. Or if your data consists of single tokens, like Toyota, BMW, Ford, check out category_encoders.