基本信息
源码名称:基于python的三层神经网络模型搭建.pdf
源码大小:1.48M
文件格式:.pdf
开发语言:Python
更新时间:2020-08-04
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 6 元×
微信扫码支付:6 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
:搭建神经网络,在 mnist 数据集上训练模型,输出手写数字识别准确
率。
#给 x y_占位
x = tf.placeholder(dtype,shape)
y_ = tf.placeholder(dtype,shape)
10
#前向传播得到预测结果 y
y = mnist_forward.forward(x, None) #前向传播得到 y
#实例化可还原滑动平均的 saver
ema = tf.train.ExponentialMovingAverage(滑动衰减率)
ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore) #计算正确率 correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) while True: with tf.Session() as sess: #加载训练好的模型 ckpt = tf.train.get_checkpoint_state(存储路径) #如果已有 ckpt 模型则恢复 if ckpt and ckpt.model_checkpoint_path: #恢复会话 saver.restore(sess, ckpt.model_checkpoint_path) #恢复轮数 global_ste = ckpt.model_checkpoint_path.split ('/')[-1].split('-')[-1] #计算准确率 accuracy_score = sess.run(accuracy, feed_dict= {x:测试数据, y_:测试数据标签 }) # 打印提示 print("After %s training step(s), test accuracy= %g" % (global_step, accuracy_score)) #如果没有模型 else: print('No checkpoint file found') #模型不存在提示 return 其次,制定 main()函数 def main(): #加载测试数据集 mnist = input_data.read_data_sets("./data/", one_hot=True) #调用定义好的测试函数 test()
11
test(mnist) if __name__ == '__main__': main()
通
:搭建神经网络,在 mnist 数据集上训练模型,输出手写数字识别准确
率。
人工智能,机器学习
def test( mnist ):
with tf.Graph( ).as_default( ) as g:#给 x y_占位
x = tf.placeholder(dtype,shape)
y_ = tf.placeholder(dtype,shape)
10
#前向传播得到预测结果 y
y = mnist_forward.forward(x, None) #前向传播得到 y
#实例化可还原滑动平均的 saver
ema = tf.train.ExponentialMovingAverage(滑动衰减率)
ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore) #计算正确率 correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) while True: with tf.Session() as sess: #加载训练好的模型 ckpt = tf.train.get_checkpoint_state(存储路径) #如果已有 ckpt 模型则恢复 if ckpt and ckpt.model_checkpoint_path: #恢复会话 saver.restore(sess, ckpt.model_checkpoint_path) #恢复轮数 global_ste = ckpt.model_checkpoint_path.split ('/')[-1].split('-')[-1] #计算准确率 accuracy_score = sess.run(accuracy, feed_dict= {x:测试数据, y_:测试数据标签 }) # 打印提示 print("After %s training step(s), test accuracy= %g" % (global_step, accuracy_score)) #如果没有模型 else: print('No checkpoint file found') #模型不存在提示 return 其次,制定 main()函数 def main(): #加载测试数据集 mnist = input_data.read_data_sets("./data/", one_hot=True) #调用定义好的测试函数 test()
11
test(mnist) if __name__ == '__main__': main()
通