tensorfow使用基础(3)--MNiST--1

博客围绕TensorFlow基础展开,聚焦MNIST相关内容,虽未给出具体内容,但推测会涉及利用TensorFlow处理MNIST数据集的技术要点,如数据处理、模型构建等信息技术相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import tensorflow as tf
import tensorflow.examples.tutorials.mnist as mnist

#导入数据
data = mnist.input_data.read_data_sets("MNIST_data", one_hot=True)
batch_size = 100
n_batch = data.train.num_examples // batch_size

"""
构建网络结构,其结构为[784, 10]
"""
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])

w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

z = tf.matmul(x, w) + b
z_plus = tf.nn.tanh(z)

loss = tf.reduce_mean(tf.square(z_plus - y))
train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(z_plus, 1))#argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for epochs in range(20):
        for i in range(n_batch):
            batch_x, batch_y = data.train.next_batch(batch_size=batch_size)
            sess.run(train, feed_dict={x:batch_x,y:batch_y})
        acc = sess.run(accuracy, feed_dict={x: data.test.images, y: data.test.labels})
        print("Iter " + str(epochs) + ",Testing Accuracy " + str(acc))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值