﻿ tensorflow学习笔记二：入门基础-一起大数据

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

from http://www.cnblogs.com/denny402/p/5852083.html

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.initialize_all_variables()`

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.initialize_all_variables()
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.mul(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.mul(5.0,a),tf.mul(4.0,b))`