使用 pip install tensorflow,安装tensorflow时,下载很慢,而且还经常下载到一半就断掉,又要重新下载。
解决办法:
步骤1,观察打印出的信息,找到 wheel文件的下载链接,并通过浏览器页面下载。
在 pip install tensorflow之后,观察打印出的信息,找到 wheel文件的下载链接。在网页上下载这个wheel文件。每个机器上安装时的下载链接会有所不同。如下图所示,我的wheel文件的下载地址是:
https://www.piwheels.org/simple/tensorflow/tensorflow-1.14.0-cp37-none-linux_armv7l.whl
在浏览器中输入这个链接,就会弹出下载页面,下载保存wheel文件,并将这个文件拷贝到home目录。
步骤2,在命令行窗口,执行下列命令安装wheel文件。
pip install tensorflow-1.14.0-cp7-none-linux_armv7l.whl --user
其中,install后面的文件名就是第一步中下载的wheel文件的文件名。
安装过程中,会自动下载一些依赖项。
步骤3,实例
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
tf.reset_default_graph()
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
w = tf.Variable(tf.random_normal([784, 10]))
b = tf.Variable(tf.zeros([10]))
pred = tf.matmul(x, w) + b
pred = tf.nn.softmax(pred)
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
training_epochs = 25
batch_size = 100
display_step = 1
save_path = 'model/'
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples/batch_size)
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x:batch_xs, y:batch_ys})
avg_cost += c / total_batch
if (epoch + 1) % display_step == 0:
print('epoch= ', epoch+1, ' cost= ', avg_cost)
print('finished')
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print('accuracy: ', accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))
save = saver.save(sess, save_path=save_path+'mnist.cpkt')
print(" starting 2nd session ...... ")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.restore(sess, save_path=save_path+'mnist.cpkt')
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print('accuracy: ', accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
output = tf.argmax(pred, 1)
batch_xs, batch_ys = mnist.test.next_batch(2)
outputval= sess.run([output], feed_dict={x:batch_xs, y:batch_ys})
print(outputval)
im = batch_xs[0]
im = im.reshape(-1, 28)
plt.imshow(im, cmap='gray')
plt.show()
im = batch_xs[1]
im = im.reshape(-1, 28)
plt.imshow(im, cmap='gray')
plt.show()
步骤4,最新版本的tensorflow中没有预装 example
步骤3的实例在tensorflow 1.13版本中可以正常运行。但是在1.14版本中出错,报错信息为:no module named. ‘tensorflow.examples.tutorials’
解决办放参考:https://stackoverflow.com/questions/50313441/modulenotfounderror-no-module-named-tensorflow-examples
另外一种解决方法:
步骤5,确认tensorflow的安装路径
使用 tf.path
步骤6,最终采用的方法下载input_data.py,并修改import
input_data.py的下载地址: https://github.com/admin110/tensorflow
步骤7,三维卷积神经网络预测MNIST数字详解
参考:http://c.biancheng.net/view/1929.html
其他网页:
keras安装:https://keras-cn.readthedocs.io/en/latest/for_beginners/keras_linux/
tensorflow中读取自己的数据集:https://blog.csdn.net/sinat_42378539/article/details/83047631
tensorflow使用CNN分析mnist手写体数字数据集: https://blog.csdn.net/dillon2015/article/details/79068653
tensorflow(12)CNN实现Mnist分类、可视化: https://blog.csdn.net/dillon2015/article/details/79068653
『TensorFlow』模型保存和载入方法汇总: https://www.cnblogs.com/think90/p/11738257.html
tensorflow将训练好的模型freeze,即将权重固化到图里面,并使用该模型进行预测: https://blog.csdn.net/lujiandong1/article/details/53385092