zoukankan      html  css  js  c++  java
  • 人工智能深度学习:TensorFlow2.0如何实现基础MLP网络?

    1.回归任务

    # 导入数据
    (x_train, y_train), (x_test, y_test) = keras.datasets.boston_housing.load_data()
    print(x_train.shape, ' ', y_train.shape)
    print(x_test.shape, ' ', y_test.shape)
    (404, 13)   (404,)
    (102, 13)   (102,)
    # 构建模型
    
    model = keras.Sequential([
        layers.Dense(32, activation='sigmoid', input_shape=(13,)),
        layers.Dense(32, activation='sigmoid'),
        layers.Dense(32, activation='sigmoid'),
        layers.Dense(1)
    ])
    
    # 配置模型
    model.compile(optimizer=keras.optimizers.SGD(0.1),
                 loss='mean_squared_error',  # keras.losses.mean_squared_error
                 metrics=['mse'])
    model.summary()
    Model: "sequential_10"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_33 (Dense)             (None, 32)                448       
    _________________________________________________________________
    dense_34 (Dense)             (None, 32)                1056      
    _________________________________________________________________
    dense_35 (Dense)             (None, 32)                1056      
    _________________________________________________________________
    dense_36 (Dense)             (None, 1)                 33        
    =================================================================
    Total params: 2,593
    Trainable params: 2,593
    Non-trainable params: 0
    _________________________________________________________________
    # 训练
    model.fit(x_train, y_train, batch_size=50, epochs=50, validation_split=0.1, verbose=1)
    Train on 363 samples, validate on 41 samples
    Epoch 1/50
    363/363 [==============================] - 0s 430us/sample - loss: 371.0176 - mse: 371.0175 - val_loss: 50.0381 - val_mse: 50.0381
    Epoch 50/50
    363/363 [==============================] - 0s 28us/sample - loss: 80.1490 - mse: 80.1490 - val_loss: 30.6706 - val_mse: 30.6706
    
    
    result = model.evaluate(x_test, y_test)
    102/102 [==============================] - 0s 116us/sample - loss: 75.0492 - mse: 75.0492
    print(model.metrics_names)
    print(result)
    ['loss', 'mse']
    [75.04923741957721, 75.04924]

    2.分类任务

    from sklearn.datasets import load_breast_cancer
    from sklearn.model_selection import train_test_split
    
    whole_data = load_breast_cancer()
    x_data = whole_data.data
    y_data = whole_data.target
    
    x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.3, random_state=7)
    
    print(x_train.shape, ' ', y_train.shape)
    print(x_test.shape, ' ', y_test.shape)
    (398, 30)   (398,)
    (171, 30)   (171,)
    # 构建模型
    model = keras.Sequential([
        layers.Dense(32, activation='relu', input_shape=(30,)),
        layers.Dense(32, activation='relu'),
        layers.Dense(1, activation='sigmoid')
    ])
    
    model.compile(optimizer=keras.optimizers.Adam(),
                 loss=keras.losses.binary_crossentropy,
                 metrics=['accuracy'])
    model.summary()
    Model: "sequential_14"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_46 (Dense)             (None, 32)                992       
    _________________________________________________________________
    dense_47 (Dense)             (None, 32)                1056      
    _________________________________________________________________
    dense_48 (Dense)             (None, 1)                 33        
    =================================================================
    Total params: 2,081
    Trainable params: 2,081
    Non-trainable params: 0
    _________________________________________________________________
    model.fit(x_train, y_train, batch_size=64, epochs=10, verbose=1)
    Epoch 10/10
    398/398 [==============================] - 0s 27us/sample - loss: 0.2597 - accuracy: 0.9045
    
    
    model.evaluate(x_test, y_test)
    171/171 [==============================] - 0s 463us/sample - loss: 0.3877 - accuracy: 0.8772
    
    
    
    
    
    [0.38765583248340596, 0.877193]
    print(model.metrics_names)
    ['loss', 'accuracy']
    
  • 相关阅读:
    全志A10_linux3.0内核编译记录
    C#使用Socket登陆WordPress源码
    UIKeyboardType键盘
    浅谈 iOS 版本号
    学习软件开发应该看的书
    NSPredicate的用法
    ios 技巧总结(不断更新)
    RSA 加解密
    GCD下的几种实现同步的方式
    iOS事件处理
  • 原文地址:https://www.cnblogs.com/peijz/p/12985628.html
Copyright © 2011-2022 走看看