深度学习框架tensorflow入门(本人尽量用通俗易懂的语言去描绘)

一些关于tensorflow的好文章(与本博客无关):

机器之心:令人困惑的TensorFlow!https://baijiahao.baidu.com/s?id=1604877144040331406&wfr=spider&for=pc
关于tensorflow的关键介绍:https://www.jianshu.com/p/7bf7cc81e2ff
tensorflow官方文档~中文:https://www.w3cschool.cn/tensorflow_python/tf_nn_local_response_normalization.html

tensorflow的简介:

TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。顾名思义:tensor是张量的意思,flow就是流动的意思。

tensorflow平台的搭建:

1.可以选择在本地搭建
利用 pip install tensorflow对应版本 或者 从官网上下载
深度学习实战应用场合要求CPU必须得强大,所以最好有好的CPU的电脑才在本地搭建
2.选择服务器平台
搭建平台可以选择pycharm或者XManager(Xshell,Xftp)
现在主流有阿里云,腾讯云,华为云等等

把不必要的内容掩盖:

加一段代码解决即可:

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

tensorflow的run()函数:

run()函数可以让代码变得更加简洁,在搭建神经网络中,经历了数据集准备、前向传播过程设计、损失函数及反向传播过程设计等三个过程,形成计算网络,再通过会话tf.Session().run()进行循环优化网络参数。这样可以使得代码变得更加简洁,可以集中处理多个图和会话,明确调用tf.Session().run()可能是一种更加直观的方法。

总而言之,我们先规划好计算图,再编写代码,之后调用tf.Session.run()。简洁高效。

tensorflow的常量/变量常用操作:

核心语句:

import tensorflow as tf

tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()
常量(constant)

norm = tf.random_normal([2,3],mean=-1,stddev=4)    #随机产生二行三列的矩阵

c=tf.constant([[1,2],[3,4],[5,6]])    #这个是常量
shuff = tf.random_shuffle(c)        #把c随机排列

sess = tf.Session()
print(sess.run(norm))
print(sess.run(shuff))

输出结果:

[[ 0.0164932  -4.3884916  -0.41980857]
 [ 2.8782437  -3.5063827   2.4331698 ]]
[[3 4]
 [5 6]
 [1 2]]

变量(Variable)

w = tf.Variable([[0.5,1.0]])               #tensorflow规定的死死的变量
x=tf.Variable([[2.0],[1.0]])

y=tf.matmul(w,x)                         #张量相乘操作


with tf.Session() as sess :
    sess.run(tf.global_variables_initializer())        #第一步::全局变量的初始化(千万不能忘记)
    print(y.eval())                         #直接写y也行

输出结果:

[[2.]]

循环结构:

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())

    for _ in range(4):
        print(sess.run(state))                    #把state打印出来
        sess.run(update)                          #每run一次update就把关于某个变量都给跑一遍,就是new_value也得跑一次

输出结果:

0
1
2
3

注意事项:
输出不像往常一样,直接print(变量)
tensorflow有两种输出方法print(y.eval()),print(sess.run(y))
这其中最主要的区别就在于你可以使用sess.run()在同一步获取多个tensor中的值

t = tf.constant(42.0)
u = tf.constant(37.0)
tu = tf.mul(t, u)
ut = tf.mul(u, t)
with sess.as_default():
   tu.eval()  # runs one step
   ut.eval()  # runs one step
   sess.run([tu, ut])  # evaluates both tensors in a single step

numpy与tensorflow变量之间的转换

所以直接写出tensorflow所规定的就不需要转换了

import numpy as np

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

执行加减乘除

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

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

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))
    print(x)
    print(y)

输出结果:

a= 5.0
b= 10.0
a+b= 15.0
a/b= 0.5
Tensor("add:0", shape=(), dtype=float32)
Tensor("devide:0", shape=(), dtype=float32)

自行选择输入:

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)]

你可能感兴趣的:(Tensorflow一)