tf.cast()类型转换
https://www.tensorflow.org/api_docs/python/tf/cast?version=nightly
tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换,比如读入的图片如果是int8类型的,一般在要在训练前把图像的数据格式转换为float32。
import tensorflow as tf
import numpy as np
(x_train, _), (x_test, _) = tf.keras.datasets.mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], -1)
x_test = x_test.reshape(x_test.shape[0], -1)
x_train = tf.cast(x_train, tf.float32) / np.max(x_train)
x_test = tf.cast(x_test, tf.float32) / np.max(x_train)
x_train.shape, x_test.shape
(TensorShape([60000, 784]), TensorShape(</