尝试将随机梯度朗之万动力学模型实施到数据集时出现此错误。`def SGLD_step(network, X, y, N_training, epsilon): """ 给定一个 mini-batch 运行 SGLD 的一步,并更新网络的参数。
INPUT:
network : instance of classifier network, extends `nn.Module`
X_batch : batch of inputs; torch.FloatTensor, matrix of shape = (batch_size, 2)
y_batch : batch of targets: torch.FloatTensor, vector of shape = (batch_size,)
N_training : total number of training data instances in the full training set
epsilon : step size / learning rate parameters (scalar)
OUTPUT:
loss : loss on the mini-batch
"""
# 1. Sample a random mini-batch of indices
idx = torch.randperm(N_training)[:len(X)]
# 2. Sample a random mini-batch of data instances
X_batch = X[idx]
y_batch = y[idx]
# 3. Compute the gradient of the loss w.r.t. the network parameters
loss = -log_joint_minibatch(network, X_batch, y_batch, N_training)
# 4. Update the network parameters
network.weight.data -= epsilon * network.weight.grad
network.bias.data -= epsilon * network.bias.grad
# 5. Return the loss
return loss`
样本 = draw_sgld_samples(网络,N_samples=100,N_steps_per_sample=200)