TensorFlow -2-Ops-1

来源:互联网 发布:js实现鼠标特效 编辑:程序博客网 时间:2024/06/06 03:46

https://web.stanford.edu/class/cs20si/
https://web.stanford.edu/class/cs20si/syllabus.html

import tensorflow as tfa = tf.constant(2)b = tf.constant(3)x = tf.add(a, b)with tf.Session() as sess:    print sess.run(x)

用 TensorBoard 可视化

import tensorflow as tfa = tf.constant(2)b = tf.constant(3)x = tf.add(a, b)with tf.Session() as sess:    # add this line to use TensorBoard.     writer = tf.summary.FileWriter('./graphs, sess.graph)     print sess.run(x)writer.close() # close the writer when you’re done using it

运行程序

$ python [yourprogram].py$ tensorboard --logdir="./graphs" --port 6006

Then open your browser and go to http://localhost:6006/

常量constants-tf.constant()

tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
import tensorflow as tf#定义两个变量a = tf.constant([2, 2], name="a")b = tf.constant([[0, 1], [2, 3]], name="b")#计算和 乘积 与numpy相同x = tf.add(a, b, name="add")y = tf.mul(a, b, name="mul")with tf.Session() as sess:    x, y = sess.run([x, y])     print x, y #地难以特定的值,定义一个全0的数据 与numpy一样tf.zeros(shape, dtype=tf.float32, name=None)#tf.zeros([2, 3], tf.int32) ==> [[0, 0, 0], [0, 0, 0]]#定义一个全零数据作为tensor的输入,数据的类型跟input_tensor参数数据类型一样#tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)# input_tensor is [0, 1], [2, 3], [4, 5]]tf.zeros_like(input_tensor) ==> [[0, 0], [0, 0], [0, 0]]#同理定义全1tf.ones(shape, dtype=tf.float32, name=None)tf.ones_like(input_tensor, dtype=None, name=None, optimize=True)#定义一个张量用特定数字填充# tf.fill(dims, value, name=None) # tf.fill([2, 3], 8) ==> [[8, 8, 8], [8, 8, 8]]

序列作为常量:

tf.linspace(start, stop, num, name=None) # slightly different from np.linspacetf.linspace(10.0, 13.0, 4) ==> [10.0 11.0 12.0 13.0]tf.range(start, limit=None, delta=1, dtype=None, name='range')# 'start' is 3, 'limit' is 18, 'delta' is 3可以理解为步长吧tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]# 'limit' is 5 默认从0开始的吧tf.range(limit) ==> [0, 1, 2, 3, 4]

随机数作为常量

tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)tf.random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None)tf.random_shuffle(value, seed=None, name=None)tf.random_crop(value, size, seed=None, name=None)tf.multinomial(logits, num_samples, seed=None, name=None)tf.random_gamma(shape, alpha, beta=None, dtype=tf.float32, seed=None, name=None)tf.set_random_seed(seed)

操作符
这里写图片描述

#定义两个变量a = tf.constant([3, 6])b = tf.constant([2, 2])#两个变量和tf.add(a, b) # >> [5 8]#多个变量和tf.add_n([a, b, b]) # >> [7 10]. Equivalent to a + b + b#变量乘法tf.mul(a, b) # >> [6 12] because mul is element wise#矩阵mat乘法a和b是常量所以计算错误了tf.matmul(a, b) # >> ValueError#对a和b改变类型tf.matmul(tf.reshape(a, [1, 2]), tf.reshape(b, [2, 1])) # >> [[18]]#除 取余tf.div(a, b) # >> [1 3] tf.mod(a, b) # >> [1 0]

TensorFlow 数据类型
这里写图片描述

#0-d tensor, or "scalar" t_0 = 19 tf.zeros_like(t_0) # ==> 0tf.ones_like(t_0) # ==> 1#1-d tensor, or "vector"t_1 = ['apple', 'peach', 'banana']tf.zeros_like(t_1) # ==> ???????#2x2 tensor, or "matrix"t_2 = [[True, False, False],       [False, False, True],       [False, True, False]] tf.zeros_like(t_2) # ==> 2x2 tensor, all elements are Falsetf.ones_like(t_2) # ==> 2x2 tensor, all elements are True

TF数据类型和NP数据类型对比

tf.int32 == np.int32 # Truetf.ones([2, 2], np.float32) # ⇒ [[1.0 1.0], [1.0 1.0]]#tf.Session.run(fetches)#如果是张量的输入,输出结果将会是numpy的数组类型

常量存在的问题

import tensorflow as tfmy_const = tf.constant([1.0, 2.0], name="my_const")with tf.Session() as sess:    print sess.graph.as_graph_def()# you will see value of my_const stored in the graph’s definition#变量保存在图的定义中,如果变量值很大就会导致加载图的时候开销很大,所以只在初始化的时候使用常量、

变量Variables-tf.Variable()
tf.Variable是一个类,tf.constant是操作符,所以V大写。tf.Variable有自己的操作符号,并且必须要初始化变量使之生效。比如:

x = tf.Variable(...) x.initializer # init opx.value() # read opx.assign(...) # write opx.assign_add(...) # and more#*init = tf.global_variables_initializer()*#所有变量都初始化with tf.Session() as sess:    sess.run(init)#部分变量初始化init_ab = tf.variables_initializer([a, b], name="init_ab")with tf.Session() as sess:    sess.run(init_ab)#初始化一个变量Initialize a single variableW = tf.Variable(tf.zeros([784,10]))with tf.Session() as sess:sess.run(W.initializer)
# create variable a with scalar value用一个标量来创建变量a = tf.Variable(2, name="scalar") # create variable b as a vector向量初始化变量b = tf.Variable([2, 3], name="vector") # create variable c as a 2x2 matrix矩阵初始化变量c = tf.Variable([[0, 1], [2, 3]], name="matrix") # create variable W as 784 x 10 tensor, filled with zeros张量初始化变量W = tf.Variable(tf.zeros([784,10]))

Eval() a variable获取返回值

# W is a random 700 x 100 variable objectW = tf.Variable(tf.truncated_normal([700, 10]))with tf.Session() as sess:sess.run(W.initializer)print W>> Tensor("Variable/read:0", shape=(700, 10), dtype=float32)
# W is a random 700 x 100 variable objectW = tf.Variable(tf.truncated_normal([700, 10]))with tf.Session() as sess:sess.run(W.initializer)print W.eval()>> [[-0.76781619 -0.67020458  1.15333688 ..., -0.98434633 -1.25692499  -0.90904623] [-0.36763489 -0.65037876 -1.52936983 ...,  0.19320194 -0.38379928   0.44387451] [ 0.12510735 -0.82649058  0.4321366  ..., -0.3816964   0.70466036   1.33211911] ...,  [ 0.9203397  -0.99590844  0.76853162 ..., -0.74290705  0.37568584   0.64072722] [-0.12753558  0.52571583  1.03265858 ...,  0.59978199 -0.91293705  -0.02646019] [ 0.19076447 -0.62968266 -1.97970271 ..., -1.48389161  0.68170643   1.46369624]]
阅读全文
0 0
原创粉丝点击