TF 基本运算

目录


TF 计算的每一个变量必须是 tensor 格式;

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

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

a = 3
# 创建一个变量
w = tf.Variable([[0.5,1.0]])
x = tf.Variable([[2.0],[1.0]]) 

y = tf.matmul(w, x)  

# 全局变量初始化
init_op = tf.global_variables_initializer()

# 初始化之前,虽然构建了x,y,但只是空架子;但创建 session 后才有可计算区域;run 之后才有效果。
with tf.Session() as sess:
    sess.run(init_op)
    print (y.eval())  # [[2.]]
  • matmul

创建数据

tensorflow很多操作跟numpy有些类似的

  • tf.zeros([3, 4], int32) ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

  • tf.zeros_like(tensor) ==> [[0, 0, 0], [0, 0, 0]]

  • tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]

  • tf.ones_like(tensor) ==> [[1, 1, 1], [1, 1, 1]]

  • tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

  • tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.] [-1. -1. -1.]]

  • tf.linspace(10.0, 12.0, 3, name="linspace") => [ 10.0 11.0 12.0]

  • tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]


#生成的值服从具有指定平均值和标准偏差的正态分布
norm = tf.random_normal([2, 3], mean=-1, stddev=4)

# 洗牌
c = tf.constant([[1, 2], [3, 4], [5, 6]])
shuff = tf.random_shuffle(c)

# 每一次执行结果都会不同
sess = tf.Session()
print (sess.run(norm))
print (sess.run(shuff))

'''
[[-1.7481391 -1.2791823 -4.103035 ]
 [ 2.5581448 -2.46988    3.254735 ]]
[[3 4]
 [1 2]
 [5 6]]
'''

state = tf.Variable(0)
new_value = tf.add(state, tf.constant(1))
update = tf.assign(state, new_value)

with tf.Session() as sess:
    # 第一步,全局变量的初始化
    sess.run(tf.global_variables_initializer())
    print(sess.run(state))    
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

import numpy as np
a = np.zeros((3,3))
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:
     print(sess.run(ta))

'''
[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
'''

a = tf.constant(5.0)
b = tf.constant(10.0)

x = tf.add(a, b, name="add")
y = tf.div(a, b, name="divide")

with tf.Session() as sess:
    print("a =", sess.run(a))
    print("b =", sess.run(b))
    print("a + b =", sess.run(x))
    print("a/b =", sess.run(y))

'''
a = 5.0
b = 10.0
a + b = 15.0
a/b = 0.5
'''

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
    print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))

# [array([ 14.], dtype=float32)]

线性回归

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

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

import numpy as np 
import matplotlib.pyplot as plt

# 随机生成1000个点,围绕在 y=0.2x+0.4 的直线周围,增加小范围浮动
num_points = 1000
vectors_set = []
for i in range(num_points):
    x1 = np.random.normal(0.0, 0.55)
    y1 = x1 * 0.2 + 0.4 + np.random.normal(0.0, 0.03)
    vectors_set.append([x1, y1])

# 生成一些样本
x_data = [v[0] for v in vectors_set]
y_data = [v[1] for v in vectors_set]

plt.scatter(x_data,y_data,c='r')

TF 基本运算


# 生成1维的W矩阵,取值是[-1,1]之间的随机数
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='W')

# 生成1维的b矩阵,初始值是0
b = tf.Variable(tf.zeros([1]), name='b')

# 经过计算得出预估值y
y = W * x_data + b

# 以预估值y和实际值y_data之间的均方误差作为损失
loss = tf.reduce_mean(tf.square(y - y_data), name='loss')

# 采用梯度下降法来优化参数
optimizer = tf.train.GradientDescentOptimizer(0.5)

# 训练的过程就是最小化这个误差值
train = optimizer.minimize(loss, name='train')


sess = tf.Session()

init = tf.global_variables_initializer()
sess.run(init)

# 初始化的W和b是多少
print ("W =", sess.run(W), "b =",  sess.run(b), "lossess.run(b)s =", sess.run(loss))

# 执行20次训练
for step in range(20):
    sess.run(train)
    
    # 输出训练好的W和b
    print("W = ", sess.run(W), "b =", sess.run(b), "loss = ", sess.run(loss) )

'''
W = [-0.45451117] b = [0.] lossess.run(b)s = 0.2924671
W =  [0.10856208] b = [0.4001943] loss =  0.003454611
W =  [0.13663287] b = [0.4001553] loss =  0.0021193654
W =  [0.15612908] b = [0.4001286] loss =  0.0014752678
W =  [0.16966991] b = [0.40011007] loss =  0.001164567
W =  [0.17907451] b = [0.4000972] loss =  0.0010146907
W =  [0.18560636] b = [0.40008825] loss =  0.0009423933
W =  [0.19014296] b = [0.40008205] loss =  0.0009075183
W =  [0.1932938] b = [0.40007773] loss =  0.00089069526
W =  [0.19548216] b = [0.40007475] loss =  0.0008825802
W =  [0.19700207] b = [0.40007266] loss =  0.00087866565
W =  [0.19805771] b = [0.4000712] loss =  0.0008767772
W =  [0.1987909] b = [0.40007022] loss =  0.0008758663
W =  [0.19930011] b = [0.4000695] loss =  0.0008754269
W =  [0.19965377] b = [0.40006903] loss =  0.000875215
W =  [0.19989942] b = [0.4000687] loss =  0.0008751127
W =  [0.20007002] b = [0.40006846] loss =  0.0008750634
W =  [0.20018852] b = [0.4000683] loss =  0.00087503955
W =  [0.20027082] b = [0.4000682] loss =  0.00087502814
W =  [0.20032798] b = [0.4000681] loss =  0.0008750226
W =  [0.20036767] b = [0.40006804] loss =  0.00087501993
'''

