我为一对一单词分类建立了一个lstm模型:
model = Sequential()
model.add(layers.Input(shape=(None, 512), ragged=True))
model.add(layers.LSTM(32, return_sequences=True, dropout=0.4))
model.add(layers.TimeDistributed(layers.Dense(13, activation='softmax')))
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.summary()
然后,我将x_train生成为tf.RaggedTensor,其形状为[10000, 无,512],dtype ='float32'。 y_train作为tf.RaggedTensor,具有形状 [10000,无,13],dtype ='float32'。
现在可以通过以下方式运行它:
history = model.fit(x, y, epochs=10, verbose=1)
我会得到:
TypeError: Failed to convert object of type <class 'tensorflow.python.ops.ragged.ragged_tensor.RaggedTensor'> to Tensor. Contents: tf.RaggedTensor(values=Tensor("sequential_42/time_distributed_46/dense_54/Softmax:0", shape=(None, 13), dtype=float32), row_splits=Tensor("sequential_42/time_distributed_46/RaggedFromRowLengths/control_dependency:0", shape=(None,), dtype=int64)). Consider casting elements to a supported type.
这是简化的完整代码:
import tensorflow as tf
from tensorflow.keras import layers, Model, Sequential
import numpy as np
x = tf.RaggedTensor.from_row_splits(np.ones((100, 512)), [0, 4, 20, 100])
y = tf.RaggedTensor.from_row_splits(np.ones((100, 13)), [0, 4, 20, 100])
print(x.shape)
print(y.shape)
model = Sequential()
model.add(layers.Input(shape=(None, 512), ragged=True))
model.add(layers.LSTM(32, return_sequences=True, dropout=0.4))
model.add(layers.TimeDistributed(layers.Dense(13, activation='softmax')))
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.summary()
history = model.fit(x=x, y=y, epochs=10, verbose=1)