export TF_BINARY_URL = https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.0rc0-cp35-cp35m-linux_x86_64.whl
pip install --upgrade $ TF_BINARY_URL Tensorflow 不支持32位,只支持64位。这太坑爹,本人的电脑性能不行,一直用的是Virtualbox或是Vmware运行的32位Ubuntu(12.04),刚开始还以为是Ubuntu版本太低,一直升级到Ubuntu16,发现还是不行,最后再查才发现TensorFlow只支持64位。
这绝对是一个大坑,谨慎勿入!
以下是报错: tensorflow-1.0.0rc0-cp35-cp35m-linux_x86_64.whl is not a supported wheel on this platform
我们同时看到运行过程中有一些警告:
WARNING:tensorflow:From tf_exam1.py:23: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
意思是initialize_all_variables即将要被弃用,推荐用global_variables_initializer()函数来代替。所以根据提示之际用新的函数代替即可。
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations
这些warings的意思是说:你的机器上有这些指令集可以用,并且用了他们会加快你的CPU运行速度,但是你的TensorFlow在编译的时候并没有用到这些指令集。
我的tensorflow在安装的时候采用的pip install指令,这种安装方式会存在这种问题。主要有两种解决方法,一种是修改警告信息的显示级别,使这种信息不再出现,另外一种就是自己重新编译安装tensorflow,在编译的时候使用这些指令集。这里我尝试第二种解决方法。并且由于我的机器上没有高效的GPU,所以我尝试安装的是CPU版本。
这里因为不影响实验就不处理这个warning,所以修改了第一个warning之后的程序变成:
"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
import tensorflow as tf
import numpy as np #科学计算的模块
# create data
x_data = np.random.rand(100).astype(np.float32) #生成100个随机数列,数据类型是float32,tf中大多数都用的是float32
y_data = x_data*0.1 + 0.3 #0.1为权重,0.3为偏置
### create tensorflow structure start ###
Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))#用Variable初始化Weights权重变量-随机数列生成-1到1的1维数据,
biases = tf.Variable(tf.zeros([1])) #偏置初始化为0,
y = Weights*x_data + biases #所要预测的y,通过训练使得y逼近真实的y_data,优化得到的参数变量就即Weights和biases
loss = tf.reduce_mean(tf.square(y-y_data)) #预测的y和实际的y_data的差别,即均方差
optimizer = tf.train.GradientDescentOptimizer(0.5) #用tf的梯度下降优化器减少误差loss,提升参数的准确度,0.5为学习效率(一般小于1)
train = optimizer.minimize(loss) #训练以减少误差
init = tf.global_variables_initializer() #初始化tf结构图中的所有变量,这里是Weights和biases
### create tensorflow structure end ###
sess = tf.Session() #激活创建的结构图,Sesssion是神经网路的执行命令的对话控制
sess.run(init) #激活init,也就是所有结构
for step in range(201):#让神经网络一步一步的训练 201步
sess.run(train) # 开始训练
if step % 20 == 0: #每间隔20步打印以下训练得到的变量值
print(step, sess.run(Weights), sess.run(biases))
运行的效果: