Conv2D 模型训练卡住 [MNIST 数据集]
Training of Conv2D model stuck [MNIST dataset]
作为一个更大项目的一部分,我正在编写一个小型卷积二维模型来在 MNIST 数据集上训练神经网络。
我的(经典)工作流程如下:
- 加载数据集并将其转换为
np array
- 将数据集拆分为训练集和验证集
- 重塑 (
X_train.reshape(X.shape[0], 28, 28, 1)
) 和 one_hot_encode (keras.utils.to_categorical(y_train, 10)
)
- 获取模型
- 根据数据训练,并保存
我的训练函数定义如下:
def train(model, X_train, y_train, X_val, y_val):
model.fit_generator(
generator=get_next_batch(X_train, y_train),
steps_per_epoch=200,
epochs=EPOCHS,
validation_data=get_next_batch(X_val, y_val),
validation_steps=len(X_val)
)
return model
我使用的生成器:
def get_next_batch(X, y):
# Will contains images and labels
X_batch = np.zeros((BATCH_SIZE, 28, 28, 1))
y_batch = np.zeros((BATCH_SIZE, 10))
while True:
for i in range(0, BATCH_SIZE):
random_index = np.random.randint(len(X))
X_batch[i] = X[random_index]
y_batch[i] = y[random_index]
yield X_batch, y_batch
现在,它按原样进行训练,但在最后几步挂起:
Using TensorFlow backend.
Epoch 1/3
2018-04-18 19:25:08.170609: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
199/200 [============================>.] - ETA: 0s - loss:
而如果我不使用任何生成器:
def train(model, X_train, y_train, X_val, y_val):
model.fit(
X_train,
y_train,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
verbose=1,
validation_data=(X_val, y_val)
)
return model
效果很好。
显然我的方法 get_next_batch
做错了什么,但我不明白为什么。
我们非常欢迎任何帮助!
问题是您在生成器函数中创建了一个巨大的验证集。查看这些参数传递到哪里...
validation_data=get_next_batch(X_val, y_val),
validation_steps=len(X_val)
假设您的 BATCH_SIZE 是 1,000。所以你拉取了 1,000 张图像,运行 通过它们 1,000 次。
所以 1,000 x 1,000 = 1,000,000。这就是通过您的网络 运行 的图像数量,这将需要很长时间。您可以将步骤更改为评论中提到的静态数字,我只是认为解释有助于正确看待它。
作为一个更大项目的一部分,我正在编写一个小型卷积二维模型来在 MNIST 数据集上训练神经网络。
我的(经典)工作流程如下:
- 加载数据集并将其转换为
np array
- 将数据集拆分为训练集和验证集
- 重塑 (
X_train.reshape(X.shape[0], 28, 28, 1)
) 和 one_hot_encode (keras.utils.to_categorical(y_train, 10)
) - 获取模型
- 根据数据训练,并保存
我的训练函数定义如下:
def train(model, X_train, y_train, X_val, y_val):
model.fit_generator(
generator=get_next_batch(X_train, y_train),
steps_per_epoch=200,
epochs=EPOCHS,
validation_data=get_next_batch(X_val, y_val),
validation_steps=len(X_val)
)
return model
我使用的生成器:
def get_next_batch(X, y):
# Will contains images and labels
X_batch = np.zeros((BATCH_SIZE, 28, 28, 1))
y_batch = np.zeros((BATCH_SIZE, 10))
while True:
for i in range(0, BATCH_SIZE):
random_index = np.random.randint(len(X))
X_batch[i] = X[random_index]
y_batch[i] = y[random_index]
yield X_batch, y_batch
现在,它按原样进行训练,但在最后几步挂起:
Using TensorFlow backend.
Epoch 1/3
2018-04-18 19:25:08.170609: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
199/200 [============================>.] - ETA: 0s - loss:
而如果我不使用任何生成器:
def train(model, X_train, y_train, X_val, y_val):
model.fit(
X_train,
y_train,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
verbose=1,
validation_data=(X_val, y_val)
)
return model
效果很好。
显然我的方法 get_next_batch
做错了什么,但我不明白为什么。
我们非常欢迎任何帮助!
问题是您在生成器函数中创建了一个巨大的验证集。查看这些参数传递到哪里...
validation_data=get_next_batch(X_val, y_val),
validation_steps=len(X_val)
假设您的 BATCH_SIZE 是 1,000。所以你拉取了 1,000 张图像,运行 通过它们 1,000 次。
所以 1,000 x 1,000 = 1,000,000。这就是通过您的网络 运行 的图像数量,这将需要很长时间。您可以将步骤更改为评论中提到的静态数字,我只是认为解释有助于正确看待它。