def GetExpectGainData(inputDataSet): expectGainData = torch.zeros(inputDataSet.shape[0]) if not IsMatrix(inputDataSet): print('GetExpectGainData inputDataSet is not a matrix') return expectGainData trainDateArray = GetTrainDateArray(); trainData = [] for trainDate in trainDateArray: currentTrainData = MergeTargetValByCol(inputDataSet, 0, trainDate) trainData = torch.cat((trainData, currentTrainData), dim = 0) labelData = trainData[:, 2] labelData = labelData[1, :] newDayData = inputDataSet[torch.LongTensor([array1.shape[0]]) - 1] labelData = torch.cat(labelData, newDayData[2]) print(trainData) print(labelData) 000004.csv Traceback (most recent call last): File "D:\BlackOp\Source\QuantitativeTrade\Source\DataAnalyze.py", line 97, in <module> File "D:\BlackOp\Source\QuantitativeTrade\Source\DataAnalyze.py", line 39, in CalAnalyzeRst expectGainData = GetExpectGainData(inputDataSet) File "D:\BlackOp\Source\QuantitativeTrade\Source\DataAnalyze.py", line 77, in GetExpectGainData trainData = torch.cat((trainData, currentTrainData), dim = 0) TypeError: expected Tensor as element 0 in argument 0, but got list
时间: 2024-04-22 08:28:07 浏览: 267
pytorch:torch.mm()和torch.matmul()的使用
根据您提供的代码,出现了一个错误。错误提示是`TypeError: expected Tensor as element 0 in argument 0, but got list`,这意味着在使用torch.cat()函数时,传入了一个列表而不是张量。
请注意,在使用torch.cat()函数时,传入的参数应为张量。根据您的代码,`trainData`被初始化为一个空列表,而不是一个张量。为了解决这个问题,您可以将`trainData`初始化为一个空的张量,然后使用torch.cat()函数将新的训练数据添加到其中。
修改后的代码如下所示:
```python
def GetExpectGainData(inputDataSet):
expectGainData = torch.zeros(inputDataSet.shape[0])
if not IsMatrix(inputDataSet):
print('GetExpectGainData inputDataSet is not a matrix')
return expectGainData
trainDateArray = GetTrainDateArray()
trainData = torch.tensor([]) # 初始化为一个空张量
for trainDate in trainDateArray:
currentTrainData = MergeTargetValByCol(inputDataSet, 0, trainDate)
trainData = torch.cat((trainData, currentTrainData), dim=0)
labelData = trainData[:, 2]
labelData = labelData[1, :]
newDayData = inputDataSet[torch.LongTensor([array1.shape[0]]) - 1]
labelData = torch.cat((labelData, newDayData[2])) # 将newDayData[2]添加到labelData中
print(trainData)
print(labelData)
```
这样修改后,`trainData`将会被正确地初始化为一个空张量,并且可以使用torch.cat()函数将新的训练数据添加到其中。同样,我还在最后一行代码中修复了另一个错误,将`torch.cat(labelData, newDayData[2])`修改为`torch.cat((labelData, newDayData[2]))`来正确地将`newDayData[2]`添加到`labelData`中。
希望这可以解决您遇到的问题。如果还有其他问题,请随时提问。
阅读全文