双系统下成功安装ubuntu18.04的tensorflow-cpu版本验证mnist手写字符实验(终端和pycharm)

刚刚开始接触ubuntu,边学习边记录,错误的地方欢迎各路大神指正,也希望对自己有帮助。
由于以后毕设要用到android,所以就想着用tensorflow来训练网络,同时google公司出的。

由于之前安装了ubuntu18.04的双系统,并分了200g给它,所以想着自己可以倒腾一下,之前装了caffe,想在装个tensorflow学习一下。

首先安装ancaconda,在官网下载最新linux版本64位的anaconda。在downloads中找到下载文件,并用ternimal打开,开始安装。

$ bash Anaconda********.sh (****是文件名称)
开始安装
后面根据提示安装anaconda
下面开始安装TensorFlow
先尝试用pip安装
在这里插入图片描述
失败!!!
又尝试用conda安装
安装环境
在这里插入图片描述
开始了!!!
过程遇到proceed,选择Yes
但是下载包的时候又报错
查了之后,前辈说要更换源
添加这些源

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

再一次使用conda create -n tensorflow python=3.6进行环境的安装
在这里插入图片描述
成功安装!!!
激活tensorflow:source activate tensorflow
开始tensorflow安装
在这里插入图片描述
然后用pip安装又失败!!!
在这里插入图片描述
改用conda安装也失败了
开始度TAT
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
Warning: ‘https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/’ already in ‘channels’ list, moving to the top(这条和之前重复了,有代码提醒已经存在)
conda config --set show_channel_urls yes
在这里插入图片描述
再用conda install tensorflow
成功!!!!!!!!!!!

验证是否安装成功
输入python

结束!!!
然后今天再来更新mnist手写字符在tensorflow上的实验
之前已经装好了tensorflow,今天直接用终端激活tensorflow使用
然后就遇到了bug!!!!显示没有环境,用conda info --envs查看环境,明明存在。
在这里插入图片描述
多次尝试失败之后开始度
此处贴上解决出处
(https://blog.csdn.net/weixin_42705683/article/details/83013553)
1)
$ sudo gedit ~/.bashrc
在弹出的文件末尾加上下面的代码,路径就根据自己的填写,由于上面查找了存在环境
export tensorflow=/home/anaconda2/envs/tensorflow:$tensorflow
点击save保存
2)
$ sudo gedit /etc/profile
在最后一行添加以下内容,其中的路径根据自己的安装路径填写
export tensorflow=/home/anaconda2/envs/tensorflow:$tensorflow
点击save 保存
3)
$ source /etc/profile
修改完成
再次激活成功进入
血泪PS:激活使用完成之后一定要用 soure deactivate 退出环境!!!!不然下次会出错!!!
在这里插入图片描述
由于尝试在tensorflow上直接下载mnist数据集很慢,就提前下载好了,将下载好的数据集的压缩包解压之后里面还有四个压缩包,此处重点!!!!里面的四个压缩包不要解压,如下图就可以了,由于input_data.read_data_sets读取的是压缩包,之前我把四个都解压了,然后读取数据一直报错TAT,一把辛酸泪~~~

在这里插入图片描述
mnist数据集四个文件夹内容如下:
在这里插入图片描述

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow.examples.tutorials.mnist.input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

把MNIST_data数据集放在根目录下,input_data.read_data_sets会查找位置下是否有对应文件,没有直接的话直接下载,在根目录下直接生成一个MNIST_data数据集文件。
读取文件,并查看文件

print(mnist.train.images.shape,mnist.train.labels.shape)
print(mnist.test.images.shape,mnist.test.labels.shape)
print(mnist.validation.images.shape,mnist.validation.labels.shape)

在这里插入图片描述

import tensorflow as tf
sess=tf.InteractiveSession()

在这里插入图片描述
一些警告,先不管。

x=tf.placeholder(tf.float32,[None,784])
w=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
y=tf.nn.softmax(tf.matmul(x,w)+b)

在这里插入图片描述

y=tf.nn.softmax(tf.matmul(x,w)+b)
y_=tf.placeholder(tf.float32,[None,10])
cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)
tf.global_variables_initializer().run()
 for i in range(1000):
...     batch_xs,batch_ys=mnist.train.next_batch(100)
...     train_step.run({x:batch_xs,y_:batch_ys})

在这里插入图片描述

tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))
0.9094

在这里插入图片描述
在这里插入图片描述
pycharm的明天补上!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值