![7059dc08e60731a70c1caed2ddf78ba4.png](https://i-blog.csdnimg.cn/blog_migrate/ef6ce99ce85f23d16082336ae0669bde.jpeg)
![924bf663ac5e02107fe4314fa6477b93.png](https://i-blog.csdnimg.cn/blog_migrate/ef4934a98904dbea8992bf74e4cd61f5.jpeg)
回调函数:
![7c2821e7f173314d8a577c7146f9b815.png](https://i-blog.csdnimg.cn/blog_migrate/c458a758cee95f830daa201ee93d9c26.jpeg)
inputs = tf.keras.Input(shape=(32,)) #(batch_size=32,数据维度32),shape为改成接受的数据的维度
x = tf.keras.layers.Dense(64, activation='relu')(inputs) #(64个神经元,)
x = tf.keras.layers.Dense(64, activation='relu')(x)#(63个神经元)
predictions = tf.keras.layers.Dense(10)(x) #(输出是10类)
#- inputs(模型输入)
#- output(模型输出)
model = tf.keras.Model(inputs=inputs, outputs=predictions)
#指定损失函数 (loss) tf.keras.optimizers.RMSprop
#优化器 (optimizer) tf.keras.losses.SparseCategoricalCrossentropy
#指标 (metrics) ['accuracy']
model.compile(optimizer=tf.keras.optimizers.Adam(0.001), #优化器
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), #损失函数
metrics=['accuracy']) #评估函数
常用的优化器和损失函数:
https://www.tensorflow.org/guide/keras/train_and_evaluate#many_built-in_optimizers_losses_and_metrics_are_availablewww.tensorflow.org优化器:
- SGD() (有或没有动量)
- RMSprop()
- Adam()
- Adagrad
损失:
- MeanSquaredError()
- KLDivergence()
- CosineSimilarity()
- Hinge
指标:
- AUC()
- Precision()
- Recall()
- Poisson
另外,如果想用上述的默认设置,那么在很多情况下,可以通过字符串标识符指定优化器,损失和指标:
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
构建数据集:
import numpy as np
x_train = np.random.random((1000, 32))
y_train = np.random.randint(10, size=(1000, ))
x_val = np.random.random((200, 32))
y_val = np.random.randint(10, size=(200, ))
x_test = np.random.random((200, 32))
y_test = np.random.randint(10, size=(200, ))
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data= (x_val, y_val))
Help on method fit in module tensorflow.python.keras.engine.training:
fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1,