tensorflow 前向传播 2019.07.19

张量: 多维数组(列表)     阶: 张量的维数

import tensorflow as tf
a = tf.constant([[1.9, 2.0]])
b = tf.constant([[3.9], [2.9]])
res = tf.matmul(a, b)
print res
//with tf.Session() as sess:    加上这一句显示答案
//   print sess.run()

参数: 神经元线上的权重 随机给初值

                 标准差为2   均值为0     随之种子 可以省略

w = tf.Variable(tf.random_nomal([2, 3], stddev = 2, mean = 0, seed = 1))

        正态分布          3*2的矩阵

tf.truncated_normal()                   tf.random_uniform()

去掉过大偏离的正态分布         平均分布

tf.zeros 全0数组    tf.zeros([3, 2], int32)

tf.ones 全1数组  tf.ones([3, 2], int32)

tf.fill 全定值数组  tf.fill([3, 2], 6)

tf.constant 直接给值  tf.constant([3, 2, 1])

变量初始化  计算图节点运算都要用会话(with结构) 实现

with tf.Session() as sess:
    sess.run()

 变量初始化: 在sess.run函数中用tf.global_variables_initializer()

init_op = tf.global_variables_initializer()
sess.run(init_op)

 计算图节点运算: 在sess.run函数中写入待运算的节点

sess.run(y)

用tf.placeholder 占位, 在sess.run函数中用feed_dict 喂数据

喂一组数据:

x = tf.placeholder(tf.float32, shape = (1, 2))

sess.run(y, feed_dict = {x : [[0.5, 0.6]]})

喂多组数据:

x = tf.placeholder(tf.float32, shap(None, 2))

sess.run(y, feed_dict = {x : [[0.1, 0.2], [0.2, 0.3] , [0.3, 0.4]]})

  1 #coding:utf-8
  2 import tensorflow as tf
  3 #定义输入参数
  4 x = tf.placeholder(tf.float32, shape = (None, 2))
  5 w1 = tf.Variable(tf.random_normal([2, 3]))
  6 w2 = tf.Variable(tf.random_normal([3, 1]))
  7 
  8 #定义前向传播过程
  9 a = tf.matmul(x, w1)
 10 y = tf.matmul(a, w2)
 11 
 12 #用会话计算结果
 13 with tf.Session() as sess:
 14     init_op = tf.global_variables_initializer()
 15     sess.run(init_op)
 16     print sess.run(y, feed_dict = {x : [[0.7, 0.5], [0.1, 0.2]]})
原文地址:https://www.cnblogs.com/WTSRUVF/p/11211853.html