一、这里列出了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:\n',sess.run(con1))
print('zeros1:\n',sess.run(zeros1))
print('zeros2:\n',sess.run(zeros2))
print('ones1:\n',sess.run(ones1))
print('ones2:\n',sess.run(ones2))
print('line1:\n',sess.run(line1))
print('range1:\n',sess.run(range1))
print('random1:\n',sess.run(random1))
print('random2:\n',sess.run(random2))
print('add1:\n',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:\n',sess.run(matrix1))
print('matrix2:\n',sess.run(matrix2))
print('add:\n',sess.run(add))
print('subtract:\n',sess.run(subtract))
print('product1:\n',sess.run(product1))
print('product2:\n',sess.run(product2))
print('product3:\n',sess.run(product3))
print('div:\n',sess.run(div))
print('mod:\n',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:\n',sess.run(x_in))
print('x:\n',sess.run(x))
print('w:\n',sess.run(w))
print('result:\n',sess.run(result)) test6()

Tensorflow机器学习入门——常量、变量、placeholder和基本运算的更多相关文章

  1. Tensorflow机器学习入门——读取数据

    TensorFlow 中可以通过三种方式读取数据: 一.通过feed_dict传递数据: input1 = tf.placeholder(tf.float32) input2 = tf.placeho ...

  2. Tensorflow机器学习入门——网络可视化TensorBoard

    一.在代码中标记要显示的各种量 tensorboard各函数的作用和用法请参考:https://www.cnblogs.com/lyc-seu/p/8647792.html import tensor ...

  3. Tensorflow机器学习入门——MINIST数据集识别

    参考网站:http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html #自动下载并加载数据 from tensorflow.example ...

  4. Tensorflow机器学习入门——MINIST数据集识别(卷积神经网络)

    #自动下载并加载数据 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_s ...

  5. 入门&常量&变量

    位:二进制中,每个0或1就是一个位,叫做bit(比特) 字节:计算机最小是存储单元(byte或B) 8bit = 1B 常用cmd命令: 启动: Win+R,输入cmd回车切换盘符 盘符名称:进入文件 ...

  6. Tensorflow机器学习入门——ModuleNotFoundError: No module named 'tensorflow.keras'

    这个bug的解决办法: # from tensorflow.keras import datasets, layers, models from tensorflow.python.keras imp ...

  7. Tensorflow机器学习入门——cifar10数据集的读取、展示与保存

    基本信息 官网:http://www.cs.toronto.edu/~kriz/cifar.html 共60000张图片:50000张用于训练.10000张用于测试 图片大小为:32X32 数据集图片 ...

  8. Tensorflow机器学习入门——AttributeError: module 'scipy.misc' has no attribute 'toimage'

    这个bug的解决办法: import cv2 # scipy.misc.toimage(image_array).save('cifar10_data/raw/%d.jpg' % i) cv2.imw ...

  9. TensorFlow入门(常量变量及其基本运算)

    1.tensorflow常量变量的定义 测试代码如下: # encoding:utf-8 # OpenCV tensorflow # 类比 语法 api 原理 # 基础数据类型 运算符 流程 字典 数 ...

随机推荐

  1. DE1+回顾

    本实验使用DE1器件,cyloneV,主要做的基础实验,目的是回顾前期的学习和巩固知识和熟悉操作流程. 视频主要学习的是小梅哥视频. 工程文件夹取名 prj -----   工程文件存放目录(ip文件 ...

  2. Virtual Judge POJ 1328 Radar Installation

    贪心 #include<algorithm> #include<iostream> #include<cstdio> #include<cmath> u ...

  3. 用python制作多份试卷防止作弊(随机排列题目顺序和答案顺序,提供参考答案)

    #! /usr/bin/python# randomQuizeGenerator.py   -   Creates quizzes with questions and answers in # ra ...

  4. java中的try-catch-finally中的return的执行顺序

    在这里看到了try catch finally块中含有return语句时程序执行的几种情况,但其实总结的并不全,而且分析的比较含糊.但有一点是可以肯定的,finally块中的内容会先于try中的ret ...

  5. 截取字符,超出的用省略号代替js实现 substring

    可用到截取文字过多的问题,取0到6之间的字符,不包含6title.substring(0,6)+'...';

  6. 每天进步一点点------FPGA 静态时序分析模型——reg2reg

    2. 应用背景 静态时序分析简称STA,它是一种穷尽的分析方法,它按照同步电路设计的要求,根据电路网表的拓扑结构,计算并检查电路中每一个DFF(触发器)的建立和保持时间以及其他基于路径的时延要求是否满 ...

  7. Centos6.10-FastDFS-存储器Http配置

    Centos610系列配置 1.准备配置 cd /opt/download/fastdfs-master/confcp http.conf /etc/fdfs/http.confcp mime.typ ...

  8. Java代码三级跳——表达式、语句和代码块

    Java代码三级跳—表达式.语句和代码块 表达式(expression):Java中最基本的一个运算.比如一个加法运算表达式.1+2是一个表达式,a+b也是. 语句(statement):类似于平时说 ...

  9. Vue-cli 多页相关配置记录

    Vue-cli 多页相关配置记录 搭建一个顺手的MPA项目脚手架,其实根据项目的不同目录结构和打包配置都可以进行灵活的调整.这次的项目可能是包含各种客户端和管理后台在一起的综合项目所以需要将样式和脚本 ...

  10. 避坑之Hadoop安装伪分布式(Hadoop3.2.0/Ubuntu14.04 64位)

    一.安装JDK环境(这个可以网上随意搜一篇教程了照着弄,这里不赘述) 安装成功之后 输入 输入:java -version 显示如下说明jdk安装成功(我这里是安装JDK8) 二.安装Hadoop3. ...