LEARNING GOOGLE TENSORFLOW [L1: MAKE THE FIRST ACUAINTANCE]

博客

2016-12-06

572

0

THE DATA FLOW GRAPH

The data flow graph below can vividly demonstrate why the Google Open Source Software Library for Machine Intelligence is called TensorFlow (TF). I think the graph is impressive enough to give every begginer an intuitive understanding of TF.

tensor_flowing

HELLO, TENSORFLOW

TF was open-sourced at github and fully documented. It's very easy to get started if you refer to the tutorials. Here we use TF to fit a random generated hyper plane with TF's python API.

import tensorflow as tf
import numpy as np

# Dimension
dim = 3

# Weights and bias
weights = np.random.random([dim,dim])
bias = np.random.random([dim])

# Show original weights and bias
print("Weights:")
print(weights)
print("Bias:")
print(bias)

# Create 100 phony x, y data points in NumPy, y = x.*w + b
x_data = np.random.rand(100, dim).astype(np.float32)
y_data = np.dot(x_data, weights) + bias

# Try to find values for W and b that compute y_data = x_data.*W + b
W = tf.Variable(tf.random_uniform([dim, dim], 0.0, 1.0))
b = tf.Variable(tf.zeros([dim]))
y = tf.matmul(x_data, W) + b

# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# Before starting, initialize the variables.  We will 'run' this first.
init = tf.initialize_all_variables()

# Launch the graph.
sess = tf.Session()
sess.run(init)

# Fit the original parameters.
for step in range(2001):
    sess.run(train)
    if step % 200 == 0:
        print(step, sess.run(W), sess.run(b))

 

Let's RUN these codes to see what we will get. Do as below.

-> New a python script named "fitter.py" in any of your workspace folders.

-> Open "fitter.py" with your favorite text editor, let's say gedit.

-> Copy and paste these codes to "fitter.py" in gedit.

-> Open your command line shell in where "fitter.py" is.

-> Type the script below in the opened command line shell window, then press Enter.

python fitter.py

 

If you see the screen roll and display something like below, congratulations, you've make the first acquaintance with TensorFlow.

 

tf-learn

发表评论

全部评论:0条

帮杰

疯狂于web和智能设备开发,专注人机互联。