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里面可以用于深度学习
时间: 2024-03-20 20:40:05 浏览: 61
好的,可以按照如下方式修改函数,实现输出每一次的b值并返回可以用于深度学习的数据:
```python
import pickle
import math
import numpy as np
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)
print("b值为:", b) # 输出每一次的b值
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
# 导入dataload里面可以用于深度学习
filepath = 'data.pkl'
idx = 100
path = 4
samples, bs = sample_data(filepath, idx, path)
```
这样就可以输出每一次的b值,并返回可以用于深度学习的数据了。
阅读全文