tensorflow学习笔记

张量

import tensorflow as tf
#创建常量
rank_0_tensor = tf.constant(4)   
print(rank_0_tensor)
#创建一阶张量(向量)
rank_1_tensor = tf.constant([2.0, 3.0, 4.0])   
print(rank_1_tensor)
#创建二阶张量(向量)
rank_2_tensor = tf.constant([[1, 2],
                             [3, 4],
                             [5, 6]], dtype=tf.float16)
print(rank_2_tensor)
#创建三阶张量(向量)
rank_3_tensor = tf.constant([
  [[0, 1, 2, 3, 4],
   [5, 6, 7, 8, 9]],
  [[10, 11, 12, 13, 14],
   [15, 16, 17, 18, 19]],
  [[20, 21, 22, 23, 24],
   [25, 26, 27, 28, 29]],])

print(rank_3_tensor)

 

 

 

 张量的运算

a = tf.constant([[1, 2],
                 [3, 4]])
b = tf.constant([[1, 1],
                 [1, 1]])

print(tf.add(a, b), "\n")  #加法
print(tf.multiply(a, b), "\n")  #逐元素乘法
print(tf.matmul(a, b), "\n")  #矩阵乘法

 

形状操作

tf.shape([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]],name=None)  #返回数据的shape

tf.Tensor([2 2 3], shape=(3,), dtype=int32)

tf.size([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]], name=None) #返回数据的元素数量
tf.Tensor(12, shape=(), dtype=int32)
tf.rank([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]],name=None) #返回tensor的rank

tf.Tensor(3, shape=(), dtype=int32)

 

切片

input=[[[1, 1, 1], [2, 2, 2]],[[3, 3, 3], [4, 4, 4]],[[5, 5, 5], [6, 6, 6]]]
print(tf.slice(input, [1, 0, 0], [2, 1, 3]))

tf.Tensor(
[[[3 3 3]]

[[5 5 5]]], shape=(2, 1, 3), dtype=int32)

 

激活函数

tf.nn.relu(features, name=None)
tf.sigmoid(x, name=None)
tf.tanh(x, name=None)

卷积函数

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)
tf.nn.conv3d(input, filter, strides, padding, name=None)

池化函数

tf.nn.avg_pool(value, ksize, strides, padding, data_format=’NHWC’, name=None)    #平均方式池化
tf.nn.max_pool(value, ksize, strides, padding, data_format=’NHWC’, name=None)    #最大值方法池化

 

posted @ 2022-04-24 22:39  untitied  阅读(37)  评论(0)    收藏  举报