kereas 实现鸢尾花分类
import tensorflow as tf from sklearn import datasets import numpy as np x_train=datasets.load_iris().data y_train=datasets.load_iris().target np.random.seed(116) np.random.shuffle(x_train) np.random.seed(116) np.random.shuffle(y_train) tf.random.set_seed(116) model=tf.keras.models.Sequential([tf.keras.layers.Dense(3,activation='softmax',kernel_regularizer=tf.keras.regularizers.l2())]) model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),#由于末端使用了softmax函数,使输出是概率分布而不是原始输出,故选FALSE metrics=['sparse_categorical_accuracy']) #由于鸢尾花数据集给的标签是0/1/2,是数值,神经网络前向传播的输出是概率分布,故选择'sparse_categorical_accuracy' model.fit(x_train,y_train,batch_size=32,epochs=500,validation_split=0.2,validation_freq=40) model.summary()