I have learning needs, it is to be installed tensorlow, the reason why the installation of version 1.x, because the current study is to find a video for the 1.x versions, and the difference between the version 1.x and 2.x is still there Some, I think first with version 1.x to learn, after the installation of the day, I found that this installation is not so easy, but after also try to find a better method of installation, write here, the record about
I use ubuntu is the most basic version of the virtual machine, did not add anything else, so what cuda those with little trouble, but I do not have this version installed, as a relatively simple and the
tensorflow There are two versions, one for the 2.7 version of the python, and another for a 3.x version of python, here take 3.5 version of python (ubuntu16.04 comes with the system)
installation steps are as follows
1. Install pip3
In a system with a python2 and python3 in, pip for installation in the python2, pip3 is targeted to python3 mounting
input terminal sudo apt-get install python3-pip
intermediate input y, wait for the end of the installation
update to the latest version pip and pip3, has entered in a terminal:
sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
If during error should be on it and try again
2. Direct installation tensorflow
Input Terminal:
sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==1.14.0
I downloaded version 1.14.0 here is because I found that other versions have a problem with it, 1.14.0 directly on the line! ! !
Then install several tensorflow feature packages, here only to wait, if the intermediate appear red error trying to execute the command again!
See these basic installation can be considered complete! !
3. The test is complete
In the terminal input python3.5
, note that if the input python
, then open the default python2.7
input
import tensorflow as tf
The words appear to represent the installation of almost all the same, this jump is not error, but a number of variable declarations like too much trouble to do these logs will always occur, resulting in the feeling of use is not very good! ! !
Next New test.py file on the desktop:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
plotdata = {"batchsize":[], "loss":[]}
def moving_average(a, w=10):
if len(a)<w:
return a[:]
return [val if idx < w else sum(a[(idx-w):idx])/w for idx, val in enumerate(a)]
# 模拟数据
train_X = np.linspace(-1, 1, 100)
train_Y = 2*train_X + np.random.randn(*train_X.shape)*0.3 # 加入了噪声
# 图形展示
plt.plot(train_X,train_Y,'ro',label="original data") # label数据标签
plt.legend()
plt.show()
tf.reset_default_graph() # 重置会话
# 创建模型
# 占位符
X = tf.placeholder("float")
Y = tf.placeholder("float")
# 模型参数
W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.zeros([1]), name="bias")
# 前向结构
z = tf.multiply(X, W) +b
# 反向优化
cost = tf.reduce_mean(tf.square(Y-z))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# 初始化变量
init = tf.global_variables_initializer()
# 参数设置
training_epochs = 20
display_step = 2
saver = tf.train.Saver() # 模型生成,并保存
savedir = "log/"
# 启动session
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
for (x, y) in zip(train_X,train_Y):
sess.run(optimizer, feed_dict={X:x, Y:y})
# 显示训练中的详细信息
if epoch % display_step == 0:
loss = sess.run(cost, feed_dict={X:train_X, Y:train_Y})
print("Epoch:",epoch+1,"cost=", loss,"W=",sess.run(W),"b=",sess.run(b))
if not (loss=="NA"):
plotdata["batchsize"].append(epoch)
plotdata["loss"].append(loss)
print("finished!")
saver.save(sess, savedir+"linermodel.cpkt")
print("cost=",sess.run(cost, feed_dict={X:train_X, Y:train_Y}),"W=", sess.run(W),"b=",sess.run(b))
# 图形显示
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
plt.legend()
plt.show()
plotdata["avgloss"] = moving_average(plotdata["loss"])
plt.figure(1)
plt.subplot(211)
plt.plot(plotdata["batchsize"], plotdata["avgloss"], 'b--')
plt.xlabel('Minibatch number')
plt.ylabel('Loss')
plt.title('Minibatch run vs. Training loss')
plt.show()
Before running this file, you need to download some libraries used in this program:
sudo apt-get install python3-tk
sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib
Of course, more than two, use different modules have some libraries used to get library, you can download on demand
finally run:
cd Desktop/
python3.5 test.py
The basic page is shown, here on the instructions to install the basic use
**
Final note, I learn here is to facilitate the installation of 1.14.0, the current version has been obtained to 2.1.0; or you want to upgrade later! ! ! !
**