标签归档:TensorFlow

TensorFlow学习笔记04

MNIST入门

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

import tensorflow as tf

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

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.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.5).minimize(cross_entropy)

sess = tf.InteractiveSession()

tf.global_variables_initializer().run()

for _ in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

修改系统错误级别排除以下错误告警:Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

引入MNIST数据集

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

建立回归模型

y = tf.nn.softmax(tf.matmul(x,W) + b)

 

TensorFlow学习笔记03

来自于官方教程中的一个例子,使用梯度下降算法进行线性逼近

import tensorflow as tf

# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W*x + b
y = tf.placeholder(tf.float32)

# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

# training data
x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
  sess.run(train, {x: x_train, y: y_train})

# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

训练集

x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]

使用梯度下降算法,步进0.01,求方差最小化

optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

结果

W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11

TensorFlow学习笔记02

在TensorFlow中文社区文档中,有一个使用TensorFlow API进行拟合的事例。但代码注释比较简略,导致学习的时候有些不明白,这里给出详细的注释。

import tensorflow as tf
import numpy as np

# 使用 NumPy 生成假数据(phony data), 总共 100 个点.
# np.random.rand(x, y)生成1个含有x个长度为y的array的array
x_data = np.float32(np.random.rand(2, 100)) 
# np.dot(x, y)对矩阵x和y进行矩阵乘运算
y_data = np.dot([0.100, 0.200], x_data) + 0.300

# 构造一个线性模型
# tf.zeros(shape)生成尺寸为shape的张量,值为0的张量
b = tf.Variable(tf.zeros([1]))
# tf.random_uniform(shape, min, max)生成尺寸为shape,值为min~max之间随机数的张量
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b

# 最小化方差
'''
A loss function measures how far apart the current model is from the provided data. We'll use a standard loss model for linear regression, which sums the squares of the deltas between the current model and the provided data. linear_model - y creates a vector where each element is the corresponding example's error delta. We call tf.square to square that error. Then, we sum all the squared errors to create a single scalar that abstracts the error of all examples using tf.reduce_sum
'''
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 初始化变量
# 新版本中已经更新为tf.global_variables_initializer()
init = tf.initialize_all_variables()

# 启动图 (graph)
sess = tf.Session()
sess.run(init)

# 拟合平面
# python3中为range(min, max)
for step in xrange(0, 201):
    sess.run(train)
    if step % 20 == 0:
        #python3中为print()
        print step, sess.run(W), sess.run(b)

# 得到最佳拟合结果 W: [[0.100  0.200]], b: [0.300]

这个文档中使用的python版本为2.x,所以使用python3时会出现不兼容的情况。另外文档也比较老了,很多函数都已经更新。建议直接阅读官方文档。

TensorFlow学习笔记01

在虚拟环境virtualenv中安装TensorFlow

yum install python34
yum install python34-pip
pip3 install virtualenv
yum install python-devel
mkdir tensorflow
cd tensorflow/
virtualenv venv -p /usr/bin/python3 --no-site-packages # 使用virtualenv虚拟环境安装
cd venv/
. bin/activate
pip install -U TensorFlow

Hello Demo

python
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

准备MNIST数据集

vim input_data.py

内容如下

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

执行后,会自动下载MNIST数据集到MNIST_data这个目录。(需要翻墙)

(venv) [root@localhost MNIST_data]# ll
total 11340
drwxr-xr-x. 2 root root    4096 Nov  8 15:47 .
drwxr-xr-x. 3 root root      43 Nov  8 15:56 ..
-rw-r--r--. 1 root root 1648877 Nov  8 15:47 t10k-images-idx3-ubyte.gz
-rw-r--r--. 1 root root    4542 Nov  8 15:47 t10k-labels-idx1-ubyte.gz
-rw-r--r--. 1 root root 9912422 Nov  8 15:47 train-images-idx3-ubyte.gz
-rw-r--r--. 1 root root   28881 Nov  8 15:47 train-labels-idx1-ubyte.gz