tensorflow(一)

1.tensorflow的helloworld

import tensorflow as tf

a = tf.constant(2)
b = tf.constant(3)

    # Launch the default graph.
with tf.Session() as sess:
print "a=2, b=3"
print "Addition with constants: %i" % sess.run(a+b)
print "Multiplication with constants: %i" % sess.run(a*b)

 



a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)

    # Define some operations
add = tf.add(a, b)
mul = tf.mul(a, b)

    # Launch the default graph.
with tf.Session() as sess:

print "Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3})
print "Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3})

  


 

 

matrix1 = tf.constant([[3., 3.]])#两个[[]]

matrix2 = tf.constant([[2.],[2.]])

product = tf.matmul(matrix1, matrix2)

with tf.Session() as sess:
result = sess.run(product)
print result

 


 

 

总结:

1.第一节学习到了tf.constant类型赋值操作,可以传入常数也可以传入矩阵

2.可以用tf.placeholder(tf.int)来充当站位,在with tf.Session() as sess:代码块中sess.run()的时候传入操作和feed_dict{}即可

3.注意with tf.Session(),大写和括号


 

 

2.用tensorflow实现线性回归

#导入库函数,第一步

import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
rng = numpy.random

#定义参数,第二步       including:  learning_rate   training_epochs    display_step

learning_rate = 0.01
training_epochs = 2000
display_step = 50

#准备训练数据,第三步

train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = numpy.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]

#第四步, 在构建计算图时,以占位方式保留一个placeholder的张量,需要在图执行时,再通过feed_dict填充

# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")

#第五步,模型相关设定: 权重、偏移量  激活函数   损失函数  优化器

    # Set model weights

W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

    # Construct a linear model
activation = tf.add(tf.mul(X, W), b)

    # Minimize the squared errors
cost = tf.reduce_sum(tf.pow(activation-Y, 2))/(2*n_samples) #L2 loss
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) #Gradient descent

#第六步,变量初始化后,进入sess操作:

#tf.global_variables_initializer
init=tf.global_variables_initializer()
# Launch the graph
with tf.Session() as sess:
  sess.run(init)

# Fit all training data
  for epoch in range(training_epochs):
    for (x, y) in zip(train_X, train_Y):
      sess.run(optimizer, feed_dict={X: x, Y: y})

#Display logs per epoch step
    if epoch % display_step == 0:
      print("Epoch:", '%04d' % (epoch+1), "cost=", \
      "{:.9f}".format(sess.run(cost, feed_dict={X: train_X, Y:train_Y})), \
        "W=", sess.run(W), "b=", sess.run(b))

  print("Optimization Finished!")


#第七步,输出最后的结果参数,包括 cost, W , b
  print("cost=", sess.run(cost, feed_dict={X: train_X, Y: train_Y}), \
  "W=", sess.run(W), "b=", sess.run(b))


 

#Graphic display
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()


 

3.用tensorflow实现逻辑回归

 

import tensorflow as tf

# Parameters
learning_rate = 0.01
training_epochs = 25
batch_size = 100
display_step = 1

# tf Graph Input
x = tf.placeholder("float", [None, 784]) # mnist data image of shape 28*28=784
y = tf.placeholder("float", [None, 10]) # 0-9 digits recognition => 10 classes

# Create model

# Set model weights
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# Construct model
activation = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax

# Minimize error using cross entropy
# Cross entropy
cost = -tf.reduce_sum(y*tf.log(activation))
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)


 

init = tf.initialize_all_variables()

with tf.Session() as sess:

    sess.run(init)

    # Training cycle

    for epoch in range(training_epochs):

        avg_cost = 0.

        total_batch = int(mnist.train.num_examples/batch_size)

        # Loop over all batches

        for i in range(total_batch):

            batch_xs, batch_ys = mnist.train.next_batch(batch_size)

            # Fit training using batch data

            sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})

            # Compute average loss

            avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/total_batch

        # Display logs per epoch step

        if epoch % display_step == 0:

            print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)

 

    print "Optimization Finished!"

 


 

    # Test model

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

    # Calculate accuracy

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

    print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})

 

3.Nearest Neighbor in TensorFlow

import numpy as np

import tensorflow as tf

 

# Import MINST data

import input_data

mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

 

# In this example, we limit mnist data

Xtr, Ytr = mnist.train.next_batch(5000) #5000 for training (nn candidates)

Xte, Yte = mnist.test.next_batch(200) #200 for testing

 

# Reshape images to 1D

Xtr = np.reshape(Xtr, newshape=(-1, 28*28))

Xte = np.reshape(Xte, newshape=(-1, 28*28))

 

# tf Graph Input

xtr = tf.placeholder("float", [None, 784])

xte = tf.placeholder("float", [784])

 

# Nearest Neighbor calculation using L1 Distance

# Calculate L1 Distance

distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.neg(xte))), reduction_indices=1)

# Predict: Get min distance index (Nearest neighbor)

pred = tf.arg_min(distance, 0)

 

accuracy = 0.

 

# Initializing the variables

init = tf.initialize_all_variables()

 

# Launch the graph

with tf.Session() as sess:

    sess.run(init)

 

    # loop over test data

    for i in range(len(Xte)):

        # Get nearest neighbor

        nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i,:]})

        # Get nearest neighbor class label and compare it to its true label

        print "Test", i, "Prediction:", np.argmax(Ytr[nn_index]), \

              "True Class:", np.argmax(Yte[i])

        # Calculate accuracy

        if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):

            accuracy += 1./len(Xte)

    print "Done!"

    print "Accuracy:", accuracy

posted @ 2018-12-13 15:24  慕云深  阅读(233)  评论(0)    收藏  举报