欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tensorflow入门

程序员文章站 2024-03-19 18:31:46
...

tensorflow入门

更多干货

tensorflow基本语法

#opencv tensorflow
#类比 语法 api 原理
#基础数据类型 运算符 流程 字典 数组
import tensorflow as tf
#常量
data1 = tf.constant(2,dtype=tf.int32)
#变量
data2 = tf.Variable(10,name='var')
print(data1)
print(data2)
'''
sess = tf.Session()
print(sess.run(data1))
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(data2))
sess.close()
# 本质 tf = tensor + 计算图
# tensor 数据
# op
# graphs 数据操作
# session
'''
init = tf.global_variables_initializer()
sess = tf.Session()
with sess:
    sess.run(init)
    print(sess.run(data2))
    Tensor("Const_2:0", shape=(), dtype=int32)
    <tf.Variable 'var_2:0' shape=() dtype=int32_ref>
    10

四则运算

import tensorflow as tf
data1 = tf.constant(6)
data2 = tf.constant(2)
dataAdd = tf.add(data1,data2)
dataMul = tf.multiply(data1,data2)
dataSub = tf.subtract(data1,data2)
dataDiv = tf.divide(data1,data2)
with tf.Session() as sess:
    print(sess.run(dataAdd))
    print(sess.run(dataMul))
    print(sess.run(dataSub))
    print(sess.run(dataDiv))
print('end!')
    8
    12
    4
    3.0
    end!
import tensorflow as tf
data1 = tf.constant(6)
data2 = tf.Variable(2)
dataAdd = tf.add(data1,data2)
dataCopy = tf.assign(data2,dataAdd)# dataAdd ->data2
dataMul = tf.multiply(data1,data2)
dataSub = tf.subtract(data1,data2)
dataDiv = tf.divide(data1,data2)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(dataAdd))
    print(sess.run(dataMul))
    print(sess.run(dataSub))
    print(sess.run(dataDiv))
    print('sess.run(dataCopy)',sess.run(dataCopy))#8->data2
    print('dataCopy.eval()',dataCopy.eval())#8+6->14->data = 14
    print('tf.get_default_session()',tf.get_default_session().run(dataCopy))
print('end!')
    8
    12
    4
    3.0
    sess.run(dataCopy) 8
    dataCopy.eval() 14
    tf.get_default_session() 20
    end!

矩阵基础

#placehold
import tensorflow as tf
data1 = tf.placeholder(tf.float32)
data2 = tf.placeholder(tf.float32)
dataAdd = tf.add(data1,data2)
with tf.Session() as sess:
    print(sess.run(dataAdd,feed_dict={data1:6,data2:2}))
    # 1 dataAdd 2 data (feed_dict = {1:6,2:2})
print('end!')
    8.0
    end!
#类比 数组 M行N列 []   内部[]  [里面 列数据]   [] 中括号整体 行数
#[[6,6]] [[6,6]]
import tensorflow as tf
data1 = tf.constant([[6,6]])
data2 = tf.constant([[2],
                     [2]])


data3 = tf.constant([[3,3]])
data4 = tf.constant([[1,2],
                     [3,4],
                     [5,6]])

print(data4.shape)# 维度
with tf.Session() as sess:
    print(sess.run(data4)) #打印整体
    print(sess.run(data4[0]))# 打印某一行
    print(sess.run(data4[:,0]))#MN 列
    print(sess.run(data4[0,1]))# 1 1  MN = 0 32 = M012 N01
    (3, 2)
    [[1 2]
     [3 4]
     [5 6]]
    [1 2]
    [1 3 5]
    2

矩阵运算

tensorflow入门
import tensorflow as tf
data1 = tf.constant([[6,6]])
data2 = tf.constant([[2],
                     [2]])
data3 = tf.constant([[3,3]])
data4 = tf.constant([[1,2],
                     [3,4],
                     [5,6]])
matMul = tf.matmul(data1,data2)
matMul2 = tf.multiply(data1,data2)
matAdd = tf.add(data1,data3)
with tf.Session() as sess:
    print(sess.run(matMul))#1 维 M=1 N2. 1X2(MK) 2X1(KN) = 1
    print(sess.run(matAdd))#1行2列
    print(sess.run(matMul2))# 1x2 2x1 = 2x2
    print(sess.run([matMul,matAdd]))
    [[24]]
    [[9 9]]
    [[12 12]
     [12 12]]
    [array([[24]]), array([[9, 9]])]
import tensorflow as tf
mat0 = tf.constant([[0,0,0],[0,0,0]])
mat1 = tf.zeros([2,3])
mat2 = tf.ones([3,2])
mat3 = tf.fill([2,3],15)
with tf.Session() as sess:
    #print(sess.run(mat0))
    #print(sess.run(mat1))
    #print(sess.run(mat2))
    print(sess.run(mat3))
    [[15 15 15]
     [15 15 15]]
import tensorflow as tf
mat1 = tf.constant([[2],[3],[4]])
mat2 = tf.zeros_like(mat1)
mat3 = tf.linspace(0.0,2.0,11)
mat4 = tf.random_uniform([2,3],-1,2)
with tf.Session() as sess:
    #print(sess.run(mat1))
    #print(sess.run(mat2))
    #print(sess.run(mat3))
    print(sess.run(mat4))
    [[ 1.01364231  0.03153861 -0.35802007]
     [ 1.68033934  1.30461025 -0.84316409]]

模块numpy的使用

#CURD
import numpy as np
data1 = np.array([1,2,3,4,5])
print(data1)
data2 = np.array([[1,2],
                  [3,4]])
print(data2)
#维度
print(data1.shape,data2.shape)
# zero ones
print(np.zeros([2,3]),np.ones([2,2]))
# 改查
data2[1,0] = 5
print(data2)
print(data2[1,1])
# 基本运算
data3 = np.ones([2,3])
print(data3*2)#对应相乘
print(data3/3)
print(data3+2)
# 矩阵+*
data4 = np.array([[1,2,3],[4,5,6]])
print(data3+data4)
print(data3*data4)