我创建了一个名为Tensor
的类
import numpy as np
class Tensor:
def __init__(self, data):
self.data = np.array(data)
我想使用Tensor
设置numpy数组的元素:
x = np.array([[1,2,3,4],[4,3,2,1]])
x[:,::2] = Tensor([[0,0],[1,1]])
但结果是错误ValueError: setting an array element with a sequence.
一种解决方法是检索Tensor的数据属性:x[:,::2] = Tensor([[0,0],[1,1]]).data
,但我想知道如何在不手动检索任何内容的情况下执行此操作,例如可以使用列表或numpy数组设置值:x[:,::2] = [[0,0],[1,1]]
或x[:,::2] = np.array([[0,0],[1,1]])