例如,我们要同时处理文本数据(全连接层),图片数据(2D卷积层),就不能使用Sequential模型,我们就需要多模态输入,例如,我们的输入数据为元数据,文本描述,图片,来预测一个一个商品的价格,我们就可以将Dense模块,RNN模块,卷积神经网络模块结合起来
例如,给定一部小说,我们将要他归类,还要预测其写作时间,这时就要两个输出,一个输出用来判别其类别,另一种输出来预测其写作时间
from keras.models import Sequential,Model
from keras import layers
from keras import Input
# 之前学习过的Sequential模型
seq_model = Sequential()
seq_model.add(layers.Dense(32,activation = 'relu',input_shape = ((64,))))
seq_model.add(layers.Dense(32,activation = 'relu'))
seq_model.add(layers.Dense(10,activation = 'softmax'))
seq_model.summary()
# 对应的函数式API实现
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()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_4 (Dense) (None, 32) 2080
_________________________________________________________________
dense_5 (Dense) (None, 32) 1056
_________________________________________________________________
dense_6 (Dense) (None, 10) 330
=================================================================
Total params: 3,466
Trainable params: 3,466
Non-trainable params: 0
_________________________________________________________________
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 64) 0
_________________________________________________________________
dense_7 (Dense) (None, 32) 2080
_________________________________________________________________
dense_8 (Dense) (None, 32) 1056
_________________________________________________________________
dense_9 (Dense) (None, 10) 330
=================================================================
Total params: 3,466
Trainable params: 3,466
Non-trainable params: 0
_________________________________________________________________
举例:一个问答问题,输入为一个问题和所有问题的文本数据集合,输出为一个回答
from keras.models import Model
from keras import layers
from keras import Input
import keras
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)
encode_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)
encode_question = layers.LSTM(16)(embedded_question)
# 将两个不同的输入,经过不同的网络,得到的结果级联起来
concatenated = layers.concatenate([encode_text,encode_question],axis = -1)
# 最后添加一个softmax分类器
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'])
# 将数据输入到多输入模型中
import numpy as np
num_samples= 1000
max_length = 100
# 生成虚构的numpy数据
text = np.random.randint(1,text_vocabulary_size,size = (num_samples,max_length)) #参数,low,high,size
question = np.random.randint(1,question_vocabulary_size,size = (num_samples,max_length))
answers = np.random.randint(answer_vocabulary_size,size = (num_samples))
# 回答进行独热编码
answers = keras.utils.to_categorical(answers,answer_vocabulary_size)
# 两种方法进行模型训练
# 1.使用输入组成的列表
model.fit([text,question],answers,epochs = 10,batch_size = 128)
# 2.使用输入组成的字典
model.fit({'text':text,'question':question},answers,epochs = 10,batch_size = 128)
Epoch 1/10
1000/1000 [==============================] - 6s 6ms/step - loss: 6.2141 - acc: 0.0020
Epoch 2/10
1000/1000 [==============================] - 3s 3ms/step - loss: 6.1979 - acc: 0.0420
Epoch 3/10
1000/1000 [==============================] - 3s 3ms/step - loss: 6.1546 - acc: 0.0220
Epoch 4/10
1000/1000 [==============================] - 3s 3ms/step - loss: 6.0582 - acc: 0.0060
Epoch 5/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.9883 - acc: 0.0080
Epoch 6/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.9284 - acc: 0.0080
Epoch 7/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.8564 - acc: 0.0100
Epoch 8/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.7803 - acc: 0.0130
Epoch 9/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.6730 - acc: 0.0160
Epoch 10/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.6041 - acc: 0.0230
Epoch 1/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.5310 - acc: 0.0370
Epoch 2/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.4517 - acc: 0.0410
Epoch 3/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.4116 - acc: 0.0430
Epoch 4/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.3387 - acc: 0.0530
Epoch 5/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.2483 - acc: 0.0550
Epoch 6/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.2013 - acc: 0.0540
Epoch 7/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.1401 - acc: 0.0740
Epoch 8/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.0840 - acc: 0.0900
Epoch 9/10
1000/1000 [==============================] - 3s 3ms/step - loss: 5.0057 - acc: 0.0870
Epoch 10/10
1000/1000 [==============================] - 3s 3ms/step - loss: 4.9580 - acc: 0.0920
一个网络,同时预测数据的不同性质
from keras import layers
from keras import Input
from keras.models import Model
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)
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])
# 将不同预测的损失加在一起,组成一个全局损失,优化这个全局损失
# 两种方法编译模型
# 1.列表级联,还可以给不同的损失加上不同的权重
model.compile(optimizer = 'rmsprop',
loss = ['mse','categorical_crossentropy','binary_crossentropy'],
loss_weights = [0.25,1.0,10.])
# 2.用字典
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)
model.fit(posts,{'age':age_targets,'income':income_targets.'gender':gender_targets},epochs = 10,batch_size = 64)
# 1. Inception模块,一个输入,多个分支,一个输出
from keras import layers
x = Input(shape = (None,None,10))
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)
# 将不同的分支得到的结果级联起来oo
output = layers.concatenate([branch_a,branch_b,branch_c,branch_d],axis = -1)
# 2.残差连接
# 假设输入张量是4维的
from keras import layers
# 特征图尺寸相同
x = ...
y = layers.Conv2D(128,3,activation = 'relu',padding = 'same')(x)
y = layers.Conv2D(128,3,activation = 'relu',paddingg = 'same')(y)
y = layers.Conv2D(128,3,activation = 'relu',padding = 'same')(y)
y = layers.add([y,x]) # 将原始x与输出y相加
# 特征图尺寸不同
x = ...
y = layers.Conv2D(128,3,activation = 'relu',padding = 'same')(x)
y = layers.Conv2D(128,3,activation = 'relu',paddingg = 'same')(y)
y = layers.MaxPooling2D(2,strides = 2)(y)
residual = layers.Conv2D(128,1,strides = 2,padding = 'same')(x)
y = layers.add([y,residual])
from keras import layers
from keras import Input
from keras.models import Model
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')(megred)
model = Model([left_input,right_input],predictions)
model.fit([left_data,right_data],targets)
# 一个层实例可能被多次重复使用,它可以被调用任意多次,每次都重复使用一组相同的权重
# 模型可以看作是更大的层
from keras import layers
from keras import applications
from keras import Input
# 这是一个自带的模型
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_features = xception_base(right_input)
megred_features = layers.concatenate([left_features,right_features],axis = -1)
1.如果你需要实现的架构不仅仅是层的线性堆叠,就不要局限于sequential API
2.使用Keras函数式API来构建多输入模型,多输出模型,和具有复杂的内部网络拓扑结构的模型
3.通过多西调用相同的层实例或者模型实例,