我正在使用堆叠式LSTM进行多类分类,其中有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
我知道如何解决该错误! 有人知道这可能是什么原因吗?以及如何调试此错误?
答案 0 :(得分:1)
似乎您正在尝试将字符串数据直接馈入网络。因此,错误Cast string to float is not supported
。如果要处理分类数据,则应先将其转换为数字。根据您使用的分类数据的类型,应采用不同的技术。对于文本,请考虑阅读官方的Tensorflow guide on embedding。或者,如果您的数据由单个令牌组成,例如Toyota
,BMW
,Ford
,请签出category_encoders。