Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用tf.GradientTape进行量化感知训练会在TensorFlow2.0中产生错误_Python 3.x_Tensorflow2.x_Quantization Aware Training - Fatal编程技术网

Python 3.x 使用tf.GradientTape进行量化感知训练会在TensorFlow2.0中产生错误

Python 3.x 使用tf.GradientTape进行量化感知训练会在TensorFlow2.0中产生错误,python-3.x,tensorflow2.x,quantization-aware-training,Python 3.x,Tensorflow2.x,Quantization Aware Training,我正在使用TensorFlow-2.2、TensorFlow_模型_优化和Python 3.8。我正在尝试量化和训练一个LeNet-300-100密集神经网络,它包含91.3375%的稀疏性。这意味着91.3375%的权重为零。我一直在遵循,我想训练这样一个稀疏网络,它是使用tf.GradientTape而不是q_aware_model.fit()量化的 如果查看,相关代码段如下: quantize_model = tfmot.quantization.keras.quantize_model

我正在使用TensorFlow-2.2、TensorFlow_模型_优化和Python 3.8。我正在尝试量化和训练一个LeNet-300-100密集神经网络,它包含91.3375%的稀疏性。这意味着91.3375%的权重为零。我一直在遵循,我想训练这样一个稀疏网络,它是使用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
quantize\u model=tfmot.quantization.keras.quantize\u model
#q_aware代表量化感知。
q_感知_模型=量化_模型(模型)
#“量化_模型”需要重新编译-
q_aware_model.compile(
优化器=tf.keras.optimizers.Adam(lr=0.0012),
损失=tf.keras.loss.categorical_交叉熵,
指标=['准确度']
)
#在此处定义“训练一步()和“测试一步()”函数-
@功能
def序列一步(模型、遮罩模型、优化器、x、y):
'''
函数计算梯度下降优化的一步
'''
使用tf.GradientTape()作为磁带:
#使用定义的模型进行预测-
y_pred=模型(x)
#计算损失-
损失=损失_fn(y,y _pred)
#计算梯度wrt定义的损失、权重和偏差-
梯度=磁带梯度(损耗、模型、可训练变量)
#类型(梯度)
#名单
#用于保存元素之间的乘法的列表-
#计算梯度和掩模-
渐变蒙版(grad_mask_mul)=[]
#在计算的渐变和遮罩之间执行元素相乘-
对于渐变层,在拉链中遮罩(渐变,遮罩模型。可训练的权重):
grad\u mask\u mul.append(tf.math.multiply(grad\u layer,mask))
#将计算的梯度应用于模型的权重和偏差-
优化器。应用梯度(zip(梯度掩码、模型、可训练变量))
#计算精度-
列车损耗(损耗)
列车精度(y,y\U pred)
一无所获
@功能
def测试步骤(模型、优化器、数据、标签):
"""
用于测试模型性能的函数
关于测试数据集
"""
预测=模型(数据)
t_损失=损失(标签、预测)
测试损耗(t损耗)
测试精度(标签、预测)
一无所获
#使用“梯度带”的列车模型-
#初始化参数以提前停止手动执行-
#最佳价值损失=100
#loc_耐心=0
对于范围内的历元(num_历元):
如果loc_耐心>=耐心:
打印(“\n'earlysting'调用!\n”)
打破
#在下一个历元开始时重置度量
列车丢失。复位状态()
列车精度。复位状态()
测试丢失。重置状态()
测试精度。重置状态()
对于列_数据集中的x,y:
训练一步(q\U感知模型、掩码模型、优化器、x、y)
对于测试数据集中的x\u t和y\u t:
测试步骤(q_感知模型、优化器、x_t、y_t)
模板='Epoch{0},损失:{1:.4f},准确度:{2:.4f},测试损失:{3:.4f},测试准确度:{4:4f}'
'''
#“i”是修剪轮数的索引-
历史[i]['accurity'][epoch]=train\u accurity.result()*100
历史[i]['loss'][epoch]=列车损失。结果()
历史[i][val\u loss'][epoch]=测试[u loss.result()
历史[i]['val\u accurity'][epoch]=测试精度。结果()*100
''' 
打印(模板格式)(
历元+1,列车丢失。结果(),
训练精度结果()*100,测试损耗结果(),
测试精度。结果()*100)
)
#计算每层中非零参数的数量和总数-
#打印(“分层方式模型,每层中非零参数的数量为:\n”)
模型和参数=0
对于中奖票\模型中的层。可训练\权重:
#打印(tf.math.count\u非零(层,轴=无).numpy())
model_sum_params+=tf.math.count_非零(layer,axis=None).numpy()
打印(“可训练参数的总数={0}\n”。格式(model_sum_params))
#手动提前停止代码:
如果np.abs(测试损耗结果()<最佳损耗值>=最小损耗:
#将“best_val_loss”变量更新为迄今为止遇到的最低损失-
最佳值损失=测试损失。结果()
#重置“loc_耐心”变量-
loc_耐心=0
否则:#监控指标“val_损失”没有改善
loc_patience+=1#没有任何改进的历代次数
给出以下错误:

---------------------------------------------------------------------------InvalidArgumentError回溯(最近的调用) 最后)在 19 20对于列_数据集中的x,y: --->21训练一步(q\U感知模型、掩码模型、优化器、x、y) 22 二十三

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py 在通话中(self,*args,**kwds) 578 xla_context.Exit() 579其他: -->580结果=自调用(*args,**kwds) 581 582如果跟踪计数==self.\u获取跟踪计数():

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in_呼叫(self,*args,**kwds) 642#提升成功,因此变量已初始化,我们可以运行 643#无状态函数。 -->644返回自我。_无状态_fn(*args,**kwds) 645其他: 646佳能args,佳能kwds=\

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py 在中,使用self调用(self,*args,**kwargs)2418。\u lock:
2419图形_函数,args,kwargs= self.\u可能\u定义\u函数(args、kwargs) ->2420返回图形\函数。\过滤\调用(args,kwargs)\ pylint:disable=protectedaccess 2421 2422@property

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py 在过滤呼叫(self、args、kwargs)1659
args
kwargs