12.12 内容:
程序咀嚼:
1. 构筑图gragh, 这一点和python的Numpy不同,graph 为静态的,需要启动一个session。
graph 代表一个计算任务,在模式运行时,整个程序的结构,“图”在会话里被启动。
session将图的节点操作发布到CPU GPU 上,同时提供OP方法(Operation),专门运算的操作节点,每一个操作都是op。
关于tensorflow:
tensorflow 很荣部署到服务器上,这是很多框架做不到的。
大量优秀的项目正在使用tensorflow,可以在多个CPU和GPU下并行。
不足之处:
回归正文:
placeholder: 启动session时程序会真正的运行。python程序的底层为C语言或者其他语言,执行一行脚本,就是有成本的,tensorflow通过计算刘图的方式,优化整个sesion需要执行的代码,还是很有优势的。所以placeholder()函数是在神经网络构建graph时候的模型占位,此时并没有把要输入的数据传入模型,它只会分配必要的内存。等建立session,在会话中,运行模型的时候通过feed_dict()函数 向占位符喂入数据。
学习时,使用了如下示例:
import tensorflow as tf
import numpy as np
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:[3.], input2: [4.]}))
/home/iser/anaconda3/envs/tensorflow-learning/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:502: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
2020-12-12 21:58:52.727793: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 AVX512F FMA
2020-12-12 21:58:52.966041: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1105] Found device 0 with properties:
name: GeForce RTX 2080 Ti major: 7 minor: 5 memoryClockRate(GHz): 1.575
pciBusID: 0000:65:00.0
totalMemory: 10.76GiB freeMemory: 9.80GiB
2020-12-12 21:58:52.966082: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1195] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce RTX 2080 Ti, pci bus id: 0000:65:00.0, compute capability: 7.5)
[12.]
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)
with tf.Session() as sess:
#print(sess.run(y)) # ERROR:此处x还没有赋值
rand_array = np.random.rand(1024, 1024)
print(sess.run(y, feed_dict={x: rand_array}))
2020-12-12 22:05:01.645065: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 AVX512F FMA
2020-12-12 22:05:01.791864: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1105] Found device 0 with properties:
name: GeForce RTX 2080 Ti major: 7 minor: 5 memoryClockRate(GHz): 1.575
pciBusID: 0000:65:00.0
totalMemory: 10.76GiB freeMemory: 9.78GiB
2020-12-12 22:05:01.791898: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1195] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce RTX 2080 Ti, pci bus id: 0000:65:00.0, compute capability: 7.5)
[[264.88196 256.2706 258.85715 ... 252.68927 253.2667 256.4321 ]
[262.64746 253.23653 254.83377 ... 244.76108 246.85315 255.17625]
[263.8545 260.51007 263.09048 ... 252.3399 256.21805 263.8079 ]
...
[274.35175 263.1493 259.98703 ... 252.62515 259.94763 259.50565]
[264.54214 254.08362 254.66122 ... 249.22263 252.54991 254.24026]
[262.92407 254.49603 258.49493 ... 248.71774 258.1954 259.60175]]
2. tensorflow 中使用变量类型
float64
float2
int32
3. 冒号表示 取值范围。
在使用python数组时,被里面的冒号搞晕了,现在做个简单记录
原始数据
import numpy as np
x=np.array([[1,2,3],[5,6,7],[7,8,9]])
x
第一个冒号
如果出现了这样的冒号 x[:,0]
大家可以猜出下返回的结果是什么?
结果是
array([1, 5, 7])
这个冒号代表了获取了x的所有的行数据,之后逗号后面的0,是获取0列。
扩展举例
例如x[:,0:2],返回值为
array([[1, 2], [5, 6], [7, 8]])
如果出现了x[0:2,0:2],返回值为
array([[1, 2], [5, 6]])
第二个冒号
如果出现了这样的冒号 x[:,::-1]
大家可以猜测下返回的结果
array([[3, 2, 1], [7, 6, 5], [9, 8, 7]])
结果出现了逆序