plt.scatter(x_data,y_data,c='r')
plt.plot(x_data,sess.run(W)*x_data+sess.run(b))
plt.show()

TF 基本运算


Mnist数据集

tf 中内置了 Mnist,也可以额外下载;
内置的好处:东西直接写好了。

mnist 数据集:http://yann.lecun.com/exdb/mnist/

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

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

import numpy as np
import matplotlib.pyplot as plt
 
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf2

print ("001")


# 下载数据
print ("下载中...")
mnist = input_data.read_data_sets('data/', one_hot=True)
 
print (" 类型是 %s" % (type(mnist)))
print (" 训练数据有 %d" % (mnist.train.num_examples))
print (" 测试数据有 %d" % (mnist.test.num_examples))

'''
训练数据有 55000
 测试数据有 10000
'''

# 查看规格

trainimg   = mnist.train.images
trainlabel = mnist.train.labels
testimg    = mnist.test.images
testlabel  = mnist.test.labels
# 28 * 28 * 1
print (" 数据类型 is %s"    % (type(trainimg)))
print (" 标签类型 %s"  % (type(trainlabel)))
print (" 训练集的shape %s"   % (trainimg.shape,))
print (" 训练集的标签的shape %s" % (trainlabel.shape,))
print (" 测试集的shape' is %s"    % (testimg.shape,))
print (" 测试集的标签的shape %s"  % (testlabel.shape,))
'''
 数据类型 is <class 'numpy.ndarray'>
 标签类型 <class 'numpy.ndarray'>
 训练集的shape (55000, 784)
 训练集的标签的shape (55000, 10)
 测试集的shape' is (10000, 784)
 测试集的标签的shape (10000, 10)
'''

nsample = 5
randidx = np.random.randint(trainimg.shape[0], size=nsample)

for i in randidx:
    curr_img   = np.reshape(trainimg[i, :], (28, 28)) # 28 by 28 matrix 
    curr_label = np.argmax(trainlabel[i, :] ) # Label
    plt.matshow(curr_img, cmap=plt.get_cmap('gray'))
    print ("" + str(i) + "th 训练数据 " 
           + "标签是 " + str(curr_label))
    plt.show()

38252th 训练数据 标签是 3
TF 基本运算


# Batch数据
print ("Batch Learning? ")
batch_size = 100
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
print ("Batch数据 %s" % (type(batch_xs)))
print ("Batch标签 %s" % (type(batch_ys)))
print ("Batch数据的shape %s" % (batch_xs.shape,))
print ("Batch标签的shape %s" % (batch_ys.shape,))

'''
Batch Learning? 
Batch数据 <class 'numpy.ndarray'>
Batch标签 <class 'numpy.ndarray'>
Batch数据的shape (100, 784)
Batch标签的shape (100, 10)
'''


逻辑回归做 Mnist 分类

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

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

import numpy as np
import matplotlib.pyplot as plt
 
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf2

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

# 参数设置
numClasses = 10
inputSize = 784  
trainingIterations = 50000
batchSize = 64

X = tf.placeholder(tf.float32, shape = [None, inputSize])
y = tf.placeholder(tf.float32, shape = [None, numClasses])

# 参数初始化
W1 = tf.Variable(tf.random_normal([inputSize, numClasses], stddev=0.1))
B1 = tf.Variable(tf.constant(0.1), [numClasses])

# 构造模型
y_pred = tf.nn.softmax(tf.matmul(X, W1) + B1)

loss = tf.reduce_mean(tf.square(y - y_pred))
opt = tf.train.GradientDescentOptimizer(learning_rate = .05).minimize(loss)

correct_prediction = tf.equal(tf.argmax(y_pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

# 迭代计算
for i in range(trainingIterations):
    batch = mnist.train.next_batch(batchSize)
    batchInput = batch[0]
    batchLabels = batch[1]
    _, trainingLoss = sess.run([opt, loss], feed_dict={X: batchInput, y: batchLabels})
    if i%1000 == 0:
        train_accuracy = accuracy.eval(session=sess, feed_dict={X: batchInput, y: batchLabels})
        print ("step %d, training accuracy %g"%(i, train_accuracy))

# 测试结果

batch = mnist.test.next_batch(batchSize)
testAccuracy = sess.run(accuracy, feed_dict={X: batch[0], y: batch[1]})
print ("test accuracy %g"%(testAccuracy))
# test accuracy 0.96875














上一篇:查看oracle数据库中表是否被锁


下一篇:tensorflow基本常用操作