import tensorflow as tf ##import input_data from tensorflow.examples.tutorials.mnist import input_data ##下载导入mnist数据集 如果出现连接问题 请连vpn mnist = input_data.read_data_sets('data/', one_hot=True) ##part1 初始化参数 x = tf.placeholder('float', [None, 784]) y = tf.placeholder('float', [None, 10]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) ##part2 定义函数 actv = tf.nn.softmax(tf.matmul(x, W) + b) cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(actv), reduction_indices=1)) learning_rate = 0.01 optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) pred = tf.equal(tf.arg_max(actv, 1), tf.arg_max(y, 1)) accr=tf.reduce_mean(tf.cast(pred,'float')) ##part3 迭代运算 training_epochs=50 batch_size=100 display_step=5 sess=tf.Session() init=tf.global_variables_initializer() sess.run(init) for epoch in range(1,(training_epochs+1)): ##[1~50] avg_cost=0 num_batch=int(mnist.train.num_examples/batch_size) for i in range(num_batch): batch_xs,batch_ys=mnist.train.next_batch(batch_size) feeds = {x: batch_xs, y: batch_ys} sess.run(optm,feed_dict=feeds) avg_cost+=sess.run(cost,feed_dict=feeds)/num_batch if epoch%display_step==0: ##feeds_train={x:batch_xs,y:batch_ys} feeds_test = {x: mnist.test.images, y: mnist.test.labels} train_acc=sess.run(accr,feed_dict=feeds) test_acc=sess.run(accr,feed_dict=feeds_test) print("Epoch:%03d/%03d cost:%.9f train_acc:%.3f test_acc:%.3f"%(epoch,training_epochs,avg_cost,train_acc,test_acc))
Tensorflow实现逻辑回归模型
最新推荐文章于 2025-10-30 17:29:10 发布
本文介绍了一个使用TensorFlow实现的手写数字识别模型。通过加载MNIST数据集,并利用softmax回归进行训练,该模型能够在50个周期内达到较高的识别准确率。文中详细展示了从数据准备到模型训练和评估的全过程。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
TensorFlow-v2.15
TensorFlow
TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型
452

被折叠的 条评论
为什么被折叠?



