【发布时间】:2021-05-20 03:53:11
【问题描述】:
尝试使用numpy 生成大量数据但没有成功。需要明确的是,这不是库的错——我的电脑内存不足~ 32gb 的内存。
有没有更好的方法来做到这一点,例如同时保存在磁盘上而不是内存中?
这是工作代码:
import numpy as np
import tensorflow as tf
def main():
(images, labels), (_, _) = tf.keras.datasets.mnist.load_data(path="mnist.npz")
rescaled_images = np.reshape(images, (images.shape[0], images.shape[1] * images.shape[1]))
indices = np.where(labels==0)
zero_images = np.take(rescaled_images, indices, axis=0)[0]
x0 = np.array(zero_images).astype(np.float32)
length = zero_images.shape[1]
ones = np.ones(length)
steps = 1000000 # <--- problem even with one step
mu = -2.0
sigma = 1.5
dt = 0.01
data = []
for image in x0:
diff_imgs = [image]
x = x0
for i in range(steps):
x = x + mu * ones * dt + sigma * ones * np.sqrt(dt) * np.random.randn(length)
diff_imgs.append(x)
data.append(diff_imgs)
data = np.array(data, dtype=np.float32)
if __name__ == '__main__':
main()
【问题讨论】:
标签: python numpy tensorflow