Convert form keras to pytorch

Saida2020: <p>Hello</p>
<p>I am using pytorch . But I have model architecture that use keras and I want to convert it to pytorch . I want to understand the model (How many layers - what are those layers in order and activation functions they use for every one)</p>
<p>import inspect<br>
import os<br>
import sys</p>
<p>currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))<br>
parentdir = os.path.dirname(currentdir)<br>
sys.path.insert(0, parentdir)</p>
<p>import numpy as np<br>
from keras.models import Model<br>
from keras.layers import Dense, CuDNNLSTM, Bidirectional, Input, Dropout, concatenate, Conv1D, <br>
BatchNormalization<br>
from keras.layers.wrappers import TimeDistributed<br>
from layers.utility import slice_tensor<br>
from keras import optimizers<br>
from keras import regularizers</p>
<p>np.random.seed(0)</p>
<p>def model_a_cnn_bilstm(n_classes, convs=[3, 5, 7], dense_size=200, lstm_size=400, dropout_rate=0.5,<br>
features_to_use=[‘onehot’, ‘pssm’], filter_size=256, lr=0.001):<br>
‘’’<br>
:param n_classes:<br>
:param convs:<br>
:param dense_size:<br>
:param lstm_size:<br>
:param dropout_rate:<br>
:param features_to_use:<br>
:param filter_size:<br>
:return:<br>
‘’’<br>
visible = Input(shape=(None, 408))</p>
<pre><code># slice different feature types
biophysical = slice_tensor(2, 0, 16, name='biophysicalfeatures')(visible)
embedding = slice_tensor(2, 16, 66, name='skipgramembd')(visible)
onehot = slice_tensor(2, 66, 87, name='onehot')(visible)
pssm = slice_tensor(2, 87, 108, name='pssm')(visible)
elmo = slice_tensor(2, 108, 408, name='elmo')(visible)

# create input based-on selected features
input_dict = {'pssm': pssm, 'onehot': onehot, 'embedding': embedding, 'elmo': elmo,
              'biophysical': biophysical}
features = []
for feature in features_to_use:
    features.append(input_dict[feature])

## batch normalization on the input features
if len(features_to_use) == 1:
    conclayers = features
    input = BatchNormalization(name='batchnorm_input')(features[0])
else:
    input = BatchNormalization(name='batchnorm_input')(concatenate(features))
    conclayers = [input]

# performing the conlvolutions
for idx, conv in enumerate(convs):
    idx = str(idx + 1)
    conclayers.append(BatchNormalization(name='batch_norm_conv' + idx)(
        Conv1D(filter_size, conv, activation="relu", padding="same", name='conv' + idx,
               kernel_regularizer=regularizers.l2(0.001))(input)))
conc = concatenate(conclayers)

# Dropout and Dense Layer before LSTM
if dropout_rate &gt; 0:
    drop_before = Dropout(dropout_rate, name='dropoutonconvs')(conc)
    dense_convinp = Dense(dense_size, activation='relu', name='denseonconvs')(drop_before)
else:
    dense_convinp = Dense(dense_size, activation='relu', name='denseonconvs')(conc)

# Batch normalize the results of dropout
dense_convinpn = BatchNormalization(name='batch_norm_dense')(dense_convinp)

# LSTM
lstm = Bidirectional(CuDNNLSTM(lstm_size, return_sequences=True, name='bilstm'))(dense_convinpn)
drop_after_lstm = Dropout(dropout_rate)(lstm)
dense_out = Dense(dense_size, activation='relu')(drop_after_lstm)

# Labeling layer layer
timedist = TimeDistributed(Dense(n_classes, activation='softmax'))(dense_out)
model = Model(inputs=visible, outputs=timedist)
adam = optimizers.Adam(lr=lr)
model.compile(loss='categorical_crossentropy', optimizer=adam, weighted_metrics=['accuracy'],
              sample_weight_mode='temporal')

# print model
print(model.summary())
return model, 'model_a_cnn_bilstm#' + '#'.join(features_to_use) + '@conv' + '_'.join(
    [str(c) for c in convs]) + '@dense_' + str(dense_size) + '@lstm' + str(lstm_size) + '@drop_rate' + str(
    dropout_rate) + '@filtersize_' + str(filter_size) + '@lr_' + str(lr)</code></pre>