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']
    
  • 相关阅读:
    AFNet3.0上传图片
    最新 AFNetworking 3.0 简单实用封装
    iOS开发密码输入数字和字母混合
    IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)(转)
    iOS开发探索-图片压缩处理
    常用第三方框架插件
    2.1创建直线
    1.4用向导创建Hello,world程序
    vs2008找不到ObjectARX MFC Support
    vc6.0错误:error C2653: 'CCreateEnt' : is not a class or namespace name
  • 原文地址:https://www.cnblogs.com/peijz/p/12985628.html
Copyright © 2011-2022 走看看