Tensorflow2常用操作合集

Tensorflow2常用操作合集



Tensorflow2相对于Tensorflow1的提升就像python3相对于python2的提升一样。相信在不就的将来Tensorflow2会全面的代替Tensorflow。本篇博客总结了在使用Tensorflow2的过程中常用的操作

Data set loading and preprocessing

数据集的加载和预处理的主要步骤就是:

数据集加载(自己的数据集或tf2自带的数据集)==> 转化为Dataset对象 ==> 训练集随机打散,测试集不用打散 ==> 训练集设置批训练 ==> 设置数据集预处理函数 ==> 训练集可以进行数据复制增大数据量

这里以加载minist手写数据集为例

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets
def preprocess(x, y):
    """
    预处理函数
    """
    # [b, 28, 28], [b]
    print(x.shape, y.shape)
    x = tf.cast(x, dtype=tf.float32) / 255.
    x = tf.reshape(x, [-1, 28 * 28])  # 将图片打平
    y = tf.cast(y, dtype=tf.int32)
    y = tf.one_hot(y, depth=10)
    return x, y
# set batch size
batch_size = 512
# load minist data
(x,y),(x_test,y_test) = datasets.mnist.load_data()
# build tf dataset
train_db = tf.data.Dataset.from_tensor_slices((x,y))
train_db = train_db.shuffle(10000).batch(batch_size).map(preprocess)

Data set training

the steps of training data are:

  1. load Network
  2. set the input shape
  3. initialize the optimizer
  4. traverse the training data
  5. calculate the gradient
  6. use test data to calculate the degree of accuracy

for example

learning_rate = 0.01
# load Network
model = Network()
model.build(input_shape(4,9))
# initialize the oprimizer
optimitzer = tf.keras.optimitzers.RMSprop(learning_rate)
for epoch in range(200):
	for step , (x,y) in enumerate(train_db):
		# calculate the gradient
		with tf.GradientTape() as tape:
			out = model(x)
			loss = tf.reduce_mean(losses.MSE(y,out))
			mae_loss = tf.reduce_mean(losses.MAE(y,out))
		if step % 10 == 0:
			print(epoch,step,float(loss))
		grads = tape.gradient(loss,model.trainable_variables)
		opimizer.apply_gradients=(zip(grads,model,trainable_variables))
		if step % 10  == 0:
			# calculate the degree of accuracy
			for x,y in test_db:
				out = model(x)
				pred = tf.argmax(out,axis=1)
				total, total_correct = 0., 0
				# convert one_hot y to number y
				y = tf.argmax(y,axis=1)
				# bool type
				correct = tf.math.equal(pred,y)
				total_correct += tf.math.reduce_sum(tf.cast(correct,dtype=tf.int32)).numpy()
				total += x.shape[0]
			print(step, 'Evaluate Acc:', total_correct / total)
            accs.append(total_correct / total)
Notes: 
1.tf.math.argmax(
    input, axis=None, output_type=tf.dtypes.int64, name=None
)
Returns the index with the largest value across axes of a tensor.

2.tf.math.reduce_sum(
    input_tensor, axis=None, keepdims=False, name=None
)
Computes the sum of elements across dimensions of a tensor.

3.tf.math.equal(
    x, y, name=None
)
Returns the truth value of (x == y) element-wise. it likes as mask of the tensor

4. enumerate()
Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops or be converted into a list of tuples using list() method.

Set up the Network

frequently-used code

Notes frequently-user layers

from tensorflow.keras import layers

1.Fully connected layer

layers.Dense(
    units, activation=None
)

Just your regular densely-connected NN layer.
Arguments:

  • units: Positive integer, dimensionality of the output space.
  • activation: Activation function to use. If you don’t specify
    anything, no activation is applied (ie. “linear” activation: a(x) = x

2.Convolution layer

layers.Densee(
    filters, kernel_size, strides=(1, 1), padding='valid',activation=None
)

3.2D convolution layer (e.g. spatial convolution over images)

Arugment:

  • filters: Integer, the dimensionality of the output space (i.e. the
    number of output filters in the convolution).
  • kernel_size: An integer or tuple/list of 2 integers, specifying the
    height and width of the 2D convolution window. Can be a single
    integer to specify the same value for all spatial dimensions.
  • strides: An integer or tuple/list of 2 integers, specifying the
    strides of the convolution along the height and width. Can be a
    single integer to specify the same value for all spatial dimensions.
    Specifying any stride value != 1 is incompatible with specifying any
    dilation_rate value != 1.
  • padding: one of “valid” or “same” (case-insensitive).
  • activation: Activation function to use. If you don’t specify
    anything, no activation is applied (ie. “linear” activation: a(x) =
    x).

3.Conv2DTranspose layer

layers.Conv2DTranspose(
	filters, kernel_size, strides=(1, 1), padding='valid',activation=None
)

Arguments:

  • filters: Integer, the dimensionality of the output space (i.e. the
    number of output filters in the convolution).
  • kernel_size: An integer or tuple/list of 2 integers, specifying the
    height and width of the 2D convolution window. Can be a single
    integer to specify the same value for all spatial dimensions.
  • strides: An integer or tuple/list of 2 integers, specifying the
    strides of the convolution along the height and width. Can be a
    single integer to specify the same value for all spatial dimensions.
    Specifying any stride value != 1 is incompatible with specifying any
    dilation_rate value != 1.
  • padding: one of “valid” or “same” (case-insensitive).

4.MaxPool2D layer

Max pooling operation for spatial data.

layers.MaxPool2D(
	pool_size=(2, 2), strides=None, padding='valid'
)
  • pool_size: integer or tuple of 2 integers, factors by which to
    downscale (vertical, horizontal). (2, 2) will halve the input in both
    spatial dimension. If only one integer is specified, the same window
    length will be used for both dimensions.
  • strides: Integer, tuple of 2 integers, or None. Strides values. If
    None, it will default to pool_size.
  • padding: One of “valid” or “same” (case-insensitive).

Activation function

1.tf.nn.leaky_relu(features, alpha=0.2, name=None)

Args:

  • features: A Tensor representing preactivation values. Must be one of
    the following types: float16, float32, float64, int32, int64.
  • alpha: Slope of the activation function at x < 0.
  • name: A name for the operation (optional).

2.tf.nn.relu(features, name=None)

Args:

  • features: A Tensor. Must be one of the following types: float32,
    float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half,
    uint32, uint64, qint8.
  • name: A name for the operation (optional).

3.tf.nn.sigmoid_cross_entropy_with_logits(labels=None, logits=None, name=None)

Args:

  • logits: A non-empty Tensor. Must be one of the following types: half,
    float32, float64.

  • axis: The dimension softmax would be performed on. The default is -1
    which indicates the last dimension.

  • name: A name for the operation (optional).

Tensor operation

tensor operation has written in my other boke

原创文章 113 获赞 80 访问量 3万+

猜你喜欢

转载自blog.csdn.net/python_LC_nohtyp/article/details/105273756