<jupyter_start><jupyter_text>Load ksent vectors of random genome samples of 16kb<jupyter_code>#export
runs = [
    pd.read_pickle("/home/serge/development/fastai-genomic/data/ksent_vectors_baccilium_top3_500_samples_per_fasta.pkl"),
    pd.read_pickle("/home/serge/development/fastai-genomic/data/ksent_vectors_baccilium_top3_500_samples_per_fasta_run2.pkl"),
    pd.read_pickle("/home/serge/development/fastai-genomic/data/ksent_vectors_baccilium_1000_samples_per_fasta.pkl"),
    pd.read_pickle("/home/serge/development/fastai-genomic/data/ksent_vectors_baccilium_1000_samples_per_fasta_run2.pkl")
]
df = pd.concat(runs)
df = df.sample(frac=1).reset_index()
df.head()
df.ksent.values.shape
from denoiser import denoise

ksent = np.vstack(df.ksent.values)
v_dn = denoise(ksent)
data = [v_dn[i] for i in range(v_dn.shape[0])]
d=pd.DataFrame(index=df.index)
d["spicies"] = df.spicies.values
df["ksent"] = data
df.head()<jupyter_output><empty_output><jupyter_text>Create Dataset<jupyter_code>#export
valid_idx = random.sample(range(df.shape[0]), int(np.floor(df.shape[0]* 0.2)))

db = (ItemList.from_df(df,cols="ksent").
      split_by_idx(valid_idx).
      label_from_df(cols="spicies").
      databunch())<jupyter_output><empty_output><jupyter_text>Create Model<jupyter_code>#export
def submodel(dims, bias=False):
    layer_dims = list(zip(dims[:-1],dims[1:]))
    fcl = [nn.Linear(*x, bias=bias) for x in layer_dims]
    [nn.init.xavier_uniform_(m.weight) for m in fcl]
    if bias: 
        for l in fcl: l.bias.data.normal_(0, 1)
    relu = [nn.ReLU() for _ in range(len(fcl))]
    layers = np.asarray(list(zip(fcl, relu))).ravel()[:-1]
    return nn.Sequential(*layers)

#export
class Classifier (nn.Module):

    def __init__(self, encoder_dims, classifier_dims):
        super().__init__()
        self.encoder = submodel(encoder_dims,bias=True)
        self.classifier = submodel(classifier_dims,bias=True)


    def forward(self, x):
        x = self.encoder(x)
        return F.softmax(self.classifier(x), dim=1)

    def save_encoder(self,file:PathOrStr):
        torch.save(self.encoder.state_dict(), path)
    
    def save_model(self, file:PathOrStr, epoch):
        torch.save({
            'epoch': epoch,
            'model_state_dict': model.state_dict(),
            'optimizer_state_dict': optimizer.state_dict(),
            'loss': loss}, file)
model = Classifier([100,50,3,2], [2,20,3]).double()
model<jupyter_output><empty_output><jupyter_text>Learner<jupyter_code>learn = Learner(db, model,metrics=[accuracy])
learn.loss_func
learn.lr_find()
learn.recorder.plot()
learn.fit_one_cycle(50,1e-2)
learn.recorder.plot_metrics()
interpretation = learn.interpret()
interpretation.plot_confusion_matrix()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup