我正在使用 TensorFlow-2.2、tensorflow_model_optimization 和 Python 3.8。我正在尝试量化和训练一个包含 91.3375% 稀疏度的 LeNet-300-100 Dense 神经网络。这意味着 91.3375% 的权重为零。我正在关注Quantization TF 教程,我想训练一个使用tf.GradientTape而不是q_aware_model.fit()量化的稀疏网络。
如果您查看示例代码,相关的代码片段是:
quantize_model = tfmot.quantization.keras.quantize_model
# q_aware stands for for quantization aware.
q_aware_model = quantize_model(model)
# 'quantize_model' requires recompilation-
q_aware_model.compile(
optimizer = tf.keras.optimizers.Adam(lr = 0.0012),
loss=tf.keras.losses.categorical_crossentropy,
metrics=['accuracy']
)
# Define 'train_one_step()' and 'test_step()' functions here-
@tf.function
def train_one_step(model, mask_model, optimizer, x, y):
'''
Function to compute one step of gradient descent optimization
'''
with tf.GradientTape() as tape:
# Make predictions using defined model-
y_pred = model(x)
# Compute loss-
loss = loss_fn(y, y_pred)
# Compute gradients wrt defined loss and weights and biases-
grads = tape.gradient(loss, model.trainable_variables)
# type(grads)
# list
# List to hold element-wise multiplication between-
# computed gradient and masks-
grad_mask_mul = []
# Perform element-wise multiplication between computed gradients and masks-
for grad_layer, mask in zip(grads, mask_model.trainable_weights):
grad_mask_mul.append(tf.math.multiply(grad_layer, mask))
# Apply computed gradients to model's weights and biases-
optimizer.apply_gradients(zip(grad_mask_mul, model.trainable_variables))
# Compute accuracy-
train_loss(loss)
train_accuracy(y, y_pred)
return None
@tf.function
def test_step(model, optimizer, data, labels):
"""
Function to test model performance
on testing dataset
"""
predictions = model(data)
t_loss = loss_fn(labels, predictions)
test_loss(t_loss)
test_accuracy(labels, predictions)
return None
# Train model using 'GradientTape'-
# Initialize parameters for Early Stopping manual implementation-
# best_val_loss = 100
# loc_patience = 0
for epoch in range(num_epochs):
if loc_patience >= patience:
print("\n'EarlyStopping' called!\n")
break
# Reset the metrics at the start of the next epoch
train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()
for x, y in train_dataset:
train_one_step(q_aware_model, mask_model, optimizer, x, y)
for x_t, y_t in test_dataset:
test_step(q_aware_model, optimizer, x_t, y_t)
template = 'Epoch {0}, Loss: {1:.4f}, Accuracy: {2:.4f}, Test Loss: {3:.4f}, Test Accuracy: {4:4f}'
'''
# 'i' is the index for number of pruning rounds-
history_main[i]['accuracy'][epoch] = train_accuracy.result() * 100
history_main[i]['loss'][epoch] = train_loss.result()
history_main[i]['val_loss'][epoch] = test_loss.result()
history_main[i]['val_accuracy'][epoch] = test_accuracy.result() * 100
'''
print(template.format(
epoch + 1, train_loss.result(),
train_accuracy.result()*100, test_loss.result(),
test_accuracy.result()*100)
)
# Count number of non-zero parameters in each layer and in total-
# print("layer-wise manner model, number of nonzero parameters in each layer are: \n")
model_sum_params = 0
for layer in winning_ticket_model.trainable_weights:
# print(tf.math.count_nonzero(layer, axis = None).numpy())
model_sum_params += tf.math.count_nonzero(layer, axis = None).numpy()
print("Total number of trainable parameters = {0}\n".format(model_sum_params))
# Code for manual Early Stopping:
if np.abs(test_loss.result() < best_val_loss) >= minimum_delta:
# update 'best_val_loss' variable to lowest loss encountered so far-
best_val_loss = test_loss.result()
# reset 'loc_patience' variable-
loc_patience = 0
else: # there is no improvement in monitored metric 'val_loss'
loc_patience += 1 # number of epochs without any improvement
给出以下错误:
-------------------------------------------------- ------------------------- InvalidArgumentError Traceback (last last call last) in 19 20 for x, y in train_dataset: ---> 21 train_one_step( q_aware_model、mask_model、优化器、x、y) 22 23
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in call (self, *args, **kwds) 578 xla_context.Exit() 579 else: --> 580 结果= self._call(*args, **kwds) 581 582 if tracking_count == self._get_tracing_count():
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds) 642 # 提升成功,所以变量被初始化,我们可以运行643 # 无状态函数。--> 644 return self._stateless_fn(*args, **kwds) 645 else: 646 canon_args, canon_kwds = \
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in call (self, *args, **kwargs) 2418 with self._lock:
2419 graph_function, args, kwargs = self ._maybe_define_function(args, kwargs) -> 2420 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access 2421 2422 @property~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py 在 _filtered_call(self, args, kwargs) 1659
args
和kwargs
. 1660 """ -> 1661 return self._call_flat( 1662 (t for t in nest.flatten((args, kwargs), expand_composites=True) 1663
if isinstance(t, (ops.Tensor,~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _call_flat(self, args, capture_inputs, canceling_manager)
1743 and execution_eagerly): 1744 # 没有磁带正在观看;跳到运行函数。-> 1745 返回 self._build_call_outputs(self._inference_function.call(1746
ctx,args,cancellation_manager=cancellation_manager))1747
forward_backward = self._select_forward_and_backward_functions(~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in call(self, ctx, args, cancel_manager) 591 with _InterpolateFunctionError(self): 592 if cancel_manager 是 None: -- > 593 个输出 = execute.execute(594 str(self.signature.name),595 num_outputs=self._num_outputs,
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 57 try: 58 ctx.ensure_initialized() -- -> 59 张量 = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, 60 个输入, attrs, num_outputs) 61 除了 core._NotOkStatusException 为 e:
InvalidArgumentError:var 和 grad 的形状不同[10] [100,10] [[节点 Adam/Adam/update_4/ResourceApplyAdam(定义于:29)]] [Op:__inference_train_one_step_20360]
错误可能源于输入操作。连接到节点 Adam/Adam/update_4/ResourceApplyAdam 的输入源操作:Mul_4(定义于 :26)
sequential/quant_dense_2/BiasAdd/ReadVariableOp/resource(定义于 /home/arjun/.local/lib/python3.8/site-packages /tensorflow_model_optimization/python/core/quantization/keras/quantize_wrapper.py:162)函数调用栈:train_one_step
有没有办法将 TF 模型量化与 tf.GradientTape 结合起来?
谢谢!