keras函数式API以及plot_model的用法

顺序模型举例

from keras import layers, Input, Model
input_tensor = Input(shape=(64,))
x = layers.Dense(32, activation='relu')(input_tensor)
x = layers.Dense(32, activation='relu')(x)
output_tensor = layers.Dense(10, activation='softmax')(x)
model = Model(input_tensor, output_tensor)
model.summary()

为了看出来这个模型的每层的输入输出,除了用model.summary(),我们还可以使用plot_model,它会返回一张图片,其中包含有每一层输入和输出的shape

plot_model使用

使用这个函数需要安装 Python 的pydotplus 库和 pydot-ng 库,还需要安装 graphviz 库,并且电脑中还需要下载graphviz这个软件来安装一波,最最重要的是还需要将graphviz这个安装文件下面的bin文件添加到系统路径里面,是不是听上去有点小麻烦

(1)安装pydotplus 库,pydot-ng 库, graphviz 库直接利用pip install安装就可以啦
需要注意的是需要修改keras>utils>vis_utils.py文件,把里面的pydot都替换为pydotplus
(2)安装graphviz,在电脑上装好之后可以里利用下面代码加入到系统路径下面

os.environ["PATH"] += os.pathsep + r'C:\Program Files (x86)\Graphviz 2.28\bin'

3.plot_model的使用

plot_model(model, to_file=r'./data/modle.png', show_shapes=True)

在这里插入图片描述

多输入模型

from keras import layers, Input, Model
text_vocabulary_size = 10000
question_vocabulary_size = 10000
answer_vocabulary_size = 500

text_input = Input(shape=(None,), dtype='int32', name='text')
embedded_text = layers.Embedding(text_vocabulary_size, 64)(text_input)
encoded_text = layers.LSTM(32)(embedded_text)
question_input = Input(shape=(None,),dtype='int32',name='question')
embedded_question = layers.Embedding(question_vocabulary_size, 32)(question_input)
encoded_question = layers.LSTM(16)(embedded_question)
concatenated = layers.concatenate([encoded_text, encoded_question],axis=-1)
answer = layers.Dense(answer_vocabulary_size,activation='softmax')(concatenated)
model = Model([text_input, question_input], answer)

model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['acc'])
模型训练
model.fit({'text': text, 'question': question}, answers,
epochs=10, batch_size=128)

在这里插入图片描述

多输出模型

x = layers.Conv1D(256, 5, activation='relu')(x)
x = layers.Conv1D(256, 5, activation='relu')(x)
x = layers.MaxPooling1D(5)(x)
x = layers.Conv1D(256, 5, activation='relu')(x)
x = layers.Conv1D(256, 5, activation='relu')(x)
x = layers.GlobalMaxPooling1D()(x)
x = layers.Dense(128, activation='relu')(x)
age_prediction = layers.Dense(1, name='age')(x)
income_prediction = layers.Dense(num_income_groups,
activation='softmax',
name='income')(x)
gender_prediction = layers.Dense(1, activation='sigmoid', name='gender')(x)
model = Model(posts_input,
[age_prediction, income_prediction, gender_prediction])

model.compile(optimizer='rmsprop',
loss={'age': 'mse',
'income': 'categorical_crossentropy',
'gender': 'binary_crossentropy'},
loss_weights={'age': 0.25,
'income': 1.,
'gender': 10.})
模型训练
model.fit(posts, {'age': age_targets,
'income': income_targets,
'gender': gender_targets},
epochs=10, batch_size=64)

在这里插入图片描述
参考书籍:《python深度学习》

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值