## tensorflow学习笔记一：入门基础

TensorFlow用张量这种数据结构来表示所有的数据。用一阶张量来表示向量，如：v = [1.2, 2.3, 3.5] ，如二阶张量表示矩阵，如：m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]，可以看成是方括号嵌套的层数。

1、编辑器

2、常量

import tensorflow as tf

a=tf.constant(10)

3、变量

x=tf.Variable(tf.ones([3,3]))
y=tf.Variable(tf.zeros([3,3]))

init=tf.global_variables_initializer()

import tensorflow as tf
import numpy as np
x=np.array([[1,1,1],[1,-8,1],[1,1,1]])
w=tf.Variable(initial_value=x)
sess=tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(w))

4、占位符

x = tf.placeholder(tf.float32, [None, 784])

5、图(graph)

x=3
y=2
z=x+y
print(z)

import tensorflow as tf
x = tf.Variable(3)
y = tf.Variable(5)
z=x+y
init =tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)
print(sess.run(z))

Session需要先创建，使用完后还需要释放。因此我们使用with...as..语句，让系统自动释放。

import tensorflow as tf
word=tf.constant('hello,world!')
with tf.Session() as sess:
print(sess.run(word))

import tensorflow as tf
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
mul = tf.multiply(a, b)

with tf.Session() as sess:
print('a*b=',sess.run(mul, feed_dict={a: 2, b: 3}))

import tensorflow as tf
a=tf.Variable(tf.ones([3,2]))
b=tf.Variable(tf.ones([2,3]))
product=tf.matmul(5*a,4*b)
init=tf.initialize_all_variables()

with tf.Session() as sess:
sess.run(init)
print(sess.run(product))

其中

product=tf.matmul(5*a,4*b)

product=tf.matmul(tf.multiply(5.0,a),tf.multiply(4.0,b))