tf的基本函数介绍点击打开链接
下面的样例取自tensorflow中文社区
有自己实验的注释
样例1
import tensorflow as tf matrix1 = tf.constant([[3., 3.]]) matrix2 = tf.constant([[2.],[2.]]) product = tf.matmul(matrix1, matrix2) sess = tf.Session() result = sess.run(product) print result sess.close() 上面申明了两个矩阵op,然后定义了一个矩阵乘法,都是tf写好的函数,然后用一个回话运行即可run里面的参数即是要返回的结果,可以用[ans1,ans2..]查看多个结果,包括中间变量
样例2
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b') c = tf.matmul(a, b) sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) print sess.run(c)同样申明两个矩阵,但是用了shape可以讲数组转化成矩阵,这个就是定义维数,如果是-1就是系统自己计算当前维数
样例3
def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) #important print sess.run([b_conv1,W_conv1])第一个函数加了一个标准差为0.1的噪声,类型是32float,在0附近
第二个就初始为1*32的全部0.1的矩阵 注意有变量所以必须先调用初始化函数即代码中的important部分
