Hello folks,
New to Torch and trying to translate a Keras model to Torch. I’m finding a slight difference in model accuracy that makes me think I haven’t done the model translation properly. Here is the code for the two models:
Keras
def keras_model(input_tensor_length):
he_uniform_initializer = he_uniform()
input_tensor = Input(shape=(1, input_tensor_length))
dense = Dense(
units=100, activation="relu", kernel_initializer=he_uniform_initializer
)(input_tensor)
dense = Dropout(0.5)(dense)
dense = Dense(
units=50, activation="relu", kernel_initializer=he_uniform_initializer
)(dense)
dense = Dropout(0.5)(dense)
dense = Dense(
units=25, activation="relu", kernel_initializer=he_uniform_initializer
)(dense)
predictions = Dense(1, activation="linear")(dense)
sigm = Dense(1, activation="sigmoid")(predictions)
model = Model(inputs=input_tensor, outputs=sigm)
model.compile(
optimizer=Adam(lr=0.0001), loss="binary_crossentropy", metrics=["mae", "acc"]
)
Torch
class model(nn.Module):
def __init__(self):
super(model, self).__init__()
self.mod = nn.Sequential(
nn.Linear(input_dim,100),
nn.Linear(100,50),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(50, 25),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(25, 1),
nn.Sigmoid()
)
def forward(self, x):
y = self.mod(x)
return y
Both are trained using BCE on 0/1 targets. Binary Accuracy is computed on both validation sets using the same function here and the resulting accuracies are Kears: 0.697 Torch 0.689:
def binary_accuracy(self,y_true, y_pred):
N = sum(y_true == np.round(y_pred))
return N/len(y_true)