函数意义解释
相加tf.add()
# 两个实数相加
print(tf.add(1, 2))
>>>tf.Tensor(3, shape=(), dtype=int32)
两个张量相加
print(tf.add([1, 2], [3, 4]))
>>>tf.Tensor([4 6], shape=(2,), dtype=int32)
平方tf.square()
# 单个元素平方
tf.square(5)
>>>tf.Tensor(25, shape=(), dtype=int32)
# 张量平方
tf.square([1, 2])
>>>tf.Tensor([1 4], shape=(2,), dtype=int32)
# 多维张量平方
tf.square([[1, 2], [3, 4]])
>>>tf.Tensor(
[[ 1 4]
[ 9 16]], shape=(2, 2), dtype=int32)
# 降维累加求和
tf.reduce_sum([[1, 2, 3],[1, 2, 3]])
>>>tf.Tensor(12, shape=(), dtype=int32)
# 外部操作也可以
tf.add([1, 2], [3, 4]) + tf.add([1, 2], [3, 4])
>>>tf.Tensor([ 8 12], shape=(2,), dtype=int32)
# 张量乘积,跟矩阵一样
x = tf.matmul([[1]], [[2, 3]])
>>>tf.Tensor([[2 3]], shape=(1, 2), dtype=int32)
tf.matmul([[1],[2]], [[2, 3]])
>>>tf.Tensor(
[[2 3]
[4 6]], shape=(2, 2), dtype=int32)
x.shape
>>>(1, 2)
x.dtype
>>><dtype: 'int32'>
import numpy as np
# 生成单位矩阵
ndarray = np.ones([3, 3])
# 矩阵乘上一个元素
tensor = tf.multiply(ndarray, 42)
>>>tf.Tensor(
[[42. 42. 42.]
[42. 42. 42.]
[42. 42. 42.]], shape=(3, 3), dtype=float64)
# 将元素放在前面,前后顺序不一样,整型与浮点型依据第一个决定
tensor = tf.multiply(42,ndarray)
>>>tf.Tensor(
[[42 42 42]
[42 42 42]
[42 42 42]], shape=(3, 3), dtype=int32)
# 张量与元素相加
np.add(tensor, 1)
>>>[[43. 43. 43.]
[43. 43. 43.]
[43. 43. 43.]]
# 将张量转向数组
tensor.numpy()
>>>array([[42., 42., 42.],
[42., 42., 42.],
[42., 42., 42.]])
判断GPU是否可用
x = tf.random.uniform([3, 3])
print("Is there a GPU available: "),
print(tf.config.experimental.list_physical_devices("GPU"))
print("Is the Tensor on GPU #0: "),
print(x.device.endswith('GPU:0'))
强力选择代码在GPU或CPU上执行
import time
def time_matmul(x):
start = time.time()
for loop in range(10):
tf.matmul(x, x)
result = time.time()-start
print("10 loops: {:0.2f}ms".format(1000*result))
# Force execution on CPU
print("On CPU:")
with tf.device("CPU:0"):
x = tf.random.uniform([1000, 1000])
assert x.device.endswith("CPU:0")
time_matmul(x)
# Force execution on GPU #0 if available
if tf.config.experimental.list_physical_devices("GPU"):
print("On GPU:")
with tf.device("GPU:0"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.
x = tf.random.uniform([1000, 1000])
assert x.device.endswith("GPU:0")
time_matmul(x)
https://tensorflow.google.cn/tutorials/customization/basics
https://www.pyimagesearch.com/start-here/
自定义layers层
https://tensorflow.google.cn/tutorials/customization/custom_layers
class MyDenseLayer(tf.keras.layers.Layer):
def __init__(self, num_outputs):
super(MyDenseLayer, self).__init__()
self.num_outputs = num_outputs
def build(self, input_shape):
self.kernel = self.add_weight("kernel",
shape=[int(input_shape[-1]),
self.num_outputs])
def call(self, input):
return tf.matmul(input, self.kernel)
layer = MyDenseLayer(10)
构建一个ResNet模型
class ResnetIdentityBlock(tf.keras.Model):
def __init__(self, kernel_size, filters):
super(ResnetIdentityBlock, self).__init__(name='')
filters1, filters2, filters3 = filters
self.conv2a = tf.keras.layers.Conv2D(filters1, (1, 1))
self.bn2a = tf.keras.layers.BatchNormalization()
self.conv2b = tf.keras.layers.Conv2D(filters2, kernel_size, padding='same')
self.bn2b = tf.keras.layers.BatchNormalization()
self.conv2c = tf.keras.layers.Conv2D(filters3, (1, 1))
self.bn2c = tf.keras.layers.BatchNormalization()
def call(self, input_tensor, training=False):
x = self.conv2a(input_tensor)
x = self.bn2a(x, training=training)
x = tf.nn.relu(x)
x = self.conv2b(x)
x = self.bn2b(x, training=training)
x = tf.nn.relu(x)
x = self.conv2c(x)
x = self.bn2c(x, training=training)
x += input_tensor
return tf.nn.relu(x)
block = ResnetIdentityBlock(1, [1, 2, 3])
https://tensorflow.google.cn/tutorials/customization/custom_layers