在 model()中加以下两行代码,文件会保存在当前代码的目录
saver = tf.compat.v1.train.Saver()
saver.save(sess, 'model/my-model', global_step=epoch+1)
def model(X_train,Y_train,
learning_rate=0.0001,num_epoches=1500,minibatch_size=32,
print_cost=True,is_plot=True):
"""
实现一个三层的Tensorflow神经网络:LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX
参数:
X_train - 训练集,维度为(输入大小(输入节点数量) = 12288, 样本数量 = 1080)
Y_train - 训练集分类数量,维度为(输出大小(输出节点数量) = 6, 样本数量 = 1080)
X_test - 测试集,维度为(输入大小(输入节点数量) = 12288, 样本数量 = 120)
Y_test - 测试集分类数量,维度为(输出大小(输出节点数量) = 6, 样本数量 = 120)
learning_rate - 学习速率
num_epochs - 整个训练集的遍历次数
mini_batch_size - 每个小批量数据集的大小
print_cost - 是否打印成本,每100代打印一次
is_plot - 是否绘制曲线图
返回:
parameters - 学习后的参数
"""
ops.reset_default_graph()
tf.compat.v1.set_random_seed(1)
seed = 3
(n_x, m) = X_train.shape
n_y = Y_train.shape[0]
costs = []
X,Y = create_placeholders(n_x,n_y)
parameters = initialize_parameters()
Z3 = forward_propagation(X,parameters)
cost = compute_cost(Z3,Y)
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
init = tf.global_variables_initializer()
saver = tf.compat.v1.train.Saver()
with tf.compat.v1.Session() as sess:
sess.run(init)
for epoch in range(num_epoches):
epoch_cost = 0
num_minibatches = int(m / minibatch_size)
seed = seed + 1
minibatches = tf_utils.random_mini_batches(X_train,Y_train,minibatch_size,seed)
for minibatch in minibatches:
(minibatch_X,minibatch_Y) = minibatch
_,minibatch_cost = sess.run([optimizer,cost],feed_dict={X:minibatch_X,Y:minibatch_Y})
epoch_cost = epoch_cost + minibatch_cost / num_minibatches
if (epoch+1) % 5 == 0:
costs.append(epoch_cost)
if print_cost and (epoch+1) % 100 == 0:
print("epoch = " + str(epoch+1) + " epoch_cost = " + str(epoch_cost))
saver.save(sess, 'model/my-model', global_step=epoch+1)
if is_plot:
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
parameters = sess.run(parameters)
print("参数已经保存到session.")
correct_prediction = tf.math.equal(tf.math.argmax(Z3),tf.math.argmax(Y))
accuracy = tf.math.reduce_mean(tf.cast(correct_prediction,"float"))
print("训练集的准确率: ",accuracy.eval({X:X_train,Y:Y_train}))
预测模块
import tf_utils
import tensorflow as tf
X_train_orig , Y_train_orig , X_test_orig , Y_test_orig , classes = tf_utils.load_dataset()
X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0],-1).T
X_test = tf.cast(X_test_flatten / 255,tf.float32)
Y_test = tf_utils.convert_to_one_hot(Y_test_orig,6)
print("测试集样本数 = " + str(X_test.shape[1]))
print("X_test.shape: " + str(X_test.shape))
print("Y_test.shape: " + str(Y_test.shape))
def forward_propagation(X,parameters):
"""
实现一个模型的前向传播,模型结构为LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX
参数:
X - 输入数据的占位符,维度为(输入节点数量,样本数量)
parameters - 包含了W和b参数的字典
返回:
Z3 - 最后一个LINEAR节点的输出
"""
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
W3 = parameters['W3']
b3 = parameters['b3']
Z1 = tf.math.add(tf.linalg.matmul(W1,X),b1)
A1 = tf.nn.relu(Z1)
Z2 = tf.math.add(tf.linalg.matmul(W2,A1),b2)
A2 = tf.nn.relu(Z2)
Z3 = tf.math.add(tf.linalg.matmul(W3,A2),b3)
return Z3
def load_model(filepath):
with tf.compat.v1.Session() as sess:
saver =tf.compat.v1.train.import_meta_graph(filepath)
saver.restore(sess, tf.train.latest_checkpoint("model"))
W1 = sess.run('W1:0')
b1 = sess.run('b1:0')
W2 = sess.run('W2:0')
b2 = sess.run('b2:0')
W3 = sess.run('W3:0')
b3 = sess.run('b3:0')
parameters = {'W1':W1,
'b1':b1,
'W2':W2,
'b2':b2,
'W3':W3,
'b3':b3}
return parameters
parameters = load_model('model/my-model-1500.meta');
Z3 = forward_propagation(X_test,parameters)
correct_prediction = tf.math.equal(tf.math.argmax(Z3),tf.math.argmax(Y_test))
accuracy = tf.math.reduce_mean(tf.cast(correct_prediction,"float"))
with tf.compat.v1.Session() as sess:
print("测试集的准确率: " + str(sess.run(accuracy)))