解决报错ValueError: not enough values to unpack (expected 2, got 1)

ValueError: not enough values to unpack (expected 2, got 1)报错!

问题描述:

在进行神经网络实验时,对输入的数据进行归一化处理时,
出现:
ValueError: not enough values to unpack (expected 2, got 1)
错误。

问题原因:

输入到归一化函数时,因为输入数据只有一列。

def batch_norm(t: torch.Tensor):
    (batch, length) = t.shape
    for line in range(batch):
        t[line] = normalize(t[line])
    return t

在数据输入时,没有足够的值来解包(需要2,得到1)所以报错。
我的理解是,一维数据维度不够。
于是我扩了一下维度。

# y = torch.unsqueeze (y, 1)  # 维度扩张

结果还是不行,
最后我直接把整个数据直接带入:

def batch_norm1(t: torch.Tensor):
    mean = torch.mean (t)
    std = torch.std (t)  # 计算标准差
    t =  (t - mean) / std
    return t

结果运行成功了。

你可能感兴趣的:(python,深度学习,pytorch)