树莓派4学习记录(3)-安装tensorflow

1. 安装tensorflow

这里可以参考这个:
https://blog.csdn.net/zqxdsy/article/details/102910840

1.1 获取tensorflow安装文件

我直接从piwheel官网得到tensorflow的安装文件:
tensorflow安装文件
安装版本:
在这里插入图片描述
当然,因为我是树莓派4,所以选取了这个armv7l.
可以直接下载,迅雷,wget都可以,如果是学校的画,可能wget更快一点,我也不知道为什么,反正前两种方法都很慢。

1.2 安装tensorflow

我选择在python3.7,系统自带的python环境中,直接安装tensorflow
在这里插入图片描述
安装方法:当然是使用pip3了,如果没有的画,建议百度直接安装pip。
这里给个传送门:
https://blog.csdn.net/zqxdsy/article/details/102910840
按照上面这个教程,即可安装好tensorflow
在安装的过程中,可能会出现依赖库下载报错,很大程度上是因为墙的问题,所以最好要么有梯子,在有梯子的地方将其下载下来,然后安装。
我目前还没有尝试换源,因为上次换成中科大的源之后出现了错误,导致安装失败。
安装成功:
在这里插入图片描述

2. 测试tensorflow

在服务器端训练一个简单的mnist模型:

# coding: utf-8
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# 定义一个占位符x
x = tf.placeholder(tf.float32, [None, 784])  # 张量的形状是[None, 784],None表第一个维度任意

# 定义变量W,b,是可以被修改的张量,用来存放机器学习模型参数
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# 实现模型, y是预测分布
y = tf.nn.softmax(tf.matmul(x, W) + b)

# 训练模型,y_是实际分布
y_ = tf.placeholder("float", [None, 10])
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))  # 交叉嫡,cost function

# 使用梯度下降来降低cost,学习速率为0.01
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

# 初始化已经创建的变量
init = tf.global_variables_initializer()

# 在一个Session中启动模型,并初始化变量
sess = tf.Session()
sess.run(init)

# 训练模型,运行1000次,每次随机抽取100个
for i in range(1, 1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

saver = tf.train.Saver()
saver.save(sess, "Model/model.ckpt")

# 验证正确率
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

然后将得到的模型移植到树莓派上,进行测试:
在这里插入图片描述
测试脚本:

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

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# 定义一个占位符x
x = tf.placeholder(tf.float32, [None, 784])  # 张量的形状是[None, 784],None表第一个维度任意

# 定义变量W,b,是可以被修改的张量,用来存放机器学习模型参数
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# 实现模型, y是预测分布
y = tf.nn.softmax(tf.matmul(x, W) + b)

# 训练模型,y_是实际分布
y_ = tf.placeholder("float", [None, 10])
# cross_entropy = -tf.reduce_sum(y_ * tf.log(y))  # 交叉嫡,cost function

saver = tf.train.Saver()
 
with tf.Session() as sess:
    saver.restore(sess, "./Model/model.ckpt")
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
    print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

运行成功:
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值