1 import tensorflow as tf
 2 from sklearn import datasets
 3 import numpy as np
 4 
 5 # 数据集导入
 6 x_train = datasets.load_iris().data
 7 y_train = datasets.load_iris().target
 8 # 数据集乱序
 9 np.random.shuffle(x_train)
10 np.random.shuffle(y_train)
11 # 在Sequntial中搭建网络结构
12 model = tf.keras.models.Sequential([
13     tf.keras.layers.Dense(3, activation=\'softmax\', kernel_regularizer=tf.keras.regularizers.l2())
14 ])
15 
16 model.compile(
17     optimizer=tf.keras.optimizers.SGD(lr=0.1),
18     loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
19     metrics=[\'sparse_categorical_accuracy\']
20 )
21 #         x_labels y_labels batch大小      迭代次数     20%作为测试集          20次迭代训练一次
22 model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
23 #  输出参数
24 model.summary()