user14086988
user14086988

Reputation:

How can I import my CNN-model in another code?

This is my "model", I just added a piece of it here for understanding purpose. I want this model to be imported in another piece of code named as "trainer".

import torch.nn as nn
import torch

class Signet(nn.Module):
    def __init__(self, num_users=10):
        super(Signet, self).__init__()
        self.num_classes = num_users
        self.forward_util_layer = {}


This is another piece of code. Here "Model" is what I want to import like I mentioned above.

import model

class Trainer:
    def __init__(self, trainData, testData, num_users=10):
        self.trainData = trainData
        self.testData = testData

What are the ways to import it. Where to save my model.ipynb, so that my trainer.ipynb can import it. Or if there is any way to call it directly in my trainer.ipynb code?

Upvotes: 1

Views: 181

Answers (1)

Pritesh Gohil
Pritesh Gohil

Reputation: 476

Move your model code to model.py file. Keep that file in the same directory as your trainer.ipynb

Now, to load the model in the ipynb file, follow these steps.

import sys  
sys.path.insert(0, '/path/to/model/folder')
from model import Signet
m = Signet(15)

Upvotes: 0

Related Questions