File size: 1,515 Bytes
f84aec0
4afafa1
 
 
 
f84aec0
4afafa1
 
 
 
f84aec0
4afafa1
 
 
f84aec0
4afafa1
 
 
 
 
 
 
 
 
 
 
 
84da857
4afafa1
 
 
 
 
 
 
 
 
 
 
6061089
4afafa1
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
import torch
import torch.nn as nn
import time
import math

class Lreg(nn.Module):
    def __init__(self):
        super(Lreg,self).__init__()
        self.l = nn.Linear(1,1)

    def forward(self,x):
        y = self.l(x)
        return y

model = Lreg()
crit = nn.MSELoss()
optim = torch.optim.SGD(model.parameters(),lr=0.01)

def weightsbias(weight,bias,predicted,progress=gr.Progress()):
    weight = float(weight)
    bias = float(bias)
    predicted = float(predicted)
    x = torch.randn(1000)
    y = (x*weight)+bias
    progress(0,desc="Starting")
    model.train()
    for i in progress.tqdm(range(1,100)):
        for idx,ele in enumerate(x):
            ele = ele.unsqueeze(0)
            out = model(ele)
            optim.zero_grad()
            loss = crit(out,y[idx].unsqueeze(0))
            loss.backward()
            optim.step()
    model.eval()
    t = torch.tensor([predicted],dtype=torch.float32)
    rout = model(t)
    c = model.state_dict()
    return [rout.item(),c['l.weight'].item(),c['l.bias'].item()]


with gr.Blocks() as iface:
    weights = gr.Textbox(label="Weights")
    bias = gr.Textbox(label="Bias")
    predicted = gr.Textbox(label="Prediction Number")
    output = gr.Number(label="Output Predicted")
    w = gr.Number(label="Calculated Weight ")
    b = gr.Number(label="Calculated Bias ")
    train = gr.Button("Train")
    train.click(fn=weightsbias, inputs=[weights,bias,predicted], outputs=[output,w,b])

iface.queue(concurrency_count=10).launch()