我在 Sagemaker 中运行笔记本,并使用 .py 文件进行培训:
tf_estimator = TensorFlow(entry_point='train_cnn.py',
role=role,
train_instance_count=1,
train_instance_type='local', #We use the local instance
framework_version='1.12',
py_version='py3',
script_mode=True,
hyperparameters={'epochs': 1} #One epoch just to check everything is ok
)
#We fit the model with the data
tf_estimator.fit({'training': training_input_path, 'validation': validation_input_path})
在 train_cnn 文件中,我使用标准 CNN。但是,文件的最后一部分表示要像这样绘制训练的历史记录:
model.compile(loss=tensorflow.keras.losses.binary_crossentropy,
optimizer=Adam(lr=lr),
metrics=['accuracy'])
snn=model.fit(train_images, train_labels, batch_size=batch_size,
validation_data=(test_images, test_labels),
epochs=epochs,
verbose=2)
score = model.evaluate(test_images, test_labels, verbose=0)
print('Validation loss :', score[0])
print('Validation accuracy:', score[1])
plt.figure(0)
plt.plot(snn.history['acc'], 'r')
plt.plot(snn.history['val_acc'], 'g')
plt.xticks(np.arange(0, 11, 2.0))
plt.rcParams['figure.figsize'] = (8, 6)
plt.xlabel("Num of Epochs")
plt.ylabel("Accuracy")
plt.title("Training Accuracy")
plt.legend(['train', 'validation'])
plt.figure(1)
plt.plot(snn.history['loss'],'r')
plt.plot(snn.history['val_loss'],'g')
plt.xticks(np.arange(0, 11, 2.0))
plt.rcParams['figure.figsize'] = (8, 6)
plt.xlabel("Num of Epochs")
plt.ylabel("Loss")
plt.title("Training Loss vs Validation Loss")
plt.legend(['train','validation'])
plt.show()
但是,没有显示任何内容,并且训练显示成功。也许是因为在另一个实例中执行。这里显示的信息:
Epoch 1/1
algo-1-tn2vd_1 | - 2s - loss: 0.8858 - acc: 0.4615 - val_loss: 3.0894 - val_acc: 0.5000
algo-1-tn2vd_1 | Validation loss : 3.0894343852996826
algo-1-tn2vd_1 | Validation accuracy: 0.5
algo-1-tn2vd_1 | WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/simple_save.py:85: calling SavedModelBuilder.add_meta_graph_and_variables (from tensorflow.python.saved_model.builder_impl) with legacy_init_op is deprecated and will be removed in a future version.
algo-1-tn2vd_1 | Instructions for updating:
algo-1-tn2vd_1 | Pass your op to the equivalent parameter main_op instead.
algo-1-tn2vd_1 | 2020-07-12 00:42:23,538 sagemaker-containers INFO Reporting training SUCCESS
tmpuzys_qpc_algo-1-tn2vd_1 exited with code 0
Aborting on container exit...
===== Job Complete =====