如何将此keras CNN模型转换为pytorch版本 [英] How can I convert this keras cnn model to pytorch version

查看:1023
本文介绍了如何将此keras CNN模型转换为pytorch版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想转换为pytorch的示例keras代码.我的输入数据集是10000 * 1 * 102(二维标签).数据集包括10000个样本.每个样本包含一行,其中包含102个要素.我正在考虑使用1dcnn进行回归.

This is the example keras code that I want to convert to pytorch. My input dataset is 10000*1*102 (two dimensions for labels). The dataset includes 10000 samples. Each sample contains one row with 102 features. I am thinking to use 1dcnn for regression.

PS:可以根据我的10000 * 1 * 102数据集调整超参数(例如,过滤器,kernel_size,步幅,填充).

PS: hyper-parameter (e.g. filters, kernel_size, stride, padding) could be adjusted based on my 10000*1*102 dataset.

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))

推荐答案

欢迎使用pytorch. :) 我很高兴您决定从Keras切换到PyTorch.对我来说,这是重要的一步,它可以更详细地了解神经网络.如果您对代码有任何特定疑问,或者如果代码不起作用,请告诉我.

Welcome to pytorch. :) I am really glad you decide to switch from Keras to PyTorch. It was an important step for me to understand how NNs work in more detail. If you have any specific questions about code or if it isn't working please let me know.

import torch.nn as nn
a0 = nn.Conv1D(n_timesteps, 64, 3)
a1 = nn.Relu()
b0 = nn.Conv1D(64, 64, 3)
b1 = nn.Relu()
c0 = torch.nn.Dropout(p=0.5)
d0 = nn.MaxPool1d(2)
e0 = nn.Flatten()
e1 = nn.Linear(32*n_timesteps,100)
e2 = nn.Relu()
e3 = nn.Linear(n_outputs)
f0 = nn.Softmax(dim=1)

model = nn.Sequential(a0,a1,b0,b1,c0,d0,e0,e1,e2,e3,f0)

这篇关于如何将此keras CNN模型转换为pytorch版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