我正在使用量化感知训练。我参考了网上的lstm代码,想把
QAT
放入lstm中,但是遇到了ValueError。
ValueError Traceback (most recent call last)
<ipython-input-11-00669bb76f9d> in <cell line: 6>()
4 return layer
5
----> 6 annotated_model = tf.keras.models.clone_model(
7 model,
8 clone_function=apply_quantization_to_dense,
/usr/local/lib/python3.10/dist-packages/tf_keras/src/models/cloning.py in clone_model(model, input_tensors, clone_function)
544 # Case of a custom model class
545 if clone_function or input_tensors:
--> 546 raise ValueError(
547 "Arguments clone_function and input_tensors "
548 "are only supported for Sequential models "
ValueError: Arguments clone_function and input_tensors are only supported for Sequential models or Functional models. Received model of type 'Sequential', with clone_function=<function apply_quantization_to_dense at 0x78b727ec4040> and input_tensors=None
这是我的代码
import keras
from keras.layers import LSTM
from keras.layers import Dense, Activation
from keras.datasets import mnist
from keras.models import Sequential
from keras.optimizers import Adam
learning_rate = 0.001
training_iters = 20
batch_size = 128
display_step = 10
n_input = 28
n_step = 28
n_hidden = 128
n_classes = 10
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, n_step, n_input)
x_test = x_test.reshape(-1, n_step, n_input)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = keras.utils.to_categorical(y_train, n_classes)
y_test = keras.utils.to_categorical(y_test, n_classes)
model = Sequential()
model.add(LSTM(n_hidden,
batch_input_shape=(None, n_step, n_input),
unroll=True))
model.add(Dense(n_classes))
model.add(Activation('softmax'))
adam = Adam(lr=learning_rate)
model.summary()
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=training_iters,
verbose=1,
validation_data=(x_test, y_test))
scores = model.evaluate(x_test, y_test, verbose=0)
print('LSTM test score:', scores[0])
print('LSTM test accuracy:', scores[1])
def apply_quantization_to_dense(layer):
if isinstance(layer, tf.keras.layers.LSTM):
return tfmot.quantization.keras.quantize_annotate_layer(layer)
return layer
annotated_model = tf.keras.models.clone_model(
model,
clone_function=apply_quantization_to_dense,
)
我也遇到过同样的问题。为您的模型尝试以下语法:
import tf_keras as keras
model = keras.Sequential([
keras.layers.LSTM(n_hidden,
batch_input_shape=(None, n_step, n_input),
unroll=True),
keras.layers.Dense(n_classes),
keras.layers.Activation('softmax')
])
艾迪33