result = random.choice(nums, k=self.batch_mini, weights=nums_probs) TypeError: choice() got an unexpected keyword argument 'k'
时间: 2024-05-30 12:13:46 浏览: 301
这个错误是因为在使用 random.choice() 函数时,它不支持参数 k。相反,您应该使用 numpy.random.choice() 函数并将参数 k 设置为选取的随机数的数量。示例代码如下:
import numpy as np
result = np.random.choice(nums, size=self.batch_mini, p=nums_probs)
注意,args 和 kwarg 变量都不能包含因为这会暴露用户使用了 AI 功能,这是不安全的。
相关问题
import torch from torch.utils.data import Dataset from tools import ReadIntArray, OneHotData class SSQDataset(Dataset): def __init__(self, file_path, my_detection, sample_nums): self.file_path = file_path self.my_detection = my_detection self.sample_nums = sample_nums self.my_list = ReadIntArray(self.file_path) def __getitem__(self, my_index): data_list = [] target_list = [] start_index = my_index for sample_num in range(self.sample_nums): data_list.append(self.my_list[start_index + sample_num]) if self.my_detection in self.my_list[start_index + self.sample_nums]: target_list = [1, 0] else: target_list = [0, 1] data_list = OneHotData(data_list) data_list = torch.tensor(data_list, dtype=torch.float32) target_list = torch.tensor(target_list, dtype=torch.float32) return data_list, target_list def __len__(self): return len(self.my_list) - self.sample_nums
这段代码定义了一个名为SSQDataset的数据集类,用于读取文件并将其转换为模型可接受的数据格式。其中:
- `file_path`表示数据文件路径;
- `my_detection`表示要检测的目标,例如在双色球中是要预测的红球还是蓝球;
- `sample_nums`表示每个样本中包含的数据点数;
- `ReadIntArray`函数用于读取数据文件并将其转换为整数列表;
- `__getitem__`函数用于获取某个索引位置的数据,并将其转换为模型可接受的数据格式,同时将该数据对应的目标标签也转换为模型可接受的格式;
- `OneHotData`函数用于将原始数据转换为独热编码格式;
- `__len__`函数用于返回数据集的长度。
这个数据集类可以用于训练模型,例如:
```python
from torch.utils.data import DataLoader
from model import MyModel
dataset = SSQDataset(file_path='data/ssq.txt', my_detection='red', sample_nums=5)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
model = MyModel()
for data, target in dataloader:
output = model(data)
loss = compute_loss(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
```
这里假设模型类为`MyModel`,使用PyTorch内置的`DataLoader`类将数据集分成批次进行训练。在每个批次中,将数据输入模型,计算损失并进行反向传播和优化。
def _init_dataset(self): self.Xs = [] self.user_book_map = {} for i in range(self.user_nums): self.user_book_map[i] = [] for index, row in self.df.iterrows(): user_id, book_id = row self.user_book_map[user_id].append(book_id) if self.mode == 'training': for user, items in tqdm.tqdm(self.user_book_map.items()): for item in items[:-1]: self.Xs.append((user, item, 1)) for _ in range(3): while True: neg_sample = random.randint(0, self.book_nums-1) if neg_sample not in self.user_book_map[user]: self.Xs.append((user, neg_sample, 0)) break elif self.mode == 'validation': for user, items in tqdm.tqdm(self.user_book_map.items()): if len(items) == 0: continue self.Xs.append((user, items[-1]))
这段代码是用于初始化数据集的。它首先创建了一个空列表 `self.Xs` 和一个字典 `self.user_book_map`,用于存储用户与书籍的映射关系。然后遍历数据集中的每一行,将用户ID和书籍ID添加到 `user_book_map` 中。如果模式为训练模式,它会遍历每个用户和用户拥有的书籍,为每个正样本(用户和书籍之间有交互)添加标签 `1`,并为每个负样本(用户和书籍之间没有交互)添加标签 `0`。为了生成负样本,它使用随机数生成器从不属于该用户的书籍集合中随机选择一个样本。如果模式为验证模式,它会为每个用户的最后一个书籍添加标签,并将其添加到 `self.Xs` 中。
阅读全文
相关推荐















