首页 > 解决方案 > 多层感知器的张量层动态模型如何更新模型的权重

问题描述

这是多层感知器 (MNIST) 示例的代码片段,带有张量层的动态模型。

## the following code can help you understand SGD deeply
for epoch in range(n_epoch):  ## iterate the dataset n_epoch times
    start_time = time.time()
    ## iterate over the entire training set once (shuffle the data via training)
    for X_batch, y_batch in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True):
        MLP.train()  # enable dropout
        with tf.GradientTape() as tape:
            ## compute outputs
            _logits = MLP(X_batch, foo=1)
            ## compute loss and update model
            _loss = tl.cost.cross_entropy(_logits, y_batch, name='train_loss')
        grad = tape.gradient(_loss, train_weights)
        optimizer.apply_gradients(zip(grad, train_weights))

完整的例子可以在这里看到

在此代码片段中,您可以看到神经网络的 train_weights 是如何调整的,但我不知道这些权重在 MLP 模型中的每个预测的更新位置和时间MLP(X_batch, foo=1)

标签: python-3.xtensorflowmachine-learningtensorlayer

解决方案


推荐阅读