keras cnn以及lstm测试

本文详细介绍并实践了三种深度学习模型:卷积神经网络(CNN)、长短时记忆网络(LSTM)及CNN与LSTM的结合使用。通过创建虚假数据集,演示了如何使用Keras库构建、训练和评估这些模型,为读者提供了深入理解模型工作原理的机会。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

参考:
https://www.kaggle.com/nikhilroxtomar/embeddings-cnn-lstm-models-lb-0-683

Python是一种高级编程语言,Keras是一个用于构建神经网络模型的深度学习库,CNN代表卷积神经网络,LSTM代表长短期记忆。Python的流行性使得它成为使用Keras库构建神经网络模型的理想选择。 卷积神经网络(CNN)是一种前馈神经网络,常用于计算机视觉任务。其核心思想是通过卷积运算来提取图像的特征。CNN在图像识别、物体检测和语义分割等任务中表现出色。 长短期记忆(LSTM)是一种适用于处理序列数据的循环神经网络(RNN)的特殊类型。与普通的RNN相比,LSTM能够更好地捕捉到长期依赖关系。LSTM通过控制记忆单元来处理序列中的信息,对于许多自然语言处理任务,如语言建模和机器翻译,LSTM是一个非常强大的工具。 在使用Keras库时,借助Python的灵活性和易用性,我们可以轻松地利用CNNLSTM来构建复杂的深度学习模型。Keras库提供了丰富的高级API和多种预训练模型,可以帮助我们快速搭建和训练模型。 例如,我们可以使用Keras库中的layers模块来创建CNN模型的卷积层和池化层,然后使用LSTM层来处理时序数据。在构建模型时,我们可以选择性地添加Batch Normalization层或Dropout层来提高模型的泛化能力。 使用Keras库可以进行模型的编译、训练和评估等操作,还可以进行模型的保存和加载。同时,Keras库还提供了方便的可视化工具,如TensorBoard,可以帮助我们更好地理解和分析模型的结构和性能。 综上所述,Python、KerasCNNLSTM是一组强大的工具和技术,可以用于构建和训练复杂的深度学习模型,解决图像识别、自然语言处理和其他各种任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值