第三讲 神经网络八股--用keras分类iris

 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 
10 np.random.seed(116)
11 np.random.shuffle(x_train)
12 np.random.seed(116)
13 np.random.shuffle(y_train)
14 tf.random.set_seed(116)
15 
16 
17 model = tf.keras.models.Sequential([
18         tf.keras.layers.Dense(3, activation='softmax',
19         kernel_regularizer=tf.keras.regularizers.l2())
20 ])
21 
22 model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
23               loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
24               metrics=['sparse_categorical_accuracy'])
25 
26 model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
27 
28 
29 model.summary()

 

上一篇:11.分类与监督学习,朴素贝叶斯分类算法


下一篇:11.分类与监督学习,朴素贝叶斯分类算法