框架tensorflow1

TensorFlow   1

分类: 1,protocol Buffer  处理结构化数据工具; (xml,json)

   2,Bazel        自动化构建工具, 编译;

 

tensor 张量;    张量就是多维数组;

flow  流;

 

两个阶段:1   定义计算图中所有的计算;  

      2,执行计算;

张量tensor:

  1,多维数组;

   2,零阶张量表示标量(scalar),就是一个数;

  3,一阶张量表示为向量(vector), 是一维数组;

  。。。。。

     第n阶 --------------------------------n 维数组;

一个张量主要保存三个属性; name  名字、  shape 维度、     type  类型  ;

张量的使用:

  用途分类:1, 对中间计算结果的引用;

      2, 当计算图构造完成之后,张量可以用来获得计算结果,也就是得到真实数字;

1, 练习

 

[root@shenzhen ~]# cat /server/tensorflow/tensor1.py
#!/usr/local/bin/python3
#coding:utf-8

import tensorflow as tf
import numpy as np

#create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

###create tensorflow structure start###
Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights*x_data + biases

loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

#init = tf.initialize_all_variables()
init = tf.global_variables_initializer()
###create tensorflow structure start###

sess = tf.Session()
sess.run(init)  #very important

for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(Weights), sess.run(biases))




[root@shenzhen tensorflow]# python3 tensor1.py
2018-08-20 20:58:14.585672: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
0 [0.5822645] [0.00799593]
20 [0.27060172] [0.19762385]
40 [0.16020724] [0.26387033]
60 [0.12124781] [0.28724945]
80 [0.10749861] [0.2955002]
100 [0.10264634] [0.29841197]
120 [0.10093395] [0.29943955]
140 [0.10032961] [0.2998022]
160 [0.10011631] [0.2999302]
180 [0.10004105] [0.2999754]
200 [0.10001447] [0.29999134]
View Code

 

2,Session  会话 : 运行模型

[root@shenzhen tensorflow]# python3 tensor2.py
2018-08-21 20:02:22.863676: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
[[12]]
[root@shenzhen tensorflow]# vim tensor2.py
[root@shenzhen tensorflow]# python3 tensor2.py
2018-08-21 20:05:11.457492: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
[[12]]
[root@shenzhen tensorflow]# cat tensor2.py 
#!/usr/local/bin/python3
#coding:utf-8

import tensorflow as tf

matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],
                       [2]])
product = tf.matmul(matrix1,matrix2) #matrix multiply np.dot(m1,m2)

#method1
#sess = tf.Session()
#result = sess.run(product)
#print(result)
#sess.close()

#method2
with tf.Session() as sess:
    result2 = sess.run(product)
    print(result2)

 

3, tensorflow   变量

创建变量;

[root@shenzhen tensorflow]# python3 tensor3.py
2018-08-21 20:48:45.724496: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
1
2
3
[root@shenzhen tensorflow]# cat tensor3.py
#!/usr/local/bin/python3
#coding:utf-8

import tensorflow as tf

state = tf.Variable(0, name='counter')
#print(state.name)
one = tf.constant(1)

new_value = tf.add(state, one)
update = tf.assign(state, new_value)

init = tf.global_variables_initializer() #must have if define variable

with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

 

tensorflow 传入值:

 

[root@shenzhen tensorflow]# python3 tensor4.py
2018-08-22 19:39:44.495986: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
[21.]
[root@shenzhen tensorflow]# cat tensor4.py
#!/usr/local/bin/python3
#coding:utf-8

import tensorflow as tf

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

output = tf.multiply(input1,input2)

with tf.Session() as sess:
    print(sess.run(output, feed_dict={input1:[7.], input2:[3.]}))

 

posted on 2018-08-20 21:05  微子天明  阅读(156)  评论(0编辑  收藏  举报

导航