• Tensorflow机器学习入门——常量、变量、placeholder和基本运算


    一、这里列出了tensorflow的一些基本函数,比较全面:https://blog.csdn.net/M_Z_G_Y/article/details/80523834

    二、这里是tensortflow的详细教程:http://c.biancheng.net/tensorflow/

    三、下面程序是我学习常量、变量、placeholder和基本运算时形成的小函数

    import tensorflow as tf
    import numpy as np
    print(tf.__version__)#打印Tensorflow版本
    print(tf.__path__)#打印Tensorflow安装路径
    
    #3第一个tensorflow程序
    def test3():
        message = tf.constant('Welcome to the exciting world of Deep Neural Networks!')
        with tf.Session() as sess:
            print(sess.run(message).decode())
    
    #4程序结构
    def test4(): 
        v_1=tf.constant([1,3,4,5])
        v_2=tf.constant([2,3,4,5])
        v_add=tf.add(v_1,v_2)
        with tf.Session() as sess:
            print(sess.run(v_add))
    #5_1常量
    def test5_1():
        con1 = tf.constant([4,3,2])
        zeros1= tf.zeros([2,3],tf.int32)
        zeros2=tf.zeros_like(con1)
        ones1=tf.ones([2,3],tf.int32)
        ones2=tf.ones_like(con1)
        nine1=tf.fill([2, 3], 9.0) 
        diag= tf.diag([1.0, 2.0, 3.0])
        line1 = tf.linspace(2.0,5.0,5)
        range1= tf.range(10)
        random1=tf.random_normal([2,3],mean=2,stddev=4,seed=12)#正态分布随机数组
        random2=tf.truncated_normal([2,3],stddev=3,seed=12)#结尾正态随机分布数组
        add1=tf.add(con1,zeros1)
        with tf.Session() as sess:
            print('con1:
    ',sess.run(con1))
            print('zeros1:
    ',sess.run(zeros1))
            print('zeros2:
    ',sess.run(zeros2))
            print('ones1:
    ',sess.run(ones1))
            print('ones2:
    ',sess.run(ones2))
            print('line1:
    ',sess.run(line1))
            print('range1:
    ',sess.run(range1))
            print('random1:
    ',sess.run(random1))
            print('random2:
    ',sess.run(random2))
            print('add1:
    ',sess.run(add1))
        
    #5_2变量
    def test5_2():
        matrix1=tf.Variable(tf.random_uniform([2,2],0,10,seed=0),name='weights')
        matrix2=tf.Variable(tf.random_uniform([2,2],0,10,seed=1),name='weights')
        add=tf.add(matrix1,matrix2)#加法
        subtract=tf.subtract(matrix1,matrix2)#减法
        product1= tf.matmul(matrix1,matrix2)#矩阵相乘
        product2=tf.scalar_mul(2,matrix1)#标量*矩阵
        product3=matrix1*matrix2#对应元素相乘,等同于tf.multiply()
        div=tf.div(matrix1,matrix2)#对应元素相除
        mod=tf.mod(matrix1,matrix2)#对应元素取模
        init = tf.global_variables_initializer()
        with tf.Session() as sess:
            sess.run(init)
            print('matrix1:
    ',sess.run(matrix1))
            print('matrix2:
    ',sess.run(matrix2))
            print('add:
    ',sess.run(add))
            print('subtract:
    ',sess.run(subtract))
            print('product1:
    ',sess.run(product1))
            print('product2:
    ',sess.run(product2))
            print('product3:
    ',sess.run(product3))
            print('div:
    ',sess.run(div))
            print('mod:
    ',sess.run(mod))
    
    #5_3Placeholder
    def test5_3():
        x=tf.placeholder(tf.float32,[None,5])
        y=x*2
        data=tf.random_uniform([4,5],0,10)
        with tf.Session() as sess:
            x_data=sess.run(data)
            print(sess.run(y,feed_dict={x:x_data}))
     
    #几个矩阵运算
    def test6():
        a=tf.ones([2,3,4])
        b=tf.reshape(np.arange(24), [2,3,4])
        b_slice=tf.strided_slice(b, [0,0,1], [2,2,3])#张量切片
        c=tf.constant(np.arange(24))
        c_reshape=tf.reshape(c,[2,3,4])#张量调整形状
        c_transpose=tf.transpose(c_reshape, [1,2,0])#张量转置
        with tf.Session() as sess:
            print(sess.run(b))
            print(sess.run(b_slice))
            print(sess.run(c))
            print(sess.run(c_reshape))
            print(sess.run(c_transpose))
    #卷积
    def test7():  
        x_in=tf.reshape(np.arange(50), [1,2,5,5])
        x_transpose=tf.transpose(x_in,[0,3,2,1])
        x=tf.cast(x_transpose,tf.float32)#转换数据类型
        w_con=tf.ones([2,2,2,1])
        w=tf.cast(w_con,tf.float32)
        result=tf.nn.conv2d(x, w, strides = [1, 1, 1, 1], padding = 'SAME')#卷积计算
        with tf.Session() as sess:
            print('x_in:
    ',sess.run(x_in))
            print('x:
    ',sess.run(x))
            print('w:
    ',sess.run(w))
            print('result:
    ',sess.run(result))
        
        
    test6()
  • 相关阅读:
    数据结构:图 (总结)
    排序算法总结(此篇文章是14年写作,代码难看,请看我新发表的排序总结)
    no identifier specified for entity错误
    哈夫曼编码算法思想总结
    线索二叉树
    ORACLE 错误 ora-01830 解决方法
    (转)web会话管理方式
    (转)C3P0配置
    分页技术()
    简易 DBUtil 封装
  • 原文地址:https://www.cnblogs.com/Fengqiao/p/tensorflow_1.html
Copyright © 2020-2023  润新知