def train_loop(): step = 0 ax = [] ay = [] plt.ion() PASS_NUM = n_epochs for pass_id in range(PASS_NUM): total_loss_pass = 0 for data in train_reader(): avg_loss_value, = exe.run( fluid.default_main_program(), feed= feeder.feed(data), fetch_list=[avg_loss]) total_loss_pass += avg_loss_value ax.append(pass_id) ay.append(total_loss_pass) plt.clf() plt.plot(ax, ay) plt.pause(0.01) plt.ioff() step += 1 display.clear_output(wait=True) fluid.io.save_inference_model(SAVE_DIRNAME, ['x'], [prediction], exe) train_loop()
时间: 2024-02-14 08:35:35 浏览: 118
这段代码定义了一个train_loop函数,用于训练模型。在函数内部,首先设置了迭代次数PASS_NUM和当前迭代次数pass_id,然后进入数据读取和训练的循环中。在每次循环中,通过train_reader函数读取一个mini-batch的训练数据,将数据送入计算图中进行前向计算和反向传播,得到平均损失值avg_loss_value。累计每个mini-batch的平均损失值,得到当前迭代次数下的总损失值total_loss_pass。将total_loss_pass和pass_id分别保存到ax和ay数组中,并使用matplotlib库将损失曲线实时显示在屏幕上。在训练完成后,使用fluid.io.save_inference_model函数将训练得到的模型保存到SAVE_DIRNAME目录中,以便后续的预测任务使用。请注意,这里的prediction是输出层的输出,即模型的预测结果。请问您还有其他问题吗?
相关问题
import numpy as np import tensorflow as tf from tensorflow import keras import matplotlib.pyplot as plt ## Let us define a plt function for simplicity def plt_loss(x,training_metric,testing_metric,ax,colors = ['b']): ax.plot(x,training_metric,'b',label = 'Train') ax.plot(x,testing_metric,'k',label = 'Test') ax.set_xlabel('Epochs') ax.set_ylabel('Accuarcy')# ax.set_ylabel('Categorical Crossentropy Loss') plt.legend() plt.grid() plt.show() tf.keras.utils.set_random_seed(1) ## We import the Minist Dataset using Keras.datasets (train_data, train_labels), (test_data, test_labels) = keras.datasets.mnist.load_data() ## We first vectorize the image (28*28) into a vector (784) train_data = train_data.reshape(train_data.shape[0],train_data.shape[1]train_data.shape[2]) # 60000784 test_data = test_data.reshape(test_data.shape[0],test_data.shape[1]test_data.shape[2]) # 10000784 ## We next change label number to a 10 dimensional vector, e.g., 1->[0,1,0,0,0,0,0,0,0,0] train_labels = keras.utils.to_categorical(train_labels,10) test_labels = keras.utils.to_categorical(test_labels,10) ## start to build a MLP model N_batch_size = 5000 N_epochs = 100 lr = 0.01 ## we build a three layer model, 784 -> 64 -> 10 MLP_4 = keras.models.Sequential([ keras.layers.Dense(128, input_shape=(784,),activation='relu'), keras.layers.Dense(64,activation='relu'), keras.layers.Dense(10,activation='softmax') ]) MLP_4.compile( optimizer=keras.optimizers.Adam(lr), loss= 'categorical_crossentropy', metrics = ['accuracy'] ) History = MLP_4.fit(train_data[:10000],train_labels[:10000], batch_size = N_batch_size, epochs = N_epochs,validation_data=(test_data,test_labels), shuffle=False) train_acc = History.history['accuracy'] test_acc = History.history['val_accuracy']在该模型中加入early stopping,使用monitor='loss', patience = 2设置代码
Sure, here is the updated code with early stopping:
```
import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
## Let us define a plt function for simplicity
def plt_loss(x,training_metric,testing_metric,ax,colors = ['b']):
ax.plot(x,training_metric,'b',label = 'Train')
ax.plot(x,testing_metric,'k',label = 'Test')
ax.set_xlabel('Epochs')
ax.set_ylabel('Accuarcy')# ax.set_ylabel('Categorical Crossentropy Loss')
plt.legend()
plt.grid()
plt.show()
tf.keras.utils.set_random_seed(1)
## We import the Minist Dataset using Keras.datasets
(train_data, train_labels), (test_data, test_labels) = keras.datasets.mnist.load_data()
## We first vectorize the image (28*28) into a vector (784)
train_data = train_data.reshape(train_data.shape[0],train_data.shape[1]*train_data.shape[2]) # 60000*784
test_data = test_data.reshape(test_data.shape[0],test_data.shape[1]*test_data.shape[2]) # 10000*784
## We next change label number to a 10 dimensional vector, e.g., 1->[0,1,0,0,0,0,0,0,0,0]
train_labels = keras.utils.to_categorical(train_labels,10)
test_labels = keras.utils.to_categorical(test_labels,10)
## start to build a MLP model
N_batch_size = 5000
N_epochs = 100
lr = 0.01
## we build a three layer model, 784 -> 64 -> 10
MLP_4 = keras.models.Sequential([
keras.layers.Dense(128, input_shape=(784,),activation='relu'),
keras.layers.Dense(64,activation='relu'),
keras.layers.Dense(10,activation='softmax')
])
MLP_4.compile(
optimizer=keras.optimizers.Adam(lr),
loss= 'categorical_crossentropy',
metrics = ['accuracy']
)
## Add early stopping
early_stop = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=2)
History = MLP_4.fit(train_data[:10000],train_labels[:10000],
batch_size = N_batch_size,
epochs = N_epochs,
validation_data=(test_data,test_labels),
shuffle=False,
callbacks=[early_stop])
train_acc = History.history['accuracy']
test_acc = History.history['val_accuracy']
```
In this code, we added the early stopping callback by creating an instance of `tf.keras.callbacks.EarlyStopping` and passing it to the `callbacks` argument of the `fit` method. We set the `monitor` parameter to `'loss'` and the `patience` parameter to `2`. This means that training will stop if the loss does not improve after 2 epochs.
import numpy as np import tensorflow as tf from tensorflow import keras import matplotlib.pyplot as plt Let us define a plt function for simplicity def plt_loss(x,training_metric,testing_metric,ax,colors = ['b']): ax.plot(x,training_metric,'b',label = 'Train') ax.plot(x,testing_metric,'k',label = 'Test') ax.set_xlabel('Epochs') ax.set_ylabel('Accuracy') plt.legend() plt.grid() plt.show() tf.keras.utils.set_random_seed(1) We import the Minist Dataset using Keras.datasets (train_data, train_labels), (test_data, test_labels) = keras.datasets.mnist.load_data() We first vectorize the image (28*28) into a vector (784) train_data = train_data.reshape(train_data.shape[0],train_data.shape[1]train_data.shape[2]) # 60000784 test_data = test_data.reshape(test_data.shape[0],test_data.shape[1]test_data.shape[2]) # 10000784 We next change label number to a 10 dimensional vector, e.g., 1-> train_labels = keras.utils.to_categorical(train_labels,10) test_labels = keras.utils.to_categorical(test_labels,10) start to build a MLP model N_batch_size = 5000 N_epochs = 100 lr = 0.01 we build a three layer model, 784 -> 64 -> 10 MLP_3 = keras.models.Sequential([ keras.layers.Dense(128, input_shape=(784,),activation='relu'), keras.layers.Dense(64, activation='relu'), keras.layers.Dense(10,activation='softmax') ]) MLP_3.compile( optimizer=keras.optimizers.Adam(lr), loss= 'categorical_crossentropy', metrics = ['accuracy'] ) History = MLP_3.fit(train_data,train_labels, batch_size = N_batch_size, epochs = N_epochs,validation_data=(test_data,test_labels), shuffle=False) train_acc = History.history['accuracy'] test_acc = History.history对于该模型,使用不同数量的训练数据(5000,10000,15000,…,60000,公差=5000的等差数列),绘制训练集和测试集准确率(纵轴)关于训练数据大小(横轴)的曲线
Sure, here's the code:
```
import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
# Define a plt function for simplicity
def plt_loss(x,training_metric,testing_metric,ax,colors = ['b']):
ax.plot(x,training_metric,'b',label = 'Train')
ax.plot(x,testing_metric,'k',label = 'Test')
ax.set_xlabel('Epochs')
ax.set_ylabel('Accuracy')
plt.legend()
plt.grid()
plt.show()
tf.keras.utils.set_random_seed(1)
# Import the MNIST Dataset using Keras.datasets
(train_data, train_labels), (test_data, test_labels) = keras.datasets.mnist.load_data()
# Vectorize the image (28*28) into a vector (784)
train_data = train_data.reshape(train_data.shape[0],train_data.shape[1]*train_data.shape[2]) # 60000*784
test_data = test_data.reshape(test_data.shape[0],test_data.shape[1]*test_data.shape[2]) # 10000*784
# Change label number to a 10 dimensional vector, e.g., 1-> [0,1,0,0,0,0,0,0,0,0]
train_labels = keras.utils.to_categorical(train_labels,10)
test_labels = keras.utils.to_categorical(test_labels,10)
# Start to build a MLP model
N_batch_size = 5000
N_epochs = 100
lr = 0.01
# Build a three layer model, 784 -> 64 -> 10
MLP_3 = keras.models.Sequential([
keras.layers.Dense(128, input_shape=(784,),activation='relu'),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10,activation='softmax')
])
MLP_3.compile(
optimizer=keras.optimizers.Adam(lr),
loss= 'categorical_crossentropy',
metrics = ['accuracy']
)
# Store the training history
train_acc = []
test_acc = []
for i in range(5000,65000,5000):
print('Training on',i,'samples')
History = MLP_3.fit(train_data[:i],train_labels[:i], batch_size = N_batch_size, epochs = N_epochs,validation_data=(test_data,test_labels), shuffle=False)
train_acc.append(History.history['accuracy'][-1])
test_acc.append(History.history['val_accuracy'][-1])
# Plot the training and testing accuracy as a function of training data size
plt.figure(figsize=(8,5))
plt.plot(range(5000,65000,5000),train_acc,'b',label = 'Train')
plt.plot(range(5000,65000,5000),test_acc,'k',label = 'Test')
plt.xlabel('Training Data Size')
plt.ylabel('Accuracy')
plt.legend()
plt.grid()
plt.show()
```
This code trains a three-layer MLP model on the MNIST dataset using Keras. It then loops through different numbers of training samples (5000, 10000, 15000, ..., 60000) and trains the model on each subset of the data for 100 epochs. After each training run, it records the final training and testing accuracy. Finally, it plots a graph of accuracy as a function of training data size.
阅读全文