## 现实比理论要复杂 原荐

我是任玉琢

### 函数式 API

``````def run():
# 我们学过的用 Sequential 定义层
seq_model = Sequential()
seq_model.summary()
​
# 用函数的方式定义层
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()
``````

### 多输入模型

``````#!/usr/bin/env python3
​
import time
​
import keras
​import numpy as np
from keras import Input
from keras import layers
from keras.models import Model
​
​
def run():
text_vocabulary_size = 10000
question_vocabulary_size = 10000
# 文本输入
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)
# 添加一个 softmax 分类器
# 构建模型
model.summary()
​
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc'])
​
# 生成虚拟训练数据，参考意义不大
num_samples = 1000
max_length = 100
text = np.random.randint(1, text_vocabulary_size, size=(num_samples, max_length))
question = np.random.randint(1, question_vocabulary_size, size=(num_samples, max_length))
model.fit({'text': text, 'question': question}, answers, epochs=10, batch_size=128)
​
​
if __name__ == "__main__":
time_start = time.time()
run()
time_end = time.time()
print('Time Used: ', time_end - time_start)
``````

### 多输出模型

``````def run():
vocabulary_size = 50000
num_income_groups = 10
posts_input = Input(shape=(None,), dtype='int32', name='posts')
embedded_posts = layers.Embedding(256, vocabulary_size)(posts_input)
x = layers.Conv1D(128, 5, activation='relu')(embedded_posts)
x = layers.MaxPooling1D(5)(x)
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.summary()

# 关键在这里
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_targets, income_targets, gender_targets], epochs=10, batch_size=64)
``````

### 图结构

#### Inception 模块

``````branch_a = layers.Conv2D(128, 1, activation='relu', strides=2)(x)
branch_b = layers.Conv2D(128, 1, activation='relu')(x)
branch_b = layers.Conv2D(128, 3, activation='relu', strides=2)(branch_b)
branch_c = layers.AveragePooling2D(3, strides=2)(x)
branch_c = layers.Conv2D(128, 3, activation='relu')(branch_c)
branch_d = layers.Conv2D(128, 1, activation='relu')(x)
branch_d = layers.Conv2D(128, 3, activation='relu')(branch_d)
branch_d = layers.Conv2D(128, 3, activation='relu', strides=2)(branch_d)
output = layers.concatenate([branch_a, branch_b, branch_c, branch_d], axis=-1)
``````

#### 残差连接

``````x = ...
y = layers.Conv2D(128, 3, activation='relu', padding='same')(x)
y = layers.Conv2D(128, 3, activation='relu', padding='same')(y)
y = layers.Conv2D(128, 3, activation='relu', padding='same')(y)
``````

### 共享层权重

``````# 定义一次
lstm = layers.LSTM(32)
left_input = Input(shape=(None, 128))
# 使用
left_output = lstm(left_input)
right_input = Input(shape=(None, 128))
# 使用
right_output = lstm(right_input)
merged = layers.concatenate([left_output, right_output], axis=-1)
predictions = layers.Dense(1, activation='sigmoid')(merged)
model = Model([left_input, right_input], predictions)
``````

``````# Xception 是一个网络
xception_base = applications.Xception(weights=None, include_top=False)
left_input = Input(shape=(250, 250, 3))
​right_input = Input(shape=(250, 250, 3))
left_features = xception_base(left_input)
right_input = xception_base(right_input)
merged_features = layers.concatenate([left_features, right_input], axis=-1)
``````

### 总结

• 本文首发自公众号：RAIS，欢迎关注！

### 评论(0)

2019/04/14
0
0

2019/03/18
0
0
2019人工智能学习路线图（怎么学好人工智能）

QF _AI+python
2019/01/17
0
0

2019/01/25
0
0

【IT168 资讯】目前，我们正处于“量子争夺赛”中。谷歌IBM和全球研究人员在解决一些复杂的计算的时候，通常通过最先进的量子计算机来解决。 　　量子计算机与当今的家庭电脑非常相似只是...

it168网站
2017/10/24
0
0

leetcode1227(面试题 17.09. 第 k 个数)--C语言实现

17分钟前
17
0

17分钟前
17
0
GIT 使用

23分钟前
16
0
Centos8 配置静态IP

W_Lu
27分钟前
28
0
java 金钱元和分互相转换

import java.math.BigDecimal;import java.text.DecimalFormat;/** * @ProjectName: LieIdle * @Package: com.jinzhending.comm * @Author: huat * @Date: 2020/4/6 9:04 * ......

31分钟前
11
0