tensoflow笔记

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Dense, Activation, Model

#方法一:
layers = [Dense(32, input_shape = (784,)),
   Activation('relu'),
   Dense(10),
   Activation('softmax')]
model = Sequential(layers)

#方法二:
model = Sequential()
model.add(Dense(32, input_shape = (784,)))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))

#方法三:
# 定义输入层,确定输入维度
input = Input(shape = (784, ))
# 2个隐含层,每个都有64个神经元,使用relu激活函数,且由上一层作为参数
x = Dense(64, activation='relu')(input)
x = Dense(64, activation='relu')(x)
# 输出层
y = Dense(10, activation='softmax')(x)
# 定义模型,指定输入输出
model = Model(input=input, output=y)
# 编译模型,指定优化器,损失函数,度量
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
# 模型拟合,即训练
model.fit(data, labels)