def sample_data(filepath,idx,path): f = open(filepath, 'rb') bi = pickle.load(f) data = np.arange(0, idx) data = data.tolist() samples = [] bs = [] a = path r = math.ceil(len(data)/a) for i in range(a): if len(data) > r: sample = np.random.choice(data, r, replace=False) else: sample = np.random.choice(data, len(data), replace=False) b = [bi[j] for j in list(sample)] bs.append(b) samples.append(sample) for s in sample: data.remove(s) samples = [np.array(s) for s in samples] samples = np.concatenate(samples, axis=0) bs = [np.array(e) for e in bs] bs = np.concatenate(bs, axis=0) return samples, bs请帮我调用每一次的b值并且将每一次的b值循环导入dataload里面可以用于深度学习
时间: 2024-03-20 15:40:08 浏览: 196
好的,可以按照如下方式修改函数,实现调用每一次的 b 值并将其循环导入 dataload 中,用于深度学习:
```python
import pickle
import math
import numpy as np
from torch.utils.data import DataLoader, TensorDataset
def sample_data(filepath, idx, path):
f = open(filepath, 'rb')
bi = pickle.load(f)
data = np.arange(0, idx)
data = data.tolist()
samples = []
bs = []
a = path
r = math.ceil(len(data) / a)
for i in range(a):
if len(data) > r:
sample = np.random.choice(data, r, replace=False)
else:
sample = np.random.choice(data, len(data), replace=False)
b = [bi[j] for j in list(sample)]
bs.append(b)
samples.append(sample)
for s in sample:
data.remove(s)
samples = [np.array(s) for s in samples]
samples = np.concatenate(samples, axis=0)
bs = [np.array(e) for e in bs]
bs = np.concatenate(bs, axis=0)
return samples, bs
# 调用每一次的 b 值并将其循环导入 dataload 中,用于深度学习
filepath = 'data.pkl'
idx = 100
path = 4
samples, bs = sample_data(filepath, idx, path)
batch_size = 32
dataset = TensorDataset(torch.from_numpy(samples), torch.from_numpy(bs))
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
for inputs, targets in dataloader:
# 在这里进行深度学习的操作,inputs 和 targets 分别是每一批次的数据和标签
# ...
pass
```
这样就可以调用每一次的 b 值并将其循环导入 dataload 中,用于深度学习了。
阅读全文