建设营销型网站多少钱,自学做网站可以吗,天津建设网网站打不开,wordpress游客变注册用户计算图中的操作
# python 3.6
import tensorflow as tf
import numpy as npsess tf.Session()# 将张量和占位符对象组成一个计算图#xff0c;创建一个简单的分类器# 一、计算图中的操作
# 1. 声明张量和占位符#xff0c;创建numpy数组#xff0c;传入计算图操作
x_vals …计算图中的操作
# python 3.6
import tensorflow as tf
import numpy as npsess tf.Session()# 将张量和占位符对象组成一个计算图创建一个简单的分类器# 一、计算图中的操作
# 1. 声明张量和占位符创建numpy数组传入计算图操作
x_vals np.array([1.,3.,5.,7.,9.])
x_data tf.placeholder(tf.float32)
m_const tf.constant(3.)
my_product tf.multiply(x_data, m_const)
for x_val in x_vals:print(sess.run(my_product, feed_dict{x_data: x_val}))
#输出:
# 3.0
# 9.0
# 15.0
# 21.0
# 27.0计算图中的操作
工作原理
嵌入Layer
# python 3.6
import tensorflow as tf
import numpy as npsess tf.Session()
# 二、TensorFlow的嵌入layer
# 在同一个计算图中进行多个乘法操作
# 1. 创建数据和占位符
array np.array([[1.,3.,5.,7.,9.],[2.,4.,6.,8.,10.],[-4,-2,0,1,2]]) # 3×5的矩阵
x_vals np.array([array, array1]) # 两个3×5的矩阵
# 如果未知占位符的维度可以设为None shape(3,None)
x_data tf.placeholder(tf.float32,shape(3,5))# 2. 创建常量矩阵
c1 tf.constant([[1.],[0.],[-1.],[3.],[2.]])
c2 tf.constant([[2.]])
c3 tf.constant([[10.]])# 3. 声明计算图操作
prod1 tf.matmul(x_data, c1) # (3,5) × (5×1) (3,1)
prod2 tf.matmul(prod1, c2) # (3,1) × (1×1) (3,1)
add1 tf.add(prod2, c3) #
print(prod2)
print(c3)
print(add1)
# 4. 计算图赋值
for x_val in x_vals:print(sess.run(add1,feed_dict{x_data:x_val}))# print(----)# print(sess.run(prod2,feed_dict{x_data:x_val}))# print(----)# print(sess.run(c3,feed_dict{x_data:x_val}))# print(----)# print(sess.run(add1,feed_dict{x_data:x_val}))
原理
多层Layer
# 三、TensorFlow的多层Layer
# 如何连接传播数据的多层layer
# 1. 通过numpy创建2D图像4×4像素图片。
# 将创建成四维第一维和最后一维大小为1。
# 注意TensorFlow的图像函数是处理四维图片的
# 这四维是图片数量1、高度、宽度和颜色通道1。
# 这里是一张图片单颜色通道所以设两个维度值为1
x_shape [1,4,4,1]
x_val np.random.uniform(sizex_shape)# 2. 创建占位符用来传入图片
x_data tf.placeholder(tf.float32,shapex_shape)# 3. 创建过滤4×4像素图片的滑动窗口
# 用conv2d()卷积2×2形状的常量窗口传入滑动窗口、过滤器和步长
# 窗口大小2×2 步长2
# 为了计算平均值用常量为0.25的向量与2×2窗口卷积
filter tf.constant(0.25, shape[2,2,1,1])
strides [1,2,2,1]
mov_avg_layer tf.nn.conv2d(x_data,filter,strides,paddingSAME, nameMoving_Avg_Window)# 4. 自定义layer操作滑动窗口平均的2×2的返回值
# 输入张量乘以2×2的矩阵张量 然后每个元素加1
# 因为矩阵乘法只计算二维矩阵所以剪裁squeeze()图形的多余维度
def custom_layer(input_matrix):input_matrix_sqeezed tf.squeeze(input_matrix)a tf.constant([[1.,2.],[-1.,3.]])b tf.constant(1.,shape[2,2])temp1 tf.matmul(a, input_matrix_sqeezed)temp tf.add(temp1,b)return tf.sigmoid(temp)# 5. 将自定义的layer加入计算图用tf.name.scope命名唯一的layer名字
# 后续在计算图中可以折叠或者扩展Custom_Layer
with tf.name_scope(Custom_Layer) as scope:custom_layer1 custom_layer(mov_avg_layer)# 6. 传入4×4的像素图片执行计算图
print(sess.run(custom_layer1, feed_dict{x_data:x_val}))原理