我尝试设置批量大小并运行Autoencoder程序,因为没有足够的内存来使用完整批次。所以我尝试使用tf.train.batch
。但由于函数的参数是张量,我试图将np数组转换为tf.convert_to_tensor
的张量。但内存超过2GB,无法改变为张量。我如何用小批量训练?
下面是我的代码。
N_img=47000000
batch_size=100
X_train = np.zeros(shape=(N_img, Freq_LEN, LOOK_LEN, 1), dtype='float32')
x = tf.placeholder(tf.float32, [None, FRM_LEN/2,FRM_LEN/2,1]) #FRM_LEN=256
y = tf.placeholder(tf.float32, [None, FRM_LEN/2,FRM_LEN/2,1])
X_train=tf.convert_to_tensor(X_train)
X_train_batch= tf.train.batch(X_train,batch_size=batch_size)
print("Start training..")
for step in range(n_iters):
sess.run(optm, feed_dict={x: X_train_batch, y: X_train_batch, keepprob: 0.7})
if step % 100 == 0:
print(step,sess.run(cost, feed_dict={x: X_train_batch, y: X_train_batch, keepprob: 1}))
print("finish training")
答案 0 :(得分:0)
尝试创建一个不需要张量参数的自定义generate_batch
函数(以避免tf.convert_to_tensor操作),例如:
import numpy as np
batch_size = 100
X_train = np.zeros(shape=(N_img, Freq_LEN, LOOK_LEN, 1), dtype='float32')
y_train = np.zeros(shape=(N_img, Freq_LEN, LOOK_LEN, 1), dtype='float32')
data_index = 0
def generate_batch(batch_size):
global data_index
batch = np.ndarray(shape=(batch_size, Freq_LEN, LOOK_LEN, 1), dtype=np.float32) #the same shapes as train data
labels = np.ndarray(shape=(batch_size, Freq_LEN, LOOK_LEN, 1), dtype=np.float32)
for i in range(batch_size):
batch[i] = X_train[data_index]
labels[i] = y_train[data_index]
data_index = (data_index + 1) % len(X_train)
return batch, labels
for step in range(n_iters):
X_train_batch, X_train_batch = generate_batch(batch_size)
sess.run(optm, feed_dict={x: X_train_batch, y: X_train_batch, keepprob: 0.7})
if step % 100 == 0:
print(step,sess.run(cost, feed_dict={x: X_train_batch, y: X_train_batch, keepprob: 1}))
答案 1 :(得分:0)
强制您的批处理在CPU上进行:
....
with tf.device('/cpu:0'):
x = tf.placeholder(tf.float32, [None, FRM_LEN/2,FRM_LEN/2,1]) #FRM_LEN=256
y = tf.placeholder(tf.float32, [None, FRM_LEN/2,FRM_LEN/2,1])
X_train=tf.convert_to_tensor(X_train)
X_train_batch= tf.train.batch(X_train,batch_size=batch_size)
print("Start training..")
....