首页 > 解决方案 > “避免覆盖输入张量”是什么意思?

问题描述

我一直在研究 Francois Chollet 的书 Deep Learning with python,并且有一个操作,他总是在他的一些函数中不断重复。

x = x.copy()

书中说这样做是为了“避免覆盖输入张量”

我将在这里发布一个示例代码,他使用此语句。

def naive_add(x, y):
    assert len(x.shape) == 2
    assert x.shape == y.shape

    x = x.copy()                         #Avoid overwriting the input tensor.
    for i in range(x.shape[0]):
        for j in range(x.shape[1]):
            x[i, j] += y[i, j]
    return x

这是两个二维张量的元素加法的简单代码。看懂了代码,但是想了解一下x=x.copy这个函数中这行的意义。

标签: pythonpython-3.xnumpy

解决方案


推荐阅读