ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 471|回复: 2

[求助] 求老师给这个python代码纠错

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-3-24 00:01 | 显示全部楼层 |阅读模式
```pythonimport numpy as npimport tensorflow as tf# 定义网络结构input_size = 784hidden_size = 256output_size = 10x = tf.placeholder(tf.float32, [None, input_size])y = tf.placeholder(tf.float32, [None, output_size])W1 = tf.Variable(tf.random_normal([input_size, hidden_size]))b1 = tf.Variable(tf.random_normal([hidden_size]))W2 = tf.Variable(tf.random_normal([hidden_size, output_size]))b2 = tf.Variable(tf.random_normal([output_size]))# 定义前向传播和损失函数hidden_layer = tf.nn.relu(tf.matmul(x, W1) + b1)output_layer = tf.matmul(hidden_layer, W2) + b2loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output_layer, labels=y))# 定义反向传播算法optimizer = tf.train.AdamOptimizer(learning_rate=0.01)train_step = optimizer.minimize(loss)# 加载数据from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets('MNIST_data', one_hot=True)# 训练模型sess = tf.Session()sess.run(tf.global_variables_initializer())for i in range(1000):    batch_xs, batch_ys = mnist.train.next_batch(100)    sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})# 验证模型correct_prediction = tf.equal(tf.argmax(output_layer, axis=1), tf.argmax(y, axis=1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))print(sess.run(accuracy, feed_dict={x: mnist.validation.images, y: mnist.validation.labels}))# 测试模型print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))```


我的版本3.10.4

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-3-24 00:02 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
我的版本3.10.4

TA的精华主题

TA的得分主题

发表于 2023-3-24 08:51 | 显示全部楼层
以下回复您看是否对您有帮助
1. 缺少导入`input_data`模块,应该添加`from tensorflow.examples.tutorials.mnist import input_data`。

2. 在定义网络结构时,`output_size`应该为10,而不是1。

3. 在定义损失函数时,应该使用`tf.nn.softmax_cross_entropy_with_logits`函数,而不是`tf.nn.sparse_softmax_cross_entropy_with_logits`函数。

4. 在训练模型时,应该使用`mnist.train.next_batch`方法获取批量数据。

5. 在验证模型和测试模型时,应该使用`mnist.validation.images`和`mnist.test.images`作为输入数据。

修改后的代码如下所示:

```python
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# 定义网络结构
input_size = 784
hidden_size = 256
output_size = 10

x = tf.placeholder(tf.float32, [None, input_size])
y = tf.placeholder(tf.float32, [None, output_size])

W1 = tf.Variable(tf.random_normal([input_size, hidden_size]))
b1 = tf.Variable(tf.random_normal([hidden_size]))
W2 = tf.Variable(tf.random_normal([hidden_size, output_size]))
b2 = tf.Variable(tf.random_normal([output_size]))

# 定义前向传播和损失函数
hidden_layer = tf.nn.relu(tf.matmul(x, W1) + b1)
output_layer = tf.matmul(hidden_layer, W2) + b2
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output_layer, labels=y))

# 定义反向传播算法
optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
train_step = optimizer.minimize(loss)

# 加载数据
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# 训练模型
sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})

# 验证模型
correct_prediction = tf.equal(tf.argmax(output_layer, axis=1), tf.argmax(y, axis=1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.validation.images, y: mnist.validation.labels}))

# 测试模型
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))
```
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-29 18:25 , Processed in 0.040474 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表