1.cnn测试
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
# 创建虚假数据
x_train = np.random.random((100, 100, 100, 3))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
x_test = np.random.random((20, 100, 100, 3))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)
model = Sequential()
# 输入: 100x100的3通道图像 -> 张量 (100, 100, 3).
# 在每块3x3的区域应用32个卷积过滤器.
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
model.fit(x_train, y_train, batch_size=2, epochs=1)
参考:
http://xiaosheng.me/2017/07/04/article77/
2.lstm测试
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import LSTM
import keras
import numpy as np
data_dim = 10 #词特征维度
timesteps = 7 #句子长度
num_classes = 9 #类别数
max_features=8 #词汇量
batch=5
# 创建虚假的训练数据
x_train = np.random.randint(max_features,size=(batch, timesteps)) #存的是各个字在embedding中的index
y_train = np.random.randint(num_classes, size=(batch, 1))
model = Sequential()
model.add(Embedding(max_features, output_dim=20, input_length=timesteps))
model.add(LSTM(20))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=16, epochs=1)
参考:
http://xiaosheng.me/2017/07/04/article77/
http://frankchen.xyz/2017/12/18/How-to-Use-Word-Embedding-Layers-for-Deep-Learning-with-Keras/
3.cnn 联合 lstm测试
3.1 输入维度
embedding_con.shape,embed_size,max_features,maxlen
#((100000, 1200), 300, 100000, 40)
3.2 测试代码
inp = Input(shape=(maxlen,))
embed = Embedding(max_features, embed_size*4,weights=[embedding_con], trainable=False)(inp)
filter_sizes = [1, 2, 3, 5]
num_filters = 300
conv_0 = Conv1D(num_filters, filter_sizes[0], padding='same', kernel_initializer='normal', activation='relu')(
embed)
conv_1 = Conv1D(num_filters, filter_sizes[1], padding='same', kernel_initializer='normal', activation='relu')(
embed)
conv_2 = Conv1D(num_filters, filter_sizes[2], padding='same', kernel_initializer='normal', activation='relu')(
embed)
conv_3 = Conv1D(num_filters, filter_sizes[3], padding='same', kernel_initializer='normal', activation='relu')(
embed)
l_0 = Bidirectional(CuDNNLSTM(40, return_sequences=True))(conv_0)
l_1 = Bidirectional(CuDNNLSTM(40, return_sequences=True))(conv_1)
l_2 = Bidirectional(CuDNNLSTM(40, return_sequences=True))(conv_2)
l_3 = Bidirectional(CuDNNLSTM(40, return_sequences=True))(conv_3)
atten_0 = Attention(maxlen)(l_0)
atten_1 = Attention(maxlen)(l_1)
atten_2 = Attention(maxlen)(l_2)
atten_3 = Attention(maxlen)(l_3)
conc = concatenate([atten_0, atten_1, atten_2, atten_3])
d1 = Dense(16, activation="relu")(conc)
dp1 = Dropout(0.1)(d1)
outp = Dense(1, activation="sigmoid")(dp1)
print('embed.shape:',embed.shape)
print('conv_0.shape:',conv_0.shape)
print('conv_1.shape:',conv_1.shape)
print('conv_2.shape:',conv_2.shape)
print('conv_3.shape:',conv_3.shape)
print('l_0.shape:',l_0.shape)
print('l_1.shape:',l_1.shape)
print('l_2.shape:',l_2.shape)
print('l_3.shape:',l_3.shape)
print('atten_0.shape:',atten_0.shape)
print('atten_1.shape:',atten_1.shape)
print('atten_2.shape:',atten_2.shape)
print('atten_3.shape:',atten_3.shape)
print('conc.shape:',conc.shape)
print('d1.shape:',d1.shape)
print('dp1.shape:',dp1.shape)
print('outp.shape:',outp.shape)
3.3 输出
embed.shape: (?, 40, 1200)
conv_0.shape: (?, 40, 300)
conv_1.shape: (?, 40, 300)
conv_2.shape: (?, 40, 300)
conv_3.shape: (?, 40, 300)
l_0.shape: (?, 40, 80)
l_1.shape: (?, 40, 80)
l_2.shape: (?, 40, 80)
l_3.shape: (?, 40, 80)
atten_0.shape: (?, 80)
atten_1.shape: (?, 80)
atten_2.shape: (?, 80)
atten_3.shape: (?, 80)
conc.shape: (?, 320)
d1.shape: (?, 16)
dp1.shape: (?, 16)
outp.shape: (?, 1)