tensorflow2教程-基础MLP网络

tensorflow2教程-基础MLP网络

tensorflow2教程-基础MLP网络

最全Tensorflow 2.0 入门教程持续更新:

Doit:最全Tensorflow 2.0 入门教程持续更新zhuanlan.zhihu.com图标

完整tensorflow2.0教程代码请看https://github.com/czy36mengfei/tensorflow2_tutorials_chinese (欢迎star)

本教程主要由tensorflow2.0官方教程的个人学习复现笔记整理而来,中文讲解,方便喜欢阅读中文教程的朋友,官方教程:https://www.tensorflow.org


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']

tensorflow 2.0 入门教程

TensorFlow 2.0 教程- Keras 快速入门

TensorFlow 2.0 教程-keras 函数api

TensorFlow 2.0 教程-使用keras训练模型

TensorFlow 2.0 教程-用keras构建自己的网络层

Doit:TensorFlow2教程-keras模型保持和序列化


参考:buomsoo-kim/Easy-deep-learning-with-Keras

编辑于 2019-05-02

文章被以下专栏收录