美文网首页
TensorFlow2-通过KerasModel创建模型

TensorFlow2-通过KerasModel创建模型

作者: MyChinaLin | 来源:发表于2020-07-27 22:01 被阅读0次

    本文介绍通过tf.keras.Model(inputs=input_x, outputs=pred_y),关系模型的输入、输出,建立任意模型结构的深度学习模型。

    一、模型结构信息流图

    二、导入依赖包

    # coding: utf-8

    import tensorflow as tf

    from tensorflow.keras import layers

    import numpy as np

    import os

    三、导入或者生成模型输入数据

    #训练数据导入trin_in = np.random.random((500, 128))

    trin_out = np.random.random((500, 13))

    dataset = tf.data.Dataset.from_tensor_slices((trin_in, trin_out))

    dataset = dataset.batch(100)

    dataset = dataset.repeat()

    #验证数据导入

    val_in = np.random.random((200, 128))

    val_out = np.random.random((200, 13))

    val_dataset = tf.data.Dataset.from_tensor_slices((val_in, val_out))

    val_dataset = val_dataset.batch(50)

    val_dataset = val_dataset.repeat()

    四、配置模型信息流结构

    #配置模型样式input_x = tf.keras.Input(shape=(128,))

    #正则化可选参数:regularizers.l1(0.03)、regularizers.l2(0.02)、#regularizers.l1_l2(l1=0.01, l2=0.04)

    layer_1 = layers.Dense(384, activation='tanh',

            kernel_initializer='RandomUniform',

            kernel_regularizer=tf.keras.regularizers.l2(0.02),

            bias_initializer="RandomNormal",

            bias_regularizer=tf.keras.regularizers.l1(0.03),name='layer_1')(input_x)

    layer_2 = layers.Dense(128, activation='elu', name='layer_2')(layer_1)

    #任意网络图形,注意:维度

    layer_3 = layers.Dense(64, activation='softplus',name='layer_3')(layer_2+input_x)

    pred_y = layers.Dense(13, activation='softmax',

                activity_regularizer=tf.keras.regularizers.l1(0.01),

                kernel_initializer='GlorotUniform',

                kernel_regularizer=tf.keras.regularizers.l1_l2(l1=0.01, l2=0.04),

                    bias_initializer="Ones",name='layer_out')(layer_3)

    model = tf.keras.Model(inputs=input_x, outputs=pred_y)

    五、配置tensorboard可视化

    #配置tensorboard可视化logdir = 'tensorboardLogs'

    if not os.path.exists(logdir):

        os.mkdir(logdir)

    output_model_file = os.path.join(logdir, "MySecondModel.h5")

    callbacks = [

    # 打开CMD密令窗口,输入:tensorboard --logdir "./tensorboardLogs"启动可视化网页

    tf.keras.callbacks.TensorBoard(log_dir=logdir),# 定义TensorBoard对象

    #模型保存配置

    tf.keras.callbacks.ModelCheckpoint(output_model_file,save_best_only = True),

    tf.keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3),#模型提前停止条件

    ]

    六、模型编译和训练

    #模型编译model.compile(

    optimizer=tf.keras.optimizers.RMSprop(0.05),#学习率loss=tf.keras.losses.categorical_crossentropy,metrics=['accuracy'])

    #模型拟合

    model.fit(dataset, epochs=5, steps_per_epoch=30,

    validation_data=val_dataset, validation_steps=3,callbacks=callbacks)

    七、训练打印信息

    Train for 30 steps, validate for 3 steps

    Epoch 1/5

    2020-07-22 20:58:20.428441: Itensorflow/core/profiler/lib/profiler_session.cc:184] Profiler session started.

     1/30 [>.............................]- ETA: 38s - loss: 25.6076 - accuracy: 0.0400

    WARNING:tensorflow:Method (on_train_batch_end) is slow compared to thebatch update (0.111702). Check your callbacks.

     2/30[=>............................] - ETA: 21s - loss: 113.4014 - accuracy:0.0750

    13/30 [============>.................] - ETA: 2s - loss: 843009.6592 -accuracy: 0.0815

    25/30 [========================>.....] - ETA: 0s - loss: 1638163.0178 -accuracy: 0.0824

    30/30 [==============================] - 2s 69ms/step - loss: 1890052.1565- accuracy: 0.0840 - val_loss: 2783116.2500 - val_accuracy: 0.0667

    Epoch 2/5

     1/30[>.............................] - ETA: 0s - loss: 2688977.5000 - accuracy:0.0600

    10/30 [=========>....................] - ETA: 0s - loss: 6145043.2250 -accuracy: 0.0690

    22/30 [=====================>........] - ETA: 0s - loss: 6733185.3750 -accuracy: 0.0764

    30/30 [==============================] - 0s 8ms/step - loss: 7063126.7583- accuracy: 0.0747 - val_loss: 11274707.6667 - val_accuracy: 0.0933

    Epoch 3/5

     1/30[>.............................] - ETA: 0s - loss: 11241677.0000 - accuracy:0.0900

    10/30 [=========>....................] - ETA: 0s - loss: 12574605.9500- accuracy: 0.0670

    21/30 [====================>.........] - ETA: 0s - loss: 13925799.1190- accuracy: 0.0714

    30/30 [==============================] - 0s 7ms/step - loss: 14864912.3167- accuracy: 0.0723 - val_loss: 24675306.6667 - val_accuracy: 0.1200

    Epoch 4/5

     1/30 [>.............................]- ETA: 0s - loss: 24482984.0000 - accuracy: 0.0500

    12/30 [===========>..................] - ETA: 0s - loss: 19740043.4167- accuracy: 0.0900

    23/30 [======================>.......] - ETA: 0s - loss: 21752499.1739- accuracy: 0.0870

    30/30 [==============================] - 0s 7ms/step - loss: 23650425.7000- accuracy: 0.0850 - val_loss: 29904261.3333 - val_accuracy: 0.0667

    Epoch 5/5

     1/30[>.............................] - ETA: 0s - loss: 29330360.0000 - accuracy:0.0600

    12/30 [===========>..................] - ETA: 0s - loss: 31070796.6667- accuracy: 0.0592

    16/30 [===============>..............] - ETA: 0s - loss: 32206105.0000- accuracy: 0.0637

    24/30 [=======================>......] - ETA: 0s - loss: 32991959.0833- accuracy: 0.0729

    30/30 [==============================] - 0s 12ms/step - loss:34828229.5333 - accuracy: 0.0740 - val_loss: 31032930.0000 - val_accuracy:0.0600

    八、查看tensorboard可视化

    在项目路径中输入cmd

    九、查看可视化信息

    在网页中输入http://localhost:6006/,建议使用360浏览器,反正我的火狐浏览器看不了profile。

    十、模型预测

    # coding: utf-8

    import tensorflow as tf

    import numpy as np

    import os

    #预测数据准备pre_in = np.random.random((10, 128))

    logdir ='tensorboardLogs'

    input_model = os.path.join(logdir, "MySecondModel.h5")

    # 导入模型

    model = tf.keras.models.load_model(input_model)

    #预测

    result = model.predict(pre_in, batch_size=32)

    print(result)

    十一、预测结果

    [[0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

     [0.0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

     [0. 0.0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

     [0.0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

     [0.0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

     [0.0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

     [0.0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

     [0.0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

     [0.0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

     [0.0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]]

    TensorFlow2第二篇测试用例到此结束~~~~~~~~~~

    相关文章

      网友评论

          本文标题:TensorFlow2-通过KerasModel创建模型

          本文链接:https://www.haomeiwen.com/subject/sgitrktx.html