【深度学习笔记】(二)Hello, Tensorflow!

本文介绍如何通过Docker快速安装配置TensorFlow环境,并提供了一个基于MNIST数据集的手写数字识别示例,帮助读者理解TensorFlow的基本编程流程。

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

【深度学习笔记】(二)Hello, Tensorflow!

一、安装

官方安装的方式很多种,本文采用Docker方式。Docker的深入使用文案很长很多,但我们都不需要,我们的主要目的还是Tensorflow,所以只需要基本的使用即可。PS:打开Tensorflow官网是需要翻墙的,所以上面的一些链接不能翻墙的情况下是打不开的,然鹅!Docker不需要翻墙就能打开,所以用Docker来安装Tensorflow就是为了绕墙而走。

1、Docker安装

既然不用翻墙,首先就是点我下载安装包,打开页面看到很多开发平台的版本,选择匹配自己开发平台的包下载安装即可,安装的过程就是一直点“下一步”。。。over。

安装成功后,会有两个入口:

这里写图片描述

第一个既然看不清名称就不用官他,也可以看得清也不要管他,因为我们只需要用第二个:Kitmatic,我们仅用用Kitmatic来操作Docker来提供Tensorfow的,不需要要学习第一个命令行的操作方式。

2、Kitmatic安装Tensorflow环境

点击第1步中的第二个图标启动Kitmatic;点击左上角的”NEW”按钮:

这里写图片描述

进入下图,在输入框中输入Tensorflow搜索,在Docker Hub搜索中选择一个,一般是第一个,点击“CREATE”按钮下载安装。
其他Jupyter NotebookTensorboard都可以在这里找到下载安装。

这里写图片描述


二、Hello, Tensorflow

1、编程步骤:

  1. 定义数据
  2. 定义计算图与变量
  3. 定义会话
  4. 进行计算

2、基于MNIST数据集的手写数字识别

这是很多教科书上的入门例子,但是没有说明其中代码依赖导致运行不起来。所以首先作为Tensorflow的入门例子,应该是包括在Tensorflow代码里的,先要把它clone下来:

git clone https://github.com/tensorflow/tensorflow

这时候可能因为权限问题无法拉下来,先去fork一份到自己名下就行了,或者直接打包下载。

接下来就能把代码跑起来了,写一遍感受一下

# 1、load data set
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# 2、see data set:   
# train - test - validation

# train data set
# print(mnist.train.images.shape,mnist.train.labels.shape)

# test data set
# print(mnist.test.images.shape,mnist.test.labels.shape)

# validation data set
# print(mnist.validation.images.shape,mnist.validation.labels.shape)

# 3、开启tensorflow session
import tensorflow as tf
sess = tf.InteractiveSession()

# 4、define softmax regression
# x
x = tf.placeholder(tf.float32,[None,784])
# W
W = tf.Variable(tf.zeros([784,10]))
# b
b = tf.Variable(tf.zeros(10))
# y
y = tf.nn.softmax(tf.matmul(x,W) + b)
# y_
y_ = tf.placeholder(tf.float32,[None,10])
# loss
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices=[1]))
# SGD
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# init 
tf.global_variables_initializer().run()

# 5、trainning starts
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    train_step.run({x:batch_xs, y_:batch_ys})
# trainning ends

# correct predictiong 
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
# accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
# evalue
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))

三、参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值