这里介绍两种常用的方法
def load_data():
#加载MNIST数据集
(x,y),(x_val,y_val)=datasets.minst.load_data()
#转换为浮点张量,并缩放到-1~1
x = tf.convert_to_tensor(x,dtype=tf.float32)/255.
#转换为整形张量
y = tf.convert_to_tensor(y,dtype=tf.int32)
#one_hot编码
y = tf.one_hot(y,depth=10)
方法2:
def preprocess(x,y):
x = tf.cast(x,dtype=tf.float32)/255.
y = tf.cast(y.dtype=tf.int32)
return x,y